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 | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 32 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 33 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 34 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 35 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 36 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 38 | using android::gui::FocusRequest; |
| 39 | using android::gui::TouchOcclusionMode; |
| 40 | using android::gui::WindowInfo; |
| 41 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 42 | using android::os::InputEventInjectionResult; |
| 43 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 44 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 45 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 46 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 47 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 48 | using testing::AllOf; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 49 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 50 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 51 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | |
| 53 | // An arbitrary device id. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 54 | static constexpr int32_t DEVICE_ID = 1; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 55 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 56 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 57 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 58 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 59 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 60 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 61 | static constexpr int32_t POINTER_1_DOWN = |
| 62 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 63 | static constexpr int32_t POINTER_2_DOWN = |
| 64 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 65 | static constexpr int32_t POINTER_3_DOWN = |
| 66 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 67 | static constexpr int32_t POINTER_0_UP = |
| 68 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 69 | static constexpr int32_t POINTER_1_UP = |
| 70 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 71 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 72 | // 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] | 73 | static constexpr int32_t WINDOW_PID = 999; |
| 74 | static constexpr int32_t WINDOW_UID = 1001; |
| 75 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 76 | // The default pid and uid for the windows created on the secondary display by the test. |
| 77 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; |
| 78 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; |
| 79 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 80 | // The default policy flags to use for event injection by tests. |
| 81 | 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] | 82 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 83 | // An arbitrary pid of the gesture monitor window |
| 84 | static constexpr int32_t MONITOR_PID = 2001; |
| 85 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 86 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 87 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 88 | static constexpr int expectedWallpaperFlags = |
| 89 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 90 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 91 | struct PointF { |
| 92 | float x; |
| 93 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 94 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 95 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 97 | /** |
| 98 | * Return a DOWN key event with KEYCODE_A. |
| 99 | */ |
| 100 | static KeyEvent getTestKeyEvent() { |
| 101 | KeyEvent event; |
| 102 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 103 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 104 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 105 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 106 | return event; |
| 107 | } |
| 108 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 109 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 110 | ASSERT_EQ(expectedAction, receivedAction) |
| 111 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 112 | << MotionEvent::actionToString(receivedAction); |
| 113 | } |
| 114 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 115 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 116 | bool matches = action == arg.getAction(); |
| 117 | if (!matches) { |
| 118 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 119 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 120 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 121 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 122 | if (!matches) { |
| 123 | *result_listener << "; "; |
| 124 | } |
| 125 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 126 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 127 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 128 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 129 | if (!matches) { |
| 130 | *result_listener << "; "; |
| 131 | } |
| 132 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 133 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 134 | } |
| 135 | return matches; |
| 136 | } |
| 137 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 138 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 139 | return arg.getDownTime() == downTime; |
| 140 | } |
| 141 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 142 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 143 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 144 | << inputEventSourceToString(arg.getSource()); |
| 145 | return arg.getSource() == source; |
| 146 | } |
| 147 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 148 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 149 | if (arg.getPointerCount() != 1) { |
| 150 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 151 | return false; |
| 152 | } |
| 153 | return arg.getX(0 /*pointerIndex*/) == x && arg.getY(0 /*pointerIndex*/) == y; |
| 154 | } |
| 155 | |
| 156 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 157 | // Build a map for the received pointers, by pointer id |
| 158 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 159 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 160 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 161 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 162 | } |
| 163 | return pointers == actualPointers; |
| 164 | } |
| 165 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 166 | // --- FakeInputDispatcherPolicy --- |
| 167 | |
| 168 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 169 | InputDispatcherConfiguration mConfig; |
| 170 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 171 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 172 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 173 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 174 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | |
| 176 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 177 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 178 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 179 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 180 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
| 181 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); |
| 182 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 183 | |
| 184 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 185 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 186 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 187 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 190 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 191 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
| 192 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); |
| 193 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 194 | |
| 195 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 196 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 197 | EXPECT_EQ(motionEvent.getAction(), args.action); |
| 198 | EXPECT_EQ(motionEvent.getX(0), point.x); |
| 199 | EXPECT_EQ(motionEvent.getY(0), point.y); |
| 200 | EXPECT_EQ(motionEvent.getRawX(0), point.x); |
| 201 | EXPECT_EQ(motionEvent.getRawY(0), point.y); |
| 202 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 203 | } |
| 204 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 205 | void assertFilterInputEventWasNotCalled() { |
| 206 | std::scoped_lock lock(mLock); |
| 207 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 208 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 210 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 211 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 212 | ASSERT_TRUE(mConfigurationChangedTime) |
| 213 | << "Timed out waiting for configuration changed call"; |
| 214 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 215 | mConfigurationChangedTime = std::nullopt; |
| 216 | } |
| 217 | |
| 218 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 219 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 220 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 221 | // 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] | 222 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 223 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 224 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 225 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 226 | mLastNotifySwitch = std::nullopt; |
| 227 | } |
| 228 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 229 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 230 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 231 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 232 | mOnPointerDownToken.clear(); |
| 233 | } |
| 234 | |
| 235 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 236 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 237 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 238 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 241 | // This function must be called soon after the expected ANR timer starts, |
| 242 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 243 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 244 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 245 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 246 | std::unique_lock lock(mLock); |
| 247 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 248 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 249 | ASSERT_NO_FATAL_FAILURE( |
| 250 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 251 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 254 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 255 | const sp<WindowInfoHandle>& window) { |
| 256 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 257 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 258 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 259 | } |
| 260 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 261 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 262 | const sp<IBinder>& expectedToken, |
| 263 | int32_t expectedPid) { |
| 264 | std::unique_lock lock(mLock); |
| 265 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 266 | AnrResult result; |
| 267 | ASSERT_NO_FATAL_FAILURE(result = |
| 268 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 269 | const auto& [token, pid] = result; |
| 270 | ASSERT_EQ(expectedToken, token); |
| 271 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 274 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 275 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 276 | std::unique_lock lock(mLock); |
| 277 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 278 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 279 | const auto& [token, _] = result; |
| 280 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 283 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 284 | int32_t expectedPid) { |
| 285 | std::unique_lock lock(mLock); |
| 286 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 287 | AnrResult result; |
| 288 | ASSERT_NO_FATAL_FAILURE( |
| 289 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 290 | const auto& [token, pid] = result; |
| 291 | ASSERT_EQ(expectedToken, token); |
| 292 | ASSERT_EQ(expectedPid, pid); |
| 293 | } |
| 294 | |
| 295 | /** 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] | 296 | sp<IBinder> getResponsiveWindowToken() { |
| 297 | std::unique_lock lock(mLock); |
| 298 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 299 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 300 | const auto& [token, _] = result; |
| 301 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void assertNotifyAnrWasNotCalled() { |
| 305 | std::scoped_lock lock(mLock); |
| 306 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 307 | ASSERT_TRUE(mAnrWindows.empty()); |
| 308 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 309 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 310 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 313 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 314 | mConfig.keyRepeatTimeout = timeout; |
| 315 | mConfig.keyRepeatDelay = delay; |
| 316 | } |
| 317 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 318 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 319 | std::unique_lock lock(mLock); |
| 320 | base::ScopedLockAssertion assumeLocked(mLock); |
| 321 | |
| 322 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 323 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 324 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 325 | enabled; |
| 326 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 327 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 328 | << ") to be called."; |
| 329 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 330 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 331 | auto request = *mPointerCaptureRequest; |
| 332 | mPointerCaptureRequest.reset(); |
| 333 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void assertSetPointerCaptureNotCalled() { |
| 337 | std::unique_lock lock(mLock); |
| 338 | base::ScopedLockAssertion assumeLocked(mLock); |
| 339 | |
| 340 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 341 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 342 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 343 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 344 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 345 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 346 | } |
| 347 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 348 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 349 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 350 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 351 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 352 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 353 | } |
| 354 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 355 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 356 | std::unique_lock lock(mLock); |
| 357 | base::ScopedLockAssertion assumeLocked(mLock); |
| 358 | std::optional<sp<IBinder>> receivedToken = |
| 359 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 360 | mNotifyInputChannelBroken); |
| 361 | ASSERT_TRUE(receivedToken.has_value()); |
| 362 | ASSERT_EQ(token, *receivedToken); |
| 363 | } |
| 364 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 365 | /** |
| 366 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 367 | */ |
| 368 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 369 | mInterceptKeyTimeout = timeout; |
| 370 | } |
| 371 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 372 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 373 | std::mutex mLock; |
| 374 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 375 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 376 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 377 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 378 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 379 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 380 | |
| 381 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 382 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 383 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 384 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 385 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 386 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 387 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 388 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 389 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 390 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 391 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 392 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 393 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 394 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 395 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 396 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 397 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 398 | // first entry from the container and erase it. |
| 399 | template <class T> |
| 400 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 401 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 402 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 403 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 404 | // before checking if ANR was called. |
| 405 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 406 | // to provide it some time to act. 100ms seems reasonable. |
| 407 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 408 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 409 | std::optional<T> token = |
| 410 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 411 | if (!token.has_value()) { |
| 412 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 413 | return {}; |
| 414 | } |
| 415 | |
| 416 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 417 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 418 | // the dispatcher started counting before this function was called |
| 419 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 420 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 421 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 422 | << "ms, but waited " |
| 423 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 424 | << "ms instead"; |
| 425 | } |
| 426 | return *token; |
| 427 | } |
| 428 | |
| 429 | template <class T> |
| 430 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 431 | std::queue<T>& storage, |
| 432 | std::unique_lock<std::mutex>& lock, |
| 433 | std::condition_variable& condition) |
| 434 | REQUIRES(mLock) { |
| 435 | condition.wait_for(lock, timeout, |
| 436 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 437 | if (storage.empty()) { |
| 438 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 439 | return std::nullopt; |
| 440 | } |
| 441 | T item = storage.front(); |
| 442 | storage.pop(); |
| 443 | return std::make_optional(item); |
| 444 | } |
| 445 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 446 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 447 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 448 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 451 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 452 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 453 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 454 | ASSERT_TRUE(pid.has_value()); |
| 455 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 456 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 459 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 460 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 461 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 462 | ASSERT_TRUE(pid.has_value()); |
| 463 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 464 | mNotifyAnr.notify_all(); |
| 465 | } |
| 466 | |
| 467 | void notifyNoFocusedWindowAnr( |
| 468 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 469 | std::scoped_lock lock(mLock); |
| 470 | mAnrApplications.push(applicationHandle); |
| 471 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 474 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 475 | std::scoped_lock lock(mLock); |
| 476 | mBrokenInputChannels.push(connectionToken); |
| 477 | mNotifyInputChannelBroken.notify_all(); |
| 478 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 479 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 480 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 481 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 482 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 483 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 484 | const std::vector<float>& values) override {} |
| 485 | |
| 486 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 487 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 488 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 489 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 490 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 491 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 492 | *outConfig = mConfig; |
| 493 | } |
| 494 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 495 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 496 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 497 | switch (inputEvent->getType()) { |
| 498 | case AINPUT_EVENT_TYPE_KEY: { |
| 499 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 500 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 501 | break; |
| 502 | } |
| 503 | |
| 504 | case AINPUT_EVENT_TYPE_MOTION: { |
| 505 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 506 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 507 | break; |
| 508 | } |
| 509 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 510 | return true; |
| 511 | } |
| 512 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 513 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { |
| 514 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { |
| 515 | // Clear intercept state when we handled the event. |
| 516 | mInterceptKeyTimeout = 0ms; |
| 517 | } |
| 518 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 519 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 520 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 521 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 522 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 523 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 524 | // Clear intercept state so we could dispatch the event in next wake. |
| 525 | mInterceptKeyTimeout = 0ms; |
| 526 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 529 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 533 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 534 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 535 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 536 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 537 | * essentially a passthrough for notifySwitch. |
| 538 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 539 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 542 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 543 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 544 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 545 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 546 | mOnPointerDownToken = newToken; |
| 547 | } |
| 548 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 549 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 550 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 551 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 552 | mPointerCaptureChangedCondition.notify_all(); |
| 553 | } |
| 554 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 555 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 556 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 557 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 558 | mDropTargetWindowToken = token; |
| 559 | } |
| 560 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 561 | void assertFilterInputEventWasCalledInternal( |
| 562 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 563 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 564 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 565 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 566 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 567 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | }; |
| 569 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 570 | // --- InputDispatcherTest --- |
| 571 | |
| 572 | class InputDispatcherTest : public testing::Test { |
| 573 | protected: |
| 574 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 575 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 577 | void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 578 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 579 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 580 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 581 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 582 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 585 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 586 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 587 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 588 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 589 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 590 | |
| 591 | /** |
| 592 | * Used for debugging when writing the test |
| 593 | */ |
| 594 | void dumpDispatcherState() { |
| 595 | std::string dump; |
| 596 | mDispatcher->dump(dump); |
| 597 | std::stringstream ss(dump); |
| 598 | std::string to; |
| 599 | |
| 600 | while (std::getline(ss, to, '\n')) { |
| 601 | ALOGE("%s", to.c_str()); |
| 602 | } |
| 603 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 604 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 605 | void setFocusedWindow(const sp<WindowInfoHandle>& window, |
| 606 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 607 | FocusRequest request; |
| 608 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 609 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 610 | if (focusedWindow) { |
| 611 | request.focusedToken = focusedWindow->getToken(); |
| 612 | } |
| 613 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 614 | request.displayId = window->getInfo()->displayId; |
| 615 | mDispatcher->setFocusedWindow(request); |
| 616 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | }; |
| 618 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 619 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 620 | KeyEvent event; |
| 621 | |
| 622 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 623 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 624 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 625 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 626 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 627 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 628 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 629 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | << "Should reject key events with undefined action."; |
| 631 | |
| 632 | // 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] | 633 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 634 | 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] | 635 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 636 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 637 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 638 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 639 | << "Should reject key events with ACTION_MULTIPLE."; |
| 640 | } |
| 641 | |
| 642 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 643 | MotionEvent event; |
| 644 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 645 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 646 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 647 | pointerProperties[i].clear(); |
| 648 | pointerProperties[i].id = i; |
| 649 | pointerCoords[i].clear(); |
| 650 | } |
| 651 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 652 | // Some constants commonly used below |
| 653 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 654 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 655 | constexpr int32_t metaState = AMETA_NONE; |
| 656 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 657 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 658 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 660 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 661 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 662 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 663 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 664 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 665 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 666 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 667 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 668 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 669 | << "Should reject motion events with undefined action."; |
| 670 | |
| 671 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 672 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 673 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 674 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 675 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 676 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 677 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 678 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 679 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 680 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 | << "Should reject motion events with pointer down index too large."; |
| 682 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 683 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 684 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 685 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 686 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 687 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 688 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 689 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 690 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 691 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 692 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 693 | << "Should reject motion events with pointer down index too small."; |
| 694 | |
| 695 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 696 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 697 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 698 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 699 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 700 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 701 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 702 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 703 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 704 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 705 | << "Should reject motion events with pointer up index too large."; |
| 706 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 707 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 708 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 709 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 710 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 711 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 712 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 713 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 714 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 715 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 716 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 717 | << "Should reject motion events with pointer up index too small."; |
| 718 | |
| 719 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 720 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 721 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 722 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 723 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 724 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 725 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 726 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 727 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 728 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 729 | << "Should reject motion events with 0 pointers."; |
| 730 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 731 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 732 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 733 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 734 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 735 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 736 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 737 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 738 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 739 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 740 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 741 | |
| 742 | // Rejects motion events with invalid pointer ids. |
| 743 | pointerProperties[0].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*/ 1, 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 pointer ids less than 0."; |
| 754 | |
| 755 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 756 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 757 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 758 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 759 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 760 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 761 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 762 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 763 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 764 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 766 | |
| 767 | // Rejects motion events with duplicate pointer ids. |
| 768 | pointerProperties[0].id = 1; |
| 769 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 770 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 771 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 772 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 773 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 774 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 775 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 776 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 777 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 778 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | << "Should reject motion events with duplicate pointer ids."; |
| 780 | } |
| 781 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 782 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 783 | |
| 784 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 785 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 786 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 787 | mDispatcher->notifyConfigurationChanged(&args); |
| 788 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 789 | |
| 790 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 791 | } |
| 792 | |
| 793 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 794 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 795 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 796 | mDispatcher->notifySwitch(&args); |
| 797 | |
| 798 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 799 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 800 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 801 | } |
| 802 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 803 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 804 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 805 | // Default input dispatching timeout if there is no focused application or paused window |
| 806 | // from which to determine an appropriate dispatching timeout. |
| 807 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 808 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 809 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 810 | |
| 811 | class FakeApplicationHandle : public InputApplicationHandle { |
| 812 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 813 | FakeApplicationHandle() { |
| 814 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 815 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 816 | mInfo.dispatchingTimeoutMillis = |
| 817 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 818 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 819 | virtual ~FakeApplicationHandle() {} |
| 820 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 821 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 822 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 823 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 824 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 825 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 826 | }; |
| 827 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 828 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 829 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 830 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 831 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 832 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 833 | } |
| 834 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 835 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 836 | InputEvent* event; |
| 837 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 838 | if (!consumeSeq) { |
| 839 | return nullptr; |
| 840 | } |
| 841 | finishEvent(*consumeSeq); |
| 842 | return event; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Receive an event without acknowledging it. |
| 847 | * Return the sequence number that could later be used to send finished signal. |
| 848 | */ |
| 849 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 850 | uint32_t consumeSeq; |
| 851 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 852 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 853 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 854 | status_t status = WOULD_BLOCK; |
| 855 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 856 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 857 | &event); |
| 858 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 859 | if (elapsed > 100ms) { |
| 860 | break; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | if (status == WOULD_BLOCK) { |
| 865 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 866 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | if (status != OK) { |
| 870 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 871 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 872 | } |
| 873 | if (event == nullptr) { |
| 874 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 875 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 876 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 877 | if (outEvent != nullptr) { |
| 878 | *outEvent = event; |
| 879 | } |
| 880 | return consumeSeq; |
| 881 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 882 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 883 | /** |
| 884 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 885 | */ |
| 886 | void finishEvent(uint32_t consumeSeq) { |
| 887 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 888 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 889 | } |
| 890 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 891 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 892 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 893 | ASSERT_EQ(OK, status); |
| 894 | } |
| 895 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 896 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 897 | std::optional<int32_t> expectedDisplayId, |
| 898 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 899 | InputEvent* event = consume(); |
| 900 | |
| 901 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 902 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 903 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 904 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 905 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 906 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 907 | if (expectedDisplayId.has_value()) { |
| 908 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 909 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 910 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 911 | switch (expectedEventType) { |
| 912 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 913 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 914 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 915 | if (expectedFlags.has_value()) { |
| 916 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 917 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 918 | break; |
| 919 | } |
| 920 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 921 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 922 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 923 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 924 | if (expectedFlags.has_value()) { |
| 925 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 926 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 927 | break; |
| 928 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 929 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 930 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 931 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 932 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 933 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 934 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 935 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { |
| 936 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 937 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 938 | case AINPUT_EVENT_TYPE_DRAG: { |
| 939 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 940 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 941 | default: { |
| 942 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 943 | } |
| 944 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 945 | } |
| 946 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 947 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 948 | InputEvent* event = consume(); |
| 949 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 950 | << ": consumer should have returned non-NULL event."; |
| 951 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 952 | << "Got " << inputEventTypeToString(event->getType()) |
| 953 | << " event instead of FOCUS event"; |
| 954 | |
| 955 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 956 | << mName.c_str() << ": event displayId should always be NONE."; |
| 957 | |
| 958 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 959 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 960 | } |
| 961 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 962 | void consumeCaptureEvent(bool hasCapture) { |
| 963 | const InputEvent* event = consume(); |
| 964 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 965 | << ": consumer should have returned non-NULL event."; |
| 966 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 967 | << "Got " << inputEventTypeToString(event->getType()) |
| 968 | << " event instead of CAPTURE event"; |
| 969 | |
| 970 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 971 | << mName.c_str() << ": event displayId should always be NONE."; |
| 972 | |
| 973 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 974 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 975 | } |
| 976 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 977 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 978 | const InputEvent* event = consume(); |
| 979 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 980 | << ": consumer should have returned non-NULL event."; |
| 981 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 982 | << "Got " << inputEventTypeToString(event->getType()) |
| 983 | << " event instead of DRAG event"; |
| 984 | |
| 985 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 986 | << mName.c_str() << ": event displayId should always be NONE."; |
| 987 | |
| 988 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 989 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 990 | EXPECT_EQ(x, dragEvent.getX()); |
| 991 | EXPECT_EQ(y, dragEvent.getY()); |
| 992 | } |
| 993 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 994 | void consumeTouchModeEvent(bool inTouchMode) { |
| 995 | const InputEvent* event = consume(); |
| 996 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 997 | << ": consumer should have returned non-NULL event."; |
| 998 | ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType()) |
| 999 | << "Got " << inputEventTypeToString(event->getType()) |
| 1000 | << " event instead of TOUCH_MODE event"; |
| 1001 | |
| 1002 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1003 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1004 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1005 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1006 | } |
| 1007 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1008 | void assertNoEvents() { |
| 1009 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1010 | if (event == nullptr) { |
| 1011 | return; |
| 1012 | } |
| 1013 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 1014 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1015 | ADD_FAILURE() << "Received key event " |
| 1016 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 1017 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 1018 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1019 | ADD_FAILURE() << "Received motion event " |
| 1020 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 1021 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 1022 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1023 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1024 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1025 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 1026 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1027 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1028 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1029 | } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) { |
| 1030 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1031 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1032 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1033 | } |
| 1034 | FAIL() << mName.c_str() |
| 1035 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1039 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1040 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1041 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1042 | protected: |
| 1043 | std::unique_ptr<InputConsumer> mConsumer; |
| 1044 | PreallocatedInputEventFactory mEventFactory; |
| 1045 | |
| 1046 | std::string mName; |
| 1047 | }; |
| 1048 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1049 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1050 | public: |
| 1051 | static const int32_t WIDTH = 600; |
| 1052 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1053 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1054 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1055 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1056 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1057 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1058 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1059 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1060 | dispatcher->createInputChannel(name); |
| 1061 | token = (*channel)->getConnectionToken(); |
| 1062 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | inputApplicationHandle->updateInfo(); |
| 1066 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1067 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1068 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1069 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1070 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1071 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1072 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1073 | mInfo.frameLeft = 0; |
| 1074 | mInfo.frameTop = 0; |
| 1075 | mInfo.frameRight = WIDTH; |
| 1076 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1077 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1078 | mInfo.globalScaleFactor = 1.0; |
| 1079 | mInfo.touchableRegion.clear(); |
| 1080 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1081 | mInfo.ownerPid = WINDOW_PID; |
| 1082 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1083 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1084 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1087 | sp<FakeWindowHandle> clone( |
| 1088 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1089 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1090 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1091 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1092 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1093 | return handle; |
| 1094 | } |
| 1095 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1096 | void setTouchable(bool touchable) { |
| 1097 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1098 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1099 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1100 | void setFocusable(bool focusable) { |
| 1101 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1102 | } |
| 1103 | |
| 1104 | void setVisible(bool visible) { |
| 1105 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1106 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1107 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1108 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1109 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1112 | void setPaused(bool paused) { |
| 1113 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1114 | } |
| 1115 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1116 | void setPreventSplitting(bool preventSplitting) { |
| 1117 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | void setSlippery(bool slippery) { |
| 1121 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1122 | } |
| 1123 | |
| 1124 | void setWatchOutsideTouch(bool watchOutside) { |
| 1125 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1126 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1127 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1128 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1129 | |
| 1130 | void setInterceptsStylus(bool interceptsStylus) { |
| 1131 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1132 | } |
| 1133 | |
| 1134 | void setDropInput(bool dropInput) { |
| 1135 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1136 | } |
| 1137 | |
| 1138 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1139 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1140 | } |
| 1141 | |
| 1142 | void setNoInputChannel(bool noInputChannel) { |
| 1143 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1144 | } |
| 1145 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1146 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1147 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1148 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1149 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1150 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1151 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1152 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1153 | mInfo.frameLeft = frame.left; |
| 1154 | mInfo.frameTop = frame.top; |
| 1155 | mInfo.frameRight = frame.right; |
| 1156 | mInfo.frameBottom = frame.bottom; |
| 1157 | mInfo.touchableRegion.clear(); |
| 1158 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1159 | |
| 1160 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1161 | ui::Transform translate; |
| 1162 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1163 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1166 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1167 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1168 | void setIsWallpaper(bool isWallpaper) { |
| 1169 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1170 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1171 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1172 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1173 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1174 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1175 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1176 | void setTrustedOverlay(bool trustedOverlay) { |
| 1177 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1178 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1179 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1180 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1181 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1182 | } |
| 1183 | |
| 1184 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1185 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1186 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1187 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1188 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1189 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1190 | expectedFlags); |
| 1191 | } |
| 1192 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1193 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1194 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 1195 | } |
| 1196 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1197 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1198 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1199 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 1200 | expectedFlags); |
| 1201 | } |
| 1202 | |
| 1203 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1204 | int32_t expectedFlags = 0) { |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1205 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 1206 | expectedFlags); |
| 1207 | } |
| 1208 | |
| 1209 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1210 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1211 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1212 | } |
| 1213 | |
| 1214 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1215 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1216 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1217 | expectedFlags); |
| 1218 | } |
| 1219 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1220 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1221 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1222 | int32_t expectedFlags = 0) { |
| 1223 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1224 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1225 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1226 | } |
| 1227 | |
| 1228 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1229 | int32_t expectedFlags = 0) { |
| 1230 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1231 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1232 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1233 | } |
| 1234 | |
| 1235 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1236 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1237 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1238 | expectedFlags); |
| 1239 | } |
| 1240 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1241 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1242 | int32_t expectedFlags = 0) { |
| 1243 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1244 | expectedFlags); |
| 1245 | } |
| 1246 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1247 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1248 | int32_t expectedFlags = 0) { |
| 1249 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1250 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1251 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 1252 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1253 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1254 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1255 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1256 | } |
| 1257 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1258 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1259 | ASSERT_NE(mInputReceiver, nullptr) |
| 1260 | << "Cannot consume events from a window with no receiver"; |
| 1261 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1262 | } |
| 1263 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1264 | void consumeCaptureEvent(bool hasCapture) { |
| 1265 | ASSERT_NE(mInputReceiver, nullptr) |
| 1266 | << "Cannot consume events from a window with no receiver"; |
| 1267 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1268 | } |
| 1269 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1270 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1271 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1272 | 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] | 1273 | ASSERT_THAT(*motionEvent, matcher); |
| 1274 | } |
| 1275 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1276 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1277 | std::optional<int32_t> expectedDisplayId, |
| 1278 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1279 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1280 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1281 | expectedFlags); |
| 1282 | } |
| 1283 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1284 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1285 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1286 | } |
| 1287 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1288 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1289 | ASSERT_NE(mInputReceiver, nullptr) |
| 1290 | << "Cannot consume events from a window with no receiver"; |
| 1291 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1292 | } |
| 1293 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1294 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1295 | if (mInputReceiver == nullptr) { |
| 1296 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1297 | return std::nullopt; |
| 1298 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1299 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | void finishEvent(uint32_t sequenceNum) { |
| 1303 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1304 | mInputReceiver->finishEvent(sequenceNum); |
| 1305 | } |
| 1306 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1307 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1308 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1309 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1310 | } |
| 1311 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1312 | InputEvent* consume() { |
| 1313 | if (mInputReceiver == nullptr) { |
| 1314 | return nullptr; |
| 1315 | } |
| 1316 | return mInputReceiver->consume(); |
| 1317 | } |
| 1318 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1319 | MotionEvent* consumeMotion() { |
| 1320 | InputEvent* event = consume(); |
| 1321 | if (event == nullptr) { |
| 1322 | ADD_FAILURE() << "Consume failed : no event"; |
| 1323 | return nullptr; |
| 1324 | } |
| 1325 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 1326 | ADD_FAILURE() << "Instead of motion event, got " |
| 1327 | << inputEventTypeToString(event->getType()); |
| 1328 | return nullptr; |
| 1329 | } |
| 1330 | return static_cast<MotionEvent*>(event); |
| 1331 | } |
| 1332 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1333 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1334 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1335 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1336 | return; // Can't receive events if the window does not have input channel |
| 1337 | } |
| 1338 | ASSERT_NE(nullptr, mInputReceiver) |
| 1339 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1340 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1341 | } |
| 1342 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1343 | sp<IBinder> getToken() { return mInfo.token; } |
| 1344 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1345 | const std::string& getName() { return mName; } |
| 1346 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1347 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1348 | mInfo.ownerPid = ownerPid; |
| 1349 | mInfo.ownerUid = ownerUid; |
| 1350 | } |
| 1351 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1352 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1353 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1354 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1355 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1356 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1357 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1358 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1359 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1360 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1361 | 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] | 1362 | }; |
| 1363 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1364 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1365 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1366 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1367 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1368 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1369 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1370 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1371 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1372 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1373 | KeyEvent event; |
| 1374 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1375 | |
| 1376 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1377 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1378 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 1379 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1380 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1381 | if (!allowKeyRepeat) { |
| 1382 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1383 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1384 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1385 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1386 | } |
| 1387 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1388 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1389 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1390 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 1391 | } |
| 1392 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1393 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1394 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1395 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1396 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1397 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1398 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, |
| 1399 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
| 1400 | /* allowKeyRepeat */ false); |
| 1401 | } |
| 1402 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1403 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1404 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1405 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 1406 | } |
| 1407 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1408 | class PointerBuilder { |
| 1409 | public: |
| 1410 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1411 | mProperties.clear(); |
| 1412 | mProperties.id = id; |
| 1413 | mProperties.toolType = toolType; |
| 1414 | mCoords.clear(); |
| 1415 | } |
| 1416 | |
| 1417 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1418 | |
| 1419 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1420 | |
| 1421 | PointerBuilder& axis(int32_t axis, float value) { |
| 1422 | mCoords.setAxisValue(axis, value); |
| 1423 | return *this; |
| 1424 | } |
| 1425 | |
| 1426 | PointerProperties buildProperties() const { return mProperties; } |
| 1427 | |
| 1428 | PointerCoords buildCoords() const { return mCoords; } |
| 1429 | |
| 1430 | private: |
| 1431 | PointerProperties mProperties; |
| 1432 | PointerCoords mCoords; |
| 1433 | }; |
| 1434 | |
| 1435 | class MotionEventBuilder { |
| 1436 | public: |
| 1437 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1438 | mAction = action; |
| 1439 | mSource = source; |
| 1440 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1441 | } |
| 1442 | |
| 1443 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1444 | mEventTime = eventTime; |
| 1445 | return *this; |
| 1446 | } |
| 1447 | |
| 1448 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1449 | mDisplayId = displayId; |
| 1450 | return *this; |
| 1451 | } |
| 1452 | |
| 1453 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1454 | mActionButton = actionButton; |
| 1455 | return *this; |
| 1456 | } |
| 1457 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1458 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1459 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1460 | return *this; |
| 1461 | } |
| 1462 | |
| 1463 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1464 | mRawXCursorPosition = rawXCursorPosition; |
| 1465 | return *this; |
| 1466 | } |
| 1467 | |
| 1468 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1469 | mRawYCursorPosition = rawYCursorPosition; |
| 1470 | return *this; |
| 1471 | } |
| 1472 | |
| 1473 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1474 | mPointers.push_back(pointer); |
| 1475 | return *this; |
| 1476 | } |
| 1477 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1478 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1479 | mFlags |= flags; |
| 1480 | return *this; |
| 1481 | } |
| 1482 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1483 | MotionEvent build() { |
| 1484 | std::vector<PointerProperties> pointerProperties; |
| 1485 | std::vector<PointerCoords> pointerCoords; |
| 1486 | for (const PointerBuilder& pointer : mPointers) { |
| 1487 | pointerProperties.push_back(pointer.buildProperties()); |
| 1488 | pointerCoords.push_back(pointer.buildCoords()); |
| 1489 | } |
| 1490 | |
| 1491 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1492 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1493 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1494 | mPointers.size() == 1) { |
| 1495 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1496 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1497 | } |
| 1498 | |
| 1499 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1500 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1501 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1502 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1503 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1504 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1505 | mRawYCursorPosition, identityTransform, mEventTime, mEventTime, |
| 1506 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1507 | |
| 1508 | return event; |
| 1509 | } |
| 1510 | |
| 1511 | private: |
| 1512 | int32_t mAction; |
| 1513 | int32_t mSource; |
| 1514 | nsecs_t mEventTime; |
| 1515 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1516 | int32_t mActionButton{0}; |
| 1517 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1518 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1519 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1520 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1521 | |
| 1522 | std::vector<PointerBuilder> mPointers; |
| 1523 | }; |
| 1524 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1525 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1526 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1527 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1528 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1529 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1530 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1531 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1534 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1535 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1536 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1537 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1538 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1539 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1540 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1541 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1542 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1543 | MotionEvent event = MotionEventBuilder(action, source) |
| 1544 | .displayId(displayId) |
| 1545 | .eventTime(eventTime) |
| 1546 | .rawXCursorPosition(cursorPosition.x) |
| 1547 | .rawYCursorPosition(cursorPosition.y) |
| 1548 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1549 | .x(position.x) |
| 1550 | .y(position.y)) |
| 1551 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1552 | |
| 1553 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1554 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, |
| 1555 | policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1558 | static InputEventInjectionResult injectMotionDown( |
| 1559 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1560 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1561 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1564 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1565 | int32_t source, int32_t displayId, |
| 1566 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1567 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1570 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1571 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1572 | // Define a valid key event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1573 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1574 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1575 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1576 | |
| 1577 | return args; |
| 1578 | } |
| 1579 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1580 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1581 | const std::vector<PointF>& points) { |
| 1582 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1583 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1584 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1585 | } |
| 1586 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1587 | PointerProperties pointerProperties[pointerCount]; |
| 1588 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1589 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1590 | for (size_t i = 0; i < pointerCount; i++) { |
| 1591 | pointerProperties[i].clear(); |
| 1592 | pointerProperties[i].id = i; |
| 1593 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1594 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1595 | pointerCoords[i].clear(); |
| 1596 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1597 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1598 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1599 | |
| 1600 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1601 | // Define a valid motion event. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1602 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1603 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1604 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1605 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1606 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1607 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1608 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1609 | |
| 1610 | return args; |
| 1611 | } |
| 1612 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1613 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1614 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1615 | } |
| 1616 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1617 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1618 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1619 | } |
| 1620 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1621 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1622 | const PointerCaptureRequest& request) { |
| 1623 | return NotifyPointerCaptureChangedArgs(/* id */ 0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1626 | /** |
| 1627 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1628 | * broken channel. |
| 1629 | */ |
| 1630 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1631 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1632 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1633 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1634 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1635 | |
| 1636 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1637 | |
| 1638 | // Window closes its channel, but the window remains. |
| 1639 | window->destroyReceiver(); |
| 1640 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1641 | } |
| 1642 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1643 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1644 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1645 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1646 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1647 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1648 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1650 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1651 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1652 | |
| 1653 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1654 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1655 | } |
| 1656 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1657 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1658 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1659 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1660 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1661 | |
| 1662 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1663 | // Inject a MotionEvent to an unknown display. |
| 1664 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1665 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1666 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1667 | |
| 1668 | // Window should receive motion event. |
| 1669 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1670 | } |
| 1671 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1672 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1673 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1674 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1675 | * called twice. |
| 1676 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1677 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1678 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1679 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1680 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1681 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1682 | |
| 1683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1685 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1686 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1687 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1688 | |
| 1689 | // Window should receive motion event. |
| 1690 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1691 | } |
| 1692 | |
| 1693 | /** |
| 1694 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1695 | */ |
| 1696 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1697 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1698 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1699 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1700 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1701 | |
| 1702 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1703 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1705 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1706 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1707 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1708 | |
| 1709 | // Window should receive motion event. |
| 1710 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1711 | } |
| 1712 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1713 | // The foreground window should receive the first touch down event. |
| 1714 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1715 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1716 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1717 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1718 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1719 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1720 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1721 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1722 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1723 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1724 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1725 | |
| 1726 | // 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] | 1727 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1728 | windowSecond->assertNoEvents(); |
| 1729 | } |
| 1730 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1731 | /** |
| 1732 | * Two windows: A top window, and a wallpaper behind the window. |
| 1733 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1734 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1735 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1736 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1737 | */ |
| 1738 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1740 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1741 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1742 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1743 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1744 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1745 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1746 | |
| 1747 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1748 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1749 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1750 | {100, 200})) |
| 1751 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1752 | |
| 1753 | // Both foreground window and its wallpaper should receive the touch down |
| 1754 | foregroundWindow->consumeMotionDown(); |
| 1755 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1756 | |
| 1757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1758 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1759 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1760 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1761 | |
| 1762 | foregroundWindow->consumeMotionMove(); |
| 1763 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1764 | |
| 1765 | // Now the foreground window goes away, but the wallpaper stays |
| 1766 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1767 | foregroundWindow->consumeMotionCancel(); |
| 1768 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1769 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1770 | } |
| 1771 | |
| 1772 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1773 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1774 | * with the following differences: |
| 1775 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1776 | * clean up the connection. |
| 1777 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1778 | * Ensure that there's no crash in the dispatcher. |
| 1779 | */ |
| 1780 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1781 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1782 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1783 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1784 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1785 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1786 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1787 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1788 | |
| 1789 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1790 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1791 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1792 | {100, 200})) |
| 1793 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1794 | |
| 1795 | // Both foreground window and its wallpaper should receive the touch down |
| 1796 | foregroundWindow->consumeMotionDown(); |
| 1797 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1798 | |
| 1799 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1800 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1801 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1802 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1803 | |
| 1804 | foregroundWindow->consumeMotionMove(); |
| 1805 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1806 | |
| 1807 | // Wallpaper closes its channel, but the window remains. |
| 1808 | wallpaperWindow->destroyReceiver(); |
| 1809 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1810 | |
| 1811 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1812 | // is no longer valid. |
| 1813 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1814 | foregroundWindow->consumeMotionCancel(); |
| 1815 | } |
| 1816 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1817 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1818 | public ::testing::WithParamInterface<bool> {}; |
| 1819 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1820 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1821 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1822 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1823 | * The top window gets a multitouch gesture. |
| 1824 | * Ensure that wallpaper gets the same gesture. |
| 1825 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1826 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1827 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1828 | sp<FakeWindowHandle> foregroundWindow = |
| 1829 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1830 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1831 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1832 | |
| 1833 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1834 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1835 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1836 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1837 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Touch down on top window |
| 1840 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1841 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1842 | {100, 100})) |
| 1843 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1844 | |
| 1845 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1846 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1847 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1848 | |
| 1849 | // Second finger down on the top window |
| 1850 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1851 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1852 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1853 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1854 | .x(100) |
| 1855 | .y(100)) |
| 1856 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1857 | .x(150) |
| 1858 | .y(150)) |
| 1859 | .build(); |
| 1860 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1861 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1862 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1863 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1864 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1865 | foregroundWindow->consumeMotionPointerDown(1 /* pointerIndex */); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1866 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1867 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1868 | |
| 1869 | const MotionEvent secondFingerUpEvent = |
| 1870 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1871 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1872 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1873 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1874 | .x(100) |
| 1875 | .y(100)) |
| 1876 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1877 | .x(150) |
| 1878 | .y(150)) |
| 1879 | .build(); |
| 1880 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1881 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1882 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1883 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1884 | foregroundWindow->consumeMotionPointerUp(0); |
| 1885 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1886 | |
| 1887 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1888 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1889 | {100, 100})) |
| 1890 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1891 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1892 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /** |
| 1896 | * Two windows: a window on the left and window on the right. |
| 1897 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1898 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1899 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1900 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1901 | * ACTION_POINTER_DOWN(1). |
| 1902 | */ |
| 1903 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1904 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1905 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1906 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1907 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1908 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1909 | |
| 1910 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1911 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1912 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1913 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1914 | |
| 1915 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1916 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1917 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1918 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1919 | |
| 1920 | mDispatcher->setInputWindows( |
| 1921 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1922 | |
| 1923 | // Touch down on left window |
| 1924 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1925 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1926 | {100, 100})) |
| 1927 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1928 | |
| 1929 | // Both foreground window and its wallpaper should receive the touch down |
| 1930 | leftWindow->consumeMotionDown(); |
| 1931 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1932 | |
| 1933 | // Second finger down on the right window |
| 1934 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1935 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1936 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1937 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1938 | .x(100) |
| 1939 | .y(100)) |
| 1940 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1941 | .x(300) |
| 1942 | .y(100)) |
| 1943 | .build(); |
| 1944 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1945 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1946 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1947 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1948 | |
| 1949 | leftWindow->consumeMotionMove(); |
| 1950 | // Since the touch is split, right window gets ACTION_DOWN |
| 1951 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1952 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1953 | expectedWallpaperFlags); |
| 1954 | |
| 1955 | // Now, leftWindow, which received the first finger, disappears. |
| 1956 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1957 | leftWindow->consumeMotionCancel(); |
| 1958 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1959 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1960 | |
| 1961 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1962 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1963 | const MotionEvent secondFingerMoveEvent = |
| 1964 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1965 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1966 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1967 | .x(100) |
| 1968 | .y(100)) |
| 1969 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1970 | .x(310) |
| 1971 | .y(110)) |
| 1972 | .build(); |
| 1973 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1974 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 1975 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1976 | rightWindow->consumeMotionMove(); |
| 1977 | |
| 1978 | leftWindow->assertNoEvents(); |
| 1979 | rightWindow->assertNoEvents(); |
| 1980 | wallpaperWindow->assertNoEvents(); |
| 1981 | } |
| 1982 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1983 | /** |
| 1984 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 1985 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 1986 | * The right window should receive ACTION_DOWN. |
| 1987 | */ |
| 1988 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1989 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1990 | sp<FakeWindowHandle> leftWindow = |
| 1991 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1992 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1993 | leftWindow->setDupTouchToWallpaper(true); |
| 1994 | leftWindow->setSlippery(true); |
| 1995 | |
| 1996 | sp<FakeWindowHandle> rightWindow = |
| 1997 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1998 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1999 | |
| 2000 | sp<FakeWindowHandle> wallpaperWindow = |
| 2001 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2002 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2003 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2004 | mDispatcher->setInputWindows( |
| 2005 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2006 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2007 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2008 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2009 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2010 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2011 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2012 | |
| 2013 | // Both foreground window and its wallpaper should receive the touch down |
| 2014 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2015 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2016 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2017 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2018 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2019 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2020 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2021 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2022 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2023 | leftWindow->consumeMotionCancel(); |
| 2024 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2025 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2028 | /** |
| 2029 | * On the display, have a single window, and also an area where there's no window. |
| 2030 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2031 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2032 | */ |
| 2033 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2034 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2035 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2036 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2037 | |
| 2038 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 2039 | NotifyMotionArgs args; |
| 2040 | |
| 2041 | // Touch down on the empty space |
| 2042 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 2043 | |
| 2044 | mDispatcher->waitForIdle(); |
| 2045 | window->assertNoEvents(); |
| 2046 | |
| 2047 | // Now touch down on the window with another pointer |
| 2048 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 2049 | mDispatcher->waitForIdle(); |
| 2050 | window->consumeMotionDown(); |
| 2051 | } |
| 2052 | |
| 2053 | /** |
| 2054 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2055 | * non-touchable window. |
| 2056 | */ |
| 2057 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2058 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2059 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2060 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2061 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2062 | window1->setTouchable(false); |
| 2063 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2064 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2065 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2066 | |
| 2067 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2068 | |
| 2069 | NotifyMotionArgs args; |
| 2070 | // Touch down on the non-touchable window |
| 2071 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2072 | |
| 2073 | mDispatcher->waitForIdle(); |
| 2074 | window1->assertNoEvents(); |
| 2075 | window2->assertNoEvents(); |
| 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 | window2->consumeMotionDown(); |
| 2081 | } |
| 2082 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2083 | /** |
| 2084 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2085 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2086 | */ |
| 2087 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2088 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2089 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2090 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2091 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2092 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2093 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2094 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2095 | |
| 2096 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2097 | |
| 2098 | NotifyMotionArgs args; |
| 2099 | // Touch down on the first window |
| 2100 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2101 | |
| 2102 | mDispatcher->waitForIdle(); |
| 2103 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2104 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2105 | window2->assertNoEvents(); |
| 2106 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2107 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2108 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2109 | |
| 2110 | // Now touch down on the window with another pointer |
| 2111 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2112 | mDispatcher->waitForIdle(); |
| 2113 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2114 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2115 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2116 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2117 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2118 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2119 | |
| 2120 | // Now move the pointer on the second window |
| 2121 | mDispatcher->notifyMotion( |
| 2122 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); |
| 2123 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2124 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2125 | |
| 2126 | // Now add new touch down on the second window |
| 2127 | mDispatcher->notifyMotion( |
| 2128 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); |
| 2129 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2130 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2131 | |
| 2132 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2133 | window1->consumeMotionMove(); |
| 2134 | window1->assertNoEvents(); |
| 2135 | |
| 2136 | // Now move the pointer on the first window |
| 2137 | mDispatcher->notifyMotion( |
| 2138 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}}))); |
| 2139 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2140 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2141 | |
| 2142 | mDispatcher->notifyMotion(&( |
| 2143 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); |
| 2144 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2145 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2148 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2149 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2150 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2151 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2152 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2153 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2154 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2155 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2156 | |
| 2157 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2158 | |
| 2159 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 2160 | |
| 2161 | // 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] | 2162 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2163 | injectMotionEvent(mDispatcher, |
| 2164 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2165 | AINPUT_SOURCE_MOUSE) |
| 2166 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2167 | .x(900) |
| 2168 | .y(400)) |
| 2169 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2170 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2171 | |
| 2172 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2173 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2174 | injectMotionEvent(mDispatcher, |
| 2175 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2176 | AINPUT_SOURCE_MOUSE) |
| 2177 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2178 | .x(300) |
| 2179 | .y(400)) |
| 2180 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2181 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2182 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2183 | |
| 2184 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2185 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2186 | injectMotionEvent(mDispatcher, |
| 2187 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2188 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2189 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2190 | .x(300) |
| 2191 | .y(400)) |
| 2192 | .build())); |
| 2193 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2194 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2195 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2196 | injectMotionEvent(mDispatcher, |
| 2197 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2198 | AINPUT_SOURCE_MOUSE) |
| 2199 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2200 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2201 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2202 | .x(300) |
| 2203 | .y(400)) |
| 2204 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2205 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2206 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2207 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2208 | injectMotionEvent(mDispatcher, |
| 2209 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2210 | AINPUT_SOURCE_MOUSE) |
| 2211 | .buttonState(0) |
| 2212 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2213 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2214 | .x(300) |
| 2215 | .y(400)) |
| 2216 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2217 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2218 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2220 | injectMotionEvent(mDispatcher, |
| 2221 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2222 | .buttonState(0) |
| 2223 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2224 | .x(300) |
| 2225 | .y(400)) |
| 2226 | .build())); |
| 2227 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2228 | |
| 2229 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2230 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2231 | injectMotionEvent(mDispatcher, |
| 2232 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2233 | AINPUT_SOURCE_MOUSE) |
| 2234 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2235 | .x(900) |
| 2236 | .y(400)) |
| 2237 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2238 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2239 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2240 | |
| 2241 | // No more events |
| 2242 | windowLeft->assertNoEvents(); |
| 2243 | windowRight->assertNoEvents(); |
| 2244 | } |
| 2245 | |
| 2246 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 2247 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2248 | |
| 2249 | sp<FakeWindowHandle> spyWindow = |
| 2250 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2251 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2252 | spyWindow->setTrustedOverlay(true); |
| 2253 | spyWindow->setSpy(true); |
| 2254 | sp<FakeWindowHandle> window = |
| 2255 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2256 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2257 | |
| 2258 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2259 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2260 | |
| 2261 | // Send mouse cursor to the window |
| 2262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 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(100) |
| 2268 | .y(100)) |
| 2269 | .build())); |
| 2270 | |
| 2271 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2272 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2273 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2274 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2275 | |
| 2276 | window->assertNoEvents(); |
| 2277 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 2281 | // directly in this test. |
| 2282 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2283 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2284 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2285 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2286 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2287 | |
| 2288 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2289 | |
| 2290 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2291 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2292 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2293 | injectMotionEvent(mDispatcher, |
| 2294 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2295 | AINPUT_SOURCE_MOUSE) |
| 2296 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2297 | .x(300) |
| 2298 | .y(400)) |
| 2299 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2300 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2301 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2302 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2303 | injectMotionEvent(mDispatcher, |
| 2304 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2305 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2306 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2307 | .x(300) |
| 2308 | .y(400)) |
| 2309 | .build())); |
| 2310 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2311 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2312 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2313 | injectMotionEvent(mDispatcher, |
| 2314 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2315 | AINPUT_SOURCE_MOUSE) |
| 2316 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2317 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2318 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2319 | .x(300) |
| 2320 | .y(400)) |
| 2321 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2322 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2323 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2325 | injectMotionEvent(mDispatcher, |
| 2326 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2327 | AINPUT_SOURCE_MOUSE) |
| 2328 | .buttonState(0) |
| 2329 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2330 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2331 | .x(300) |
| 2332 | .y(400)) |
| 2333 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2334 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2335 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2336 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2337 | injectMotionEvent(mDispatcher, |
| 2338 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2339 | .buttonState(0) |
| 2340 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2341 | .x(300) |
| 2342 | .y(400)) |
| 2343 | .build())); |
| 2344 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2345 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2346 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2347 | injectMotionEvent(mDispatcher, |
| 2348 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2349 | AINPUT_SOURCE_MOUSE) |
| 2350 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2351 | .x(300) |
| 2352 | .y(400)) |
| 2353 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2354 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2355 | } |
| 2356 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2357 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2358 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 2359 | * is generated. |
| 2360 | */ |
| 2361 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 2362 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2363 | sp<FakeWindowHandle> window = |
| 2364 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2365 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 2366 | |
| 2367 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2368 | |
| 2369 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2370 | |
| 2371 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2372 | injectMotionEvent(mDispatcher, |
| 2373 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2374 | AINPUT_SOURCE_MOUSE) |
| 2375 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2376 | .x(300) |
| 2377 | .y(400)) |
| 2378 | .build())); |
| 2379 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2380 | |
| 2381 | // Remove the window, but keep the channel. |
| 2382 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 2383 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2384 | } |
| 2385 | |
| 2386 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2387 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2388 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 2389 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2390 | */ |
| 2391 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 2392 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2393 | sp<FakeWindowHandle> window = |
| 2394 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2395 | window->setFrame(Rect(0, 0, 100, 100)); |
| 2396 | |
| 2397 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2398 | |
| 2399 | // Inject a hover_move from mouse. |
| 2400 | NotifyMotionArgs motionArgs = |
| 2401 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 2402 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2403 | motionArgs.xCursorPosition = 50; |
| 2404 | motionArgs.yCursorPosition = 50; |
| 2405 | mDispatcher->notifyMotion(&motionArgs); |
| 2406 | ASSERT_NO_FATAL_FAILURE( |
| 2407 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2408 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2409 | |
| 2410 | // Tap on the window |
| 2411 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2412 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2413 | mDispatcher->notifyMotion(&motionArgs); |
| 2414 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2415 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2416 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 2417 | |
| 2418 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2419 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2420 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2421 | |
| 2422 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2423 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2424 | mDispatcher->notifyMotion(&motionArgs); |
| 2425 | ASSERT_NO_FATAL_FAILURE( |
| 2426 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2427 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2428 | } |
| 2429 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2430 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 2431 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2432 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 2433 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 2434 | ADISPLAY_ID_DEFAULT); |
| 2435 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2436 | sp<FakeWindowHandle> windowSecondDisplay = |
| 2437 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 2438 | SECOND_DISPLAY_ID); |
| 2439 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2440 | |
| 2441 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2442 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2443 | |
| 2444 | // Set cursor position in window in default display and check that hover enter and move |
| 2445 | // events are generated. |
| 2446 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2447 | injectMotionEvent(mDispatcher, |
| 2448 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2449 | AINPUT_SOURCE_MOUSE) |
| 2450 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2451 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2452 | .x(300) |
| 2453 | .y(600)) |
| 2454 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2455 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2456 | |
| 2457 | // Remove all windows in secondary display and check that no event happens on window in |
| 2458 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2459 | mDispatcher->setInputWindows( |
| 2460 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2461 | windowDefaultDisplay->assertNoEvents(); |
| 2462 | |
| 2463 | // Move cursor position in window in default display and check that only hover move |
| 2464 | // event is generated and not hover enter event. |
| 2465 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2466 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2467 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2468 | injectMotionEvent(mDispatcher, |
| 2469 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2470 | AINPUT_SOURCE_MOUSE) |
| 2471 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2472 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2473 | .x(400) |
| 2474 | .y(700)) |
| 2475 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2476 | windowDefaultDisplay->consumeMotionEvent( |
| 2477 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2478 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2479 | windowDefaultDisplay->assertNoEvents(); |
| 2480 | } |
| 2481 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2482 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2483 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2484 | |
| 2485 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2486 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2487 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2488 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2489 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2490 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2491 | |
| 2492 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2493 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2494 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2495 | |
| 2496 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 2497 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2499 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2500 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2501 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2502 | windowRight->assertNoEvents(); |
| 2503 | } |
| 2504 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2505 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2506 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2507 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2508 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2509 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2510 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2511 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2512 | setFocusedWindow(window); |
| 2513 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2514 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2515 | |
| 2516 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2517 | mDispatcher->notifyKey(&keyArgs); |
| 2518 | |
| 2519 | // Window should receive key down event. |
| 2520 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2521 | |
| 2522 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 2523 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2524 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2525 | mDispatcher->notifyDeviceReset(&args); |
| 2526 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2527 | AKEY_EVENT_FLAG_CANCELED); |
| 2528 | } |
| 2529 | |
| 2530 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2531 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2532 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2533 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2534 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2536 | |
| 2537 | NotifyMotionArgs motionArgs = |
| 2538 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2539 | ADISPLAY_ID_DEFAULT); |
| 2540 | mDispatcher->notifyMotion(&motionArgs); |
| 2541 | |
| 2542 | // Window should receive motion down event. |
| 2543 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2544 | |
| 2545 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 2546 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2547 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2548 | mDispatcher->notifyDeviceReset(&args); |
| 2549 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 2550 | 0 /*expectedFlags*/); |
| 2551 | } |
| 2552 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2553 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 2554 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2555 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2556 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2557 | window->setFocusable(true); |
| 2558 | |
| 2559 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2560 | setFocusedWindow(window); |
| 2561 | |
| 2562 | window->consumeFocusEvent(true); |
| 2563 | |
| 2564 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2565 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 2566 | const nsecs_t injectTime = keyArgs.eventTime; |
| 2567 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 2568 | mDispatcher->notifyKey(&keyArgs); |
| 2569 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 2570 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2571 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 2572 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 2573 | } |
| 2574 | |
| 2575 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 2576 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2577 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2578 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2579 | window->setFocusable(true); |
| 2580 | |
| 2581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2582 | setFocusedWindow(window); |
| 2583 | |
| 2584 | window->consumeFocusEvent(true); |
| 2585 | |
| 2586 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2587 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2588 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 2589 | mDispatcher->notifyKey(&keyDown); |
| 2590 | mDispatcher->notifyKey(&keyUp); |
| 2591 | |
| 2592 | // Window should receive key event immediately when same key up. |
| 2593 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2594 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2595 | } |
| 2596 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2597 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 2598 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 2599 | * WATCH_OUTSIDE_TOUCH. |
| 2600 | * Both windows are owned by the same UID. |
| 2601 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 2602 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 2603 | */ |
| 2604 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 2605 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2606 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2607 | "First Window", ADISPLAY_ID_DEFAULT); |
| 2608 | window->setFrame(Rect{0, 0, 100, 100}); |
| 2609 | |
| 2610 | sp<FakeWindowHandle> outsideWindow = |
| 2611 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2612 | ADISPLAY_ID_DEFAULT); |
| 2613 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 2614 | outsideWindow->setWatchOutsideTouch(true); |
| 2615 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 2616 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 2617 | |
| 2618 | // Tap on first window. |
| 2619 | NotifyMotionArgs motionArgs = |
| 2620 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2621 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 2622 | mDispatcher->notifyMotion(&motionArgs); |
| 2623 | window->consumeMotionDown(); |
| 2624 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 2625 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 2626 | outsideWindow->consumeMotionEvent( |
| 2627 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 2628 | } |
| 2629 | |
| 2630 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2631 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 2632 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 2633 | * ACTION_OUTSIDE event is sent per gesture. |
| 2634 | */ |
| 2635 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 2636 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 2637 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2638 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2639 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2640 | window->setWatchOutsideTouch(true); |
| 2641 | window->setFrame(Rect{0, 0, 100, 100}); |
| 2642 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2643 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2644 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2645 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 2646 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2647 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 2648 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2649 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 2650 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 2651 | |
| 2652 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 2653 | NotifyMotionArgs motionArgs = |
| 2654 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2655 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 2656 | mDispatcher->notifyMotion(&motionArgs); |
| 2657 | window->assertNoEvents(); |
| 2658 | secondWindow->assertNoEvents(); |
| 2659 | |
| 2660 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 2661 | // Now, `window` should get ACTION_OUTSIDE. |
| 2662 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2663 | {PointF{-10, -10}, PointF{105, 105}}); |
| 2664 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 2665 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 2666 | window->consumeMotionEvent( |
| 2667 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2668 | secondWindow->consumeMotionDown(); |
| 2669 | thirdWindow->assertNoEvents(); |
| 2670 | |
| 2671 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 2672 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 2673 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2674 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 2675 | mDispatcher->notifyMotion(&motionArgs); |
| 2676 | window->assertNoEvents(); |
| 2677 | secondWindow->consumeMotionMove(); |
| 2678 | thirdWindow->consumeMotionDown(); |
| 2679 | } |
| 2680 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2681 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 2682 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2683 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2684 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2685 | window->setFocusable(true); |
| 2686 | |
| 2687 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 2688 | setFocusedWindow(window); |
| 2689 | |
| 2690 | window->consumeFocusEvent(true); |
| 2691 | |
| 2692 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2693 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2694 | mDispatcher->notifyKey(&keyDown); |
| 2695 | mDispatcher->notifyKey(&keyUp); |
| 2696 | |
| 2697 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2698 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2699 | |
| 2700 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
| 2701 | mDispatcher->onWindowInfosChanged({}, {}); |
| 2702 | |
| 2703 | window->consumeFocusEvent(false); |
| 2704 | |
| 2705 | mDispatcher->notifyKey(&keyDown); |
| 2706 | mDispatcher->notifyKey(&keyUp); |
| 2707 | window->assertNoEvents(); |
| 2708 | } |
| 2709 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2710 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 2711 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2712 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2713 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2714 | // Ensure window is non-split and have some transform. |
| 2715 | window->setPreventSplitting(true); |
| 2716 | window->setWindowOffset(20, 40); |
| 2717 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 2718 | |
| 2719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2720 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2721 | {50, 50})) |
| 2722 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2723 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2724 | |
| 2725 | const MotionEvent secondFingerDownEvent = |
| 2726 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2727 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2728 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2729 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 2730 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2731 | .x(-30) |
| 2732 | .y(-50)) |
| 2733 | .build(); |
| 2734 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2735 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2736 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2737 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2738 | |
| 2739 | const MotionEvent* event = window->consumeMotion(); |
| 2740 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 2741 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 2742 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 2743 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 2744 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 2745 | } |
| 2746 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2747 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2748 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 2749 | * |
| 2750 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 2751 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 2752 | * space. |
| 2753 | */ |
| 2754 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 2755 | public: |
| 2756 | void SetUp() override { |
| 2757 | InputDispatcherTest::SetUp(); |
| 2758 | mDisplayInfos.clear(); |
| 2759 | mWindowInfos.clear(); |
| 2760 | } |
| 2761 | |
| 2762 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 2763 | gui::DisplayInfo info; |
| 2764 | info.displayId = displayId; |
| 2765 | info.transform = transform; |
| 2766 | mDisplayInfos.push_back(std::move(info)); |
| 2767 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2768 | } |
| 2769 | |
| 2770 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 2771 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 2772 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 2773 | } |
| 2774 | |
| 2775 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 2776 | // on the display. |
| 2777 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 2778 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 2779 | // respectively. |
| 2780 | ui::Transform displayTransform; |
| 2781 | displayTransform.set(2, 0, 0, 4); |
| 2782 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 2783 | |
| 2784 | std::shared_ptr<FakeApplicationHandle> application = |
| 2785 | std::make_shared<FakeApplicationHandle>(); |
| 2786 | |
| 2787 | // Add two windows to the display. Their frames are represented in the display space. |
| 2788 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2789 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2790 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2791 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 2792 | addWindow(firstWindow); |
| 2793 | |
| 2794 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2795 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2796 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2797 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 2798 | addWindow(secondWindow); |
| 2799 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 2800 | } |
| 2801 | |
| 2802 | private: |
| 2803 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 2804 | std::vector<gui::WindowInfo> mWindowInfos; |
| 2805 | }; |
| 2806 | |
| 2807 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 2808 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2809 | // Send down to the first window. The point is represented in the display space. The point is |
| 2810 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 2811 | // end up in the incorrect window. |
| 2812 | NotifyMotionArgs downMotionArgs = |
| 2813 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2814 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 2815 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2816 | |
| 2817 | firstWindow->consumeMotionDown(); |
| 2818 | secondWindow->assertNoEvents(); |
| 2819 | } |
| 2820 | |
| 2821 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 2822 | // the event should be treated as being in the logical display space. |
| 2823 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 2824 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2825 | // Send down to the first window. The point is represented in the logical display space. The |
| 2826 | // point is selected so that if the hit test was done in logical display space, then it would |
| 2827 | // end up in the incorrect window. |
| 2828 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2829 | PointF{75 * 2, 55 * 4}); |
| 2830 | |
| 2831 | firstWindow->consumeMotionDown(); |
| 2832 | secondWindow->assertNoEvents(); |
| 2833 | } |
| 2834 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 2835 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 2836 | // event should be treated as being in the logical display space. |
| 2837 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 2838 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2839 | |
| 2840 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 2841 | ui::Transform injectedEventTransform; |
| 2842 | injectedEventTransform.set(matrix); |
| 2843 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 2844 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 2845 | |
| 2846 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2847 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2848 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2849 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2850 | .x(untransformedPoint.x) |
| 2851 | .y(untransformedPoint.y)) |
| 2852 | .build(); |
| 2853 | event.transform(matrix); |
| 2854 | |
| 2855 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 2856 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 2857 | |
| 2858 | firstWindow->consumeMotionDown(); |
| 2859 | secondWindow->assertNoEvents(); |
| 2860 | } |
| 2861 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2862 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 2863 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 2864 | |
| 2865 | // Send down to the second window. |
| 2866 | NotifyMotionArgs downMotionArgs = |
| 2867 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2868 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 2869 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2870 | |
| 2871 | firstWindow->assertNoEvents(); |
| 2872 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 2873 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 2874 | |
| 2875 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 2876 | EXPECT_EQ(300, event->getRawX(0)); |
| 2877 | EXPECT_EQ(880, event->getRawY(0)); |
| 2878 | |
| 2879 | // Ensure that the x and y values are in the window's coordinate space. |
| 2880 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 2881 | // the logical display space. This will be the origin of the window space. |
| 2882 | EXPECT_EQ(100, event->getX(0)); |
| 2883 | EXPECT_EQ(80, event->getY(0)); |
| 2884 | } |
| 2885 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 2886 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 2887 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2888 | |
| 2889 | class TransferTouchFixture : public InputDispatcherTest, |
| 2890 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 2891 | |
| 2892 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2893 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2894 | |
| 2895 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2896 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2897 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 2898 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2899 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2900 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2901 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2902 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2903 | sp<FakeWindowHandle> wallpaper = |
| 2904 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2905 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2906 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2907 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2908 | |
| 2909 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2910 | NotifyMotionArgs downMotionArgs = |
| 2911 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2912 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2913 | mDispatcher->notifyMotion(&downMotionArgs); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2914 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2915 | // Only the first window should get the down event |
| 2916 | firstWindow->consumeMotionDown(); |
| 2917 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2918 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2919 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2920 | // Transfer touch to the second window |
| 2921 | TransferFunction f = GetParam(); |
| 2922 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2923 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2924 | // The first window gets cancel and the second gets down |
| 2925 | firstWindow->consumeMotionCancel(); |
| 2926 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2927 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2928 | |
| 2929 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 2930 | NotifyMotionArgs upMotionArgs = |
| 2931 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2932 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2933 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2934 | // The first window gets no events and the second gets up |
| 2935 | firstWindow->assertNoEvents(); |
| 2936 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2937 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2938 | } |
| 2939 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2940 | /** |
| 2941 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 2942 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 2943 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 2944 | * natural to the user. |
| 2945 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 2946 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 2947 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 2948 | * the first window to the second. |
| 2949 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 2950 | * the other API, as well. |
| 2951 | */ |
| 2952 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 2953 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2954 | |
| 2955 | // Create a couple of windows + a spy window |
| 2956 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2957 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2958 | spyWindow->setTrustedOverlay(true); |
| 2959 | spyWindow->setSpy(true); |
| 2960 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2961 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2962 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2963 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 2964 | |
| 2965 | // Add the windows to the dispatcher |
| 2966 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 2967 | |
| 2968 | // Send down to the first window |
| 2969 | NotifyMotionArgs downMotionArgs = |
| 2970 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2971 | ADISPLAY_ID_DEFAULT); |
| 2972 | mDispatcher->notifyMotion(&downMotionArgs); |
| 2973 | // Only the first window and spy should get the down event |
| 2974 | spyWindow->consumeMotionDown(); |
| 2975 | firstWindow->consumeMotionDown(); |
| 2976 | |
| 2977 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 2978 | // if f === 'transferTouch'. |
| 2979 | TransferFunction f = GetParam(); |
| 2980 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 2981 | ASSERT_TRUE(success); |
| 2982 | // The first window gets cancel and the second gets down |
| 2983 | firstWindow->consumeMotionCancel(); |
| 2984 | secondWindow->consumeMotionDown(); |
| 2985 | |
| 2986 | // Send up event to the second window |
| 2987 | NotifyMotionArgs upMotionArgs = |
| 2988 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2989 | ADISPLAY_ID_DEFAULT); |
| 2990 | mDispatcher->notifyMotion(&upMotionArgs); |
| 2991 | // The first window gets no events and the second+spy get up |
| 2992 | firstWindow->assertNoEvents(); |
| 2993 | spyWindow->consumeMotionUp(); |
| 2994 | secondWindow->consumeMotionUp(); |
| 2995 | } |
| 2996 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 2997 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2998 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 2999 | |
| 3000 | PointF touchPoint = {10, 10}; |
| 3001 | |
| 3002 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3003 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3004 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3005 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3006 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3007 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3008 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3009 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3010 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3011 | |
| 3012 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3013 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3014 | |
| 3015 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3016 | NotifyMotionArgs downMotionArgs = |
| 3017 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3018 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3019 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3020 | // Only the first window should get the down event |
| 3021 | firstWindow->consumeMotionDown(); |
| 3022 | secondWindow->assertNoEvents(); |
| 3023 | |
| 3024 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3025 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3026 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3027 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3028 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 3029 | // Only the first window should get the pointer down event |
| 3030 | firstWindow->consumeMotionPointerDown(1); |
| 3031 | secondWindow->assertNoEvents(); |
| 3032 | |
| 3033 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3034 | TransferFunction f = GetParam(); |
| 3035 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3036 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3037 | // The first window gets cancel and the second gets down and pointer down |
| 3038 | firstWindow->consumeMotionCancel(); |
| 3039 | secondWindow->consumeMotionDown(); |
| 3040 | secondWindow->consumeMotionPointerDown(1); |
| 3041 | |
| 3042 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3043 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3044 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3045 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3046 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3047 | // The first window gets nothing and the second gets pointer up |
| 3048 | firstWindow->assertNoEvents(); |
| 3049 | secondWindow->consumeMotionPointerUp(1); |
| 3050 | |
| 3051 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3052 | NotifyMotionArgs upMotionArgs = |
| 3053 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3054 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3055 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3056 | // The first window gets nothing and the second gets up |
| 3057 | firstWindow->assertNoEvents(); |
| 3058 | secondWindow->consumeMotionUp(); |
| 3059 | } |
| 3060 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3061 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 3062 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3063 | |
| 3064 | // Create a couple of windows |
| 3065 | sp<FakeWindowHandle> firstWindow = |
| 3066 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3067 | ADISPLAY_ID_DEFAULT); |
| 3068 | firstWindow->setDupTouchToWallpaper(true); |
| 3069 | sp<FakeWindowHandle> secondWindow = |
| 3070 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3071 | ADISPLAY_ID_DEFAULT); |
| 3072 | secondWindow->setDupTouchToWallpaper(true); |
| 3073 | |
| 3074 | sp<FakeWindowHandle> wallpaper1 = |
| 3075 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 3076 | wallpaper1->setIsWallpaper(true); |
| 3077 | |
| 3078 | sp<FakeWindowHandle> wallpaper2 = |
| 3079 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 3080 | wallpaper2->setIsWallpaper(true); |
| 3081 | // Add the windows to the dispatcher |
| 3082 | mDispatcher->setInputWindows( |
| 3083 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 3084 | |
| 3085 | // Send down to the first window |
| 3086 | NotifyMotionArgs downMotionArgs = |
| 3087 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3088 | ADISPLAY_ID_DEFAULT); |
| 3089 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3090 | |
| 3091 | // Only the first window should get the down event |
| 3092 | firstWindow->consumeMotionDown(); |
| 3093 | secondWindow->assertNoEvents(); |
| 3094 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3095 | wallpaper2->assertNoEvents(); |
| 3096 | |
| 3097 | // Transfer touch focus to the second window |
| 3098 | TransferFunction f = GetParam(); |
| 3099 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3100 | ASSERT_TRUE(success); |
| 3101 | |
| 3102 | // The first window gets cancel and the second gets down |
| 3103 | firstWindow->consumeMotionCancel(); |
| 3104 | secondWindow->consumeMotionDown(); |
| 3105 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3106 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3107 | |
| 3108 | // Send up event to the second window |
| 3109 | NotifyMotionArgs upMotionArgs = |
| 3110 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3111 | ADISPLAY_ID_DEFAULT); |
| 3112 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3113 | // The first window gets no events and the second gets up |
| 3114 | firstWindow->assertNoEvents(); |
| 3115 | secondWindow->consumeMotionUp(); |
| 3116 | wallpaper1->assertNoEvents(); |
| 3117 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3118 | } |
| 3119 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3120 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 3121 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 3122 | // for the case where there are multiple pointers split across several windows. |
| 3123 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 3124 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3125 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3126 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3127 | return dispatcher->transferTouch(destChannelToken, |
| 3128 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3129 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3130 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3131 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3132 | return dispatcher->transferTouchFocus(from, to, |
| 3133 | false /*isDragAndDrop*/); |
| 3134 | })); |
| 3135 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3136 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3137 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3138 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3139 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3140 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3141 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3142 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3143 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3144 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3145 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3146 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3147 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3148 | |
| 3149 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3150 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3151 | |
| 3152 | PointF pointInFirst = {300, 200}; |
| 3153 | PointF pointInSecond = {300, 600}; |
| 3154 | |
| 3155 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3156 | NotifyMotionArgs firstDownMotionArgs = |
| 3157 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3158 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3159 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3160 | // Only the first window should get the down event |
| 3161 | firstWindow->consumeMotionDown(); |
| 3162 | secondWindow->assertNoEvents(); |
| 3163 | |
| 3164 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3165 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3166 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3167 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3168 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3169 | // The first window gets a move and the second a down |
| 3170 | firstWindow->consumeMotionMove(); |
| 3171 | secondWindow->consumeMotionDown(); |
| 3172 | |
| 3173 | // Transfer touch focus to the second window |
| 3174 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 3175 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 3176 | firstWindow->consumeMotionCancel(); |
| 3177 | secondWindow->consumeMotionPointerDown(1); |
| 3178 | |
| 3179 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3180 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3181 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3182 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3183 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3184 | // The first window gets nothing and the second gets pointer up |
| 3185 | firstWindow->assertNoEvents(); |
| 3186 | secondWindow->consumeMotionPointerUp(1); |
| 3187 | |
| 3188 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3189 | NotifyMotionArgs upMotionArgs = |
| 3190 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3191 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3192 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3193 | // The first window gets nothing and the second gets up |
| 3194 | firstWindow->assertNoEvents(); |
| 3195 | secondWindow->consumeMotionUp(); |
| 3196 | } |
| 3197 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3198 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 3199 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 3200 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 3201 | // window should get nothing. |
| 3202 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 3203 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3204 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3205 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3206 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3207 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3208 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3209 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3210 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3211 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3212 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3213 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3214 | |
| 3215 | // Add the windows to the dispatcher |
| 3216 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3217 | |
| 3218 | PointF pointInFirst = {300, 200}; |
| 3219 | PointF pointInSecond = {300, 600}; |
| 3220 | |
| 3221 | // Send down to the first window |
| 3222 | NotifyMotionArgs firstDownMotionArgs = |
| 3223 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3224 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3225 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3226 | // Only the first window should get the down event |
| 3227 | firstWindow->consumeMotionDown(); |
| 3228 | secondWindow->assertNoEvents(); |
| 3229 | |
| 3230 | // Send down to the second window |
| 3231 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3232 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3233 | {pointInFirst, pointInSecond}); |
| 3234 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3235 | // The first window gets a move and the second a down |
| 3236 | firstWindow->consumeMotionMove(); |
| 3237 | secondWindow->consumeMotionDown(); |
| 3238 | |
| 3239 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3240 | const bool transferred = |
| 3241 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3242 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 3243 | ASSERT_FALSE(transferred); |
| 3244 | firstWindow->assertNoEvents(); |
| 3245 | secondWindow->assertNoEvents(); |
| 3246 | |
| 3247 | // The rest of the dispatch should proceed as normal |
| 3248 | // Send pointer up to the second window |
| 3249 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3250 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3251 | {pointInFirst, pointInSecond}); |
| 3252 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3253 | // The first window gets MOVE and the second gets pointer up |
| 3254 | firstWindow->consumeMotionMove(); |
| 3255 | secondWindow->consumeMotionUp(); |
| 3256 | |
| 3257 | // Send up event to the first window |
| 3258 | NotifyMotionArgs upMotionArgs = |
| 3259 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3260 | ADISPLAY_ID_DEFAULT); |
| 3261 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3262 | // The first window gets nothing and the second gets up |
| 3263 | firstWindow->consumeMotionUp(); |
| 3264 | secondWindow->assertNoEvents(); |
| 3265 | } |
| 3266 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3267 | // This case will create two windows and one mirrored window on the default display and mirror |
| 3268 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 3269 | // the windows info of second display before default display. |
| 3270 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 3271 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3272 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3273 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3274 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3275 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3276 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3277 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3278 | |
| 3279 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3280 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3281 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3282 | |
| 3283 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3284 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3285 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3286 | |
| 3287 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3288 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3289 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3290 | |
| 3291 | // Update window info, let it find window handle of second display first. |
| 3292 | mDispatcher->setInputWindows( |
| 3293 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3294 | {ADISPLAY_ID_DEFAULT, |
| 3295 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3296 | |
| 3297 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3298 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3299 | {50, 50})) |
| 3300 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3301 | |
| 3302 | // Window should receive motion event. |
| 3303 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3304 | |
| 3305 | // Transfer touch focus |
| 3306 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 3307 | secondWindowInPrimary->getToken())); |
| 3308 | // The first window gets cancel. |
| 3309 | firstWindowInPrimary->consumeMotionCancel(); |
| 3310 | secondWindowInPrimary->consumeMotionDown(); |
| 3311 | |
| 3312 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3313 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3314 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 3315 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3316 | firstWindowInPrimary->assertNoEvents(); |
| 3317 | secondWindowInPrimary->consumeMotionMove(); |
| 3318 | |
| 3319 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3320 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3321 | {150, 50})) |
| 3322 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3323 | firstWindowInPrimary->assertNoEvents(); |
| 3324 | secondWindowInPrimary->consumeMotionUp(); |
| 3325 | } |
| 3326 | |
| 3327 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 3328 | // 'transferTouch' api. |
| 3329 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 3330 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3331 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3332 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3333 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3334 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3335 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3336 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3337 | |
| 3338 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3339 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3340 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3341 | |
| 3342 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3343 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3344 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3345 | |
| 3346 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3347 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3348 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3349 | |
| 3350 | // Update window info, let it find window handle of second display first. |
| 3351 | mDispatcher->setInputWindows( |
| 3352 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3353 | {ADISPLAY_ID_DEFAULT, |
| 3354 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3355 | |
| 3356 | // Touch on second display. |
| 3357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3358 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 3359 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3360 | |
| 3361 | // Window should receive motion event. |
| 3362 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3363 | |
| 3364 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3365 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3366 | |
| 3367 | // The first window gets cancel. |
| 3368 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3369 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3370 | |
| 3371 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3372 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3373 | SECOND_DISPLAY_ID, {150, 50})) |
| 3374 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3375 | firstWindowInPrimary->assertNoEvents(); |
| 3376 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 3377 | |
| 3378 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3379 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 3380 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3381 | firstWindowInPrimary->assertNoEvents(); |
| 3382 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 3383 | } |
| 3384 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3385 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3386 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3387 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3388 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3389 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3390 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3391 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3392 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3393 | |
| 3394 | window->consumeFocusEvent(true); |
| 3395 | |
| 3396 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3397 | mDispatcher->notifyKey(&keyArgs); |
| 3398 | |
| 3399 | // Window should receive key down event. |
| 3400 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3401 | } |
| 3402 | |
| 3403 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3404 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3405 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3406 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3407 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3408 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3409 | |
| 3410 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3411 | mDispatcher->notifyKey(&keyArgs); |
| 3412 | mDispatcher->waitForIdle(); |
| 3413 | |
| 3414 | window->assertNoEvents(); |
| 3415 | } |
| 3416 | |
| 3417 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 3418 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3419 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3420 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3421 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3422 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3423 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3424 | |
| 3425 | // Send key |
| 3426 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3427 | mDispatcher->notifyKey(&keyArgs); |
| 3428 | // Send motion |
| 3429 | NotifyMotionArgs motionArgs = |
| 3430 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3431 | ADISPLAY_ID_DEFAULT); |
| 3432 | mDispatcher->notifyMotion(&motionArgs); |
| 3433 | |
| 3434 | // Window should receive only the motion event |
| 3435 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3436 | window->assertNoEvents(); // Key event or focus event will not be received |
| 3437 | } |
| 3438 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3439 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 3440 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3441 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3442 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3443 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3444 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3445 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3446 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3447 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3448 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3449 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3450 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3451 | |
| 3452 | // Add the windows to the dispatcher |
| 3453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3454 | |
| 3455 | PointF pointInFirst = {300, 200}; |
| 3456 | PointF pointInSecond = {300, 600}; |
| 3457 | |
| 3458 | // Send down to the first window |
| 3459 | NotifyMotionArgs firstDownMotionArgs = |
| 3460 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3461 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3462 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3463 | // Only the first window should get the down event |
| 3464 | firstWindow->consumeMotionDown(); |
| 3465 | secondWindow->assertNoEvents(); |
| 3466 | |
| 3467 | // Send down to the second window |
| 3468 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3469 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3470 | {pointInFirst, pointInSecond}); |
| 3471 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3472 | // The first window gets a move and the second a down |
| 3473 | firstWindow->consumeMotionMove(); |
| 3474 | secondWindow->consumeMotionDown(); |
| 3475 | |
| 3476 | // Send pointer cancel to the second window |
| 3477 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3478 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3479 | {pointInFirst, pointInSecond}); |
| 3480 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3481 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3482 | // The first window gets move and the second gets cancel. |
| 3483 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3484 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3485 | |
| 3486 | // Send up event. |
| 3487 | NotifyMotionArgs upMotionArgs = |
| 3488 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3489 | ADISPLAY_ID_DEFAULT); |
| 3490 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3491 | // The first window gets up and the second gets nothing. |
| 3492 | firstWindow->consumeMotionUp(); |
| 3493 | secondWindow->assertNoEvents(); |
| 3494 | } |
| 3495 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3496 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 3497 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3498 | |
| 3499 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3500 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3501 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3502 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 3503 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 3504 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 3505 | |
| 3506 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); |
| 3507 | window->assertNoEvents(); |
| 3508 | mDispatcher->waitForIdle(); |
| 3509 | } |
| 3510 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3511 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3512 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3513 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3514 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3515 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3516 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3517 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3520 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 3521 | |
| 3522 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3523 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 3524 | expectedDisplayId, expectedFlags); |
| 3525 | } |
| 3526 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3527 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 3528 | |
| 3529 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 3530 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3531 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3532 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 3533 | expectedDisplayId, expectedFlags); |
| 3534 | } |
| 3535 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3536 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3537 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 3538 | expectedDisplayId, expectedFlags); |
| 3539 | } |
| 3540 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3541 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3542 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 3543 | expectedDisplayId, expectedFlags); |
| 3544 | } |
| 3545 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3546 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3547 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3548 | expectedDisplayId, expectedFlags); |
| 3549 | } |
| 3550 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3551 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 3552 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 3553 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3554 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
| 3555 | 0 /*expectedFlags*/); |
| 3556 | } |
| 3557 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3558 | MotionEvent* consumeMotion() { |
| 3559 | InputEvent* event = mInputReceiver->consume(); |
| 3560 | if (!event) { |
| 3561 | ADD_FAILURE() << "No event was produced"; |
| 3562 | return nullptr; |
| 3563 | } |
| 3564 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 3565 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 3566 | return nullptr; |
| 3567 | } |
| 3568 | return static_cast<MotionEvent*>(event); |
| 3569 | } |
| 3570 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3571 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 3572 | |
| 3573 | private: |
| 3574 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3575 | }; |
| 3576 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3577 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 3578 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3579 | /** |
| 3580 | * Two entities that receive touch: A window, and a global monitor. |
| 3581 | * The touch goes to the window, and then the window disappears. |
| 3582 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 3583 | * for the monitor, as well. |
| 3584 | * 1. foregroundWindow |
| 3585 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 3586 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3587 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3588 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3589 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3590 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3591 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3592 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3593 | |
| 3594 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3596 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3597 | {100, 200})) |
| 3598 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3599 | |
| 3600 | // Both the foreground window and the global monitor should receive the touch down |
| 3601 | window->consumeMotionDown(); |
| 3602 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3603 | |
| 3604 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3605 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3606 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 3607 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3608 | |
| 3609 | window->consumeMotionMove(); |
| 3610 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3611 | |
| 3612 | // Now the foreground window goes away |
| 3613 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3614 | window->consumeMotionCancel(); |
| 3615 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 3616 | |
| 3617 | // If more events come in, there will be no more foreground window to send them to. This will |
| 3618 | // cause a cancel for the monitor, as well. |
| 3619 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3620 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3621 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 3622 | << "Injection should fail because the window was removed"; |
| 3623 | window->assertNoEvents(); |
| 3624 | // Global monitor now gets the cancel |
| 3625 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3626 | } |
| 3627 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3628 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3629 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3630 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3631 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3632 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3633 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3634 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3635 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3636 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3637 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3638 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3639 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3640 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3643 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 3644 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3645 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3646 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3647 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3648 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3649 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3650 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3652 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3653 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3654 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3655 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3656 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3657 | // Pilfer pointers from the monitor. |
| 3658 | // This should not do anything and the window should continue to receive events. |
| 3659 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3660 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3662 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3663 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3664 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3665 | |
| 3666 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3667 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3670 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3671 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3672 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3673 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3674 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3675 | window->setWindowOffset(20, 40); |
| 3676 | window->setWindowTransform(0, 1, -1, 0); |
| 3677 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3678 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3679 | |
| 3680 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3681 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3682 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3683 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3684 | MotionEvent* event = monitor.consumeMotion(); |
| 3685 | // Even though window has transform, gesture monitor must not. |
| 3686 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 3687 | } |
| 3688 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3689 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3690 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3691 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3692 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3693 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3694 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3695 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 3696 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3699 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3700 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3701 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3702 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3703 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3704 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3705 | |
| 3706 | NotifyMotionArgs motionArgs = |
| 3707 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3708 | ADISPLAY_ID_DEFAULT); |
| 3709 | |
| 3710 | mDispatcher->notifyMotion(&motionArgs); |
| 3711 | // Window should receive motion down event. |
| 3712 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3713 | |
| 3714 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3715 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3716 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3717 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 3718 | motionArgs.pointerCoords[0].getX() - 10); |
| 3719 | |
| 3720 | mDispatcher->notifyMotion(&motionArgs); |
| 3721 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 3722 | 0 /*expectedFlags*/); |
| 3723 | } |
| 3724 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3725 | /** |
| 3726 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 3727 | * the device default right away. In the test scenario, we check both the default value, |
| 3728 | * and the action of enabling / disabling. |
| 3729 | */ |
| 3730 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3731 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3732 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3733 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3734 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3735 | |
| 3736 | // Set focused application. |
| 3737 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3738 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3739 | |
| 3740 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3741 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3742 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3743 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3744 | |
| 3745 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3746 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3747 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3748 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 3749 | |
| 3750 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3751 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 3752 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3753 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3754 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3755 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3756 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3757 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 3758 | |
| 3759 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3760 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3761 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3762 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 3763 | |
| 3764 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 3765 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 3766 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 3767 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3768 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3769 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3770 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3771 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3772 | |
| 3773 | window->assertNoEvents(); |
| 3774 | } |
| 3775 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3776 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3777 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3778 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3779 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3780 | |
| 3781 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3782 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3783 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3784 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3785 | setFocusedWindow(window); |
| 3786 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3787 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 3788 | |
| 3789 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 3790 | mDispatcher->notifyKey(&keyArgs); |
| 3791 | |
| 3792 | InputEvent* event = window->consume(); |
| 3793 | ASSERT_NE(event, nullptr); |
| 3794 | |
| 3795 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3796 | ASSERT_NE(verified, nullptr); |
| 3797 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 3798 | |
| 3799 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 3800 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 3801 | ASSERT_EQ(keyArgs.source, verified->source); |
| 3802 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 3803 | |
| 3804 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 3805 | |
| 3806 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3807 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3808 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 3809 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 3810 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 3811 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 3812 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 3813 | } |
| 3814 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3815 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3816 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3817 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3818 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3819 | |
| 3820 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3821 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3822 | ui::Transform transform; |
| 3823 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 3824 | |
| 3825 | gui::DisplayInfo displayInfo; |
| 3826 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 3827 | displayInfo.transform = transform; |
| 3828 | |
| 3829 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3830 | |
| 3831 | NotifyMotionArgs motionArgs = |
| 3832 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3833 | ADISPLAY_ID_DEFAULT); |
| 3834 | mDispatcher->notifyMotion(&motionArgs); |
| 3835 | |
| 3836 | InputEvent* event = window->consume(); |
| 3837 | ASSERT_NE(event, nullptr); |
| 3838 | |
| 3839 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 3840 | ASSERT_NE(verified, nullptr); |
| 3841 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 3842 | |
| 3843 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 3844 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 3845 | EXPECT_EQ(motionArgs.source, verified->source); |
| 3846 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 3847 | |
| 3848 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 3849 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 3850 | const vec2 rawXY = |
| 3851 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 3852 | motionArgs.pointerCoords[0].getXYValue()); |
| 3853 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 3854 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3855 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3856 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 3857 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 3858 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 3859 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 3860 | } |
| 3861 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 3862 | /** |
| 3863 | * Ensure that separate calls to sign the same data are generating the same key. |
| 3864 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 3865 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 3866 | * tests. |
| 3867 | */ |
| 3868 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 3869 | KeyEvent event = getTestKeyEvent(); |
| 3870 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3871 | |
| 3872 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 3873 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 3874 | ASSERT_EQ(hmac1, hmac2); |
| 3875 | } |
| 3876 | |
| 3877 | /** |
| 3878 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 3879 | */ |
| 3880 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 3881 | KeyEvent event = getTestKeyEvent(); |
| 3882 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 3883 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 3884 | |
| 3885 | verifiedEvent.deviceId += 1; |
| 3886 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3887 | |
| 3888 | verifiedEvent.source += 1; |
| 3889 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3890 | |
| 3891 | verifiedEvent.eventTimeNanos += 1; |
| 3892 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3893 | |
| 3894 | verifiedEvent.displayId += 1; |
| 3895 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3896 | |
| 3897 | verifiedEvent.action += 1; |
| 3898 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3899 | |
| 3900 | verifiedEvent.downTimeNanos += 1; |
| 3901 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3902 | |
| 3903 | verifiedEvent.flags += 1; |
| 3904 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3905 | |
| 3906 | verifiedEvent.keyCode += 1; |
| 3907 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3908 | |
| 3909 | verifiedEvent.scanCode += 1; |
| 3910 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3911 | |
| 3912 | verifiedEvent.metaState += 1; |
| 3913 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3914 | |
| 3915 | verifiedEvent.repeatCount += 1; |
| 3916 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 3917 | } |
| 3918 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3919 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 3920 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3921 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3922 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3923 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3924 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3925 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3926 | |
| 3927 | // Top window is also focusable but is not granted focus. |
| 3928 | windowTop->setFocusable(true); |
| 3929 | windowSecond->setFocusable(true); |
| 3930 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3931 | setFocusedWindow(windowSecond); |
| 3932 | |
| 3933 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3934 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3935 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3936 | |
| 3937 | // Focused window should receive event. |
| 3938 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3939 | windowTop->assertNoEvents(); |
| 3940 | } |
| 3941 | |
| 3942 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 3943 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3944 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3945 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3946 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3947 | |
| 3948 | window->setFocusable(true); |
| 3949 | // Release channel for window is no longer valid. |
| 3950 | window->releaseChannel(); |
| 3951 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3952 | setFocusedWindow(window); |
| 3953 | |
| 3954 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3955 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3956 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3957 | |
| 3958 | // window channel is invalid, so it should not receive any input event. |
| 3959 | window->assertNoEvents(); |
| 3960 | } |
| 3961 | |
| 3962 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 3963 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3964 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3965 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3966 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3967 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3968 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3969 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3970 | setFocusedWindow(window); |
| 3971 | |
| 3972 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3973 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3974 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3975 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3976 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3977 | window->assertNoEvents(); |
| 3978 | } |
| 3979 | |
| 3980 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 3981 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3982 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3983 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3984 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3985 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3986 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3987 | |
| 3988 | windowTop->setFocusable(true); |
| 3989 | windowSecond->setFocusable(true); |
| 3990 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 3991 | setFocusedWindow(windowTop); |
| 3992 | windowTop->consumeFocusEvent(true); |
| 3993 | |
| 3994 | setFocusedWindow(windowSecond, windowTop); |
| 3995 | windowSecond->consumeFocusEvent(true); |
| 3996 | windowTop->consumeFocusEvent(false); |
| 3997 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3998 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3999 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4000 | |
| 4001 | // Focused window should receive event. |
| 4002 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4003 | } |
| 4004 | |
| 4005 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 4006 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4007 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4008 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4009 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4010 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4011 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4012 | |
| 4013 | windowTop->setFocusable(true); |
| 4014 | windowSecond->setFocusable(true); |
| 4015 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4016 | setFocusedWindow(windowSecond, windowTop); |
| 4017 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4018 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4019 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4020 | |
| 4021 | // Event should be dropped. |
| 4022 | windowTop->assertNoEvents(); |
| 4023 | windowSecond->assertNoEvents(); |
| 4024 | } |
| 4025 | |
| 4026 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 4027 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4028 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4029 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4030 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4031 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 4032 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4033 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4034 | |
| 4035 | window->setFocusable(true); |
| 4036 | previousFocusedWindow->setFocusable(true); |
| 4037 | window->setVisible(false); |
| 4038 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 4039 | setFocusedWindow(previousFocusedWindow); |
| 4040 | previousFocusedWindow->consumeFocusEvent(true); |
| 4041 | |
| 4042 | // Requesting focus on invisible window takes focus from currently focused window. |
| 4043 | setFocusedWindow(window); |
| 4044 | previousFocusedWindow->consumeFocusEvent(false); |
| 4045 | |
| 4046 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4048 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4049 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4050 | |
| 4051 | // Window does not get focus event or key down. |
| 4052 | window->assertNoEvents(); |
| 4053 | |
| 4054 | // Window becomes visible. |
| 4055 | window->setVisible(true); |
| 4056 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4057 | |
| 4058 | // Window receives focus event. |
| 4059 | window->consumeFocusEvent(true); |
| 4060 | // Focused window receives key down. |
| 4061 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4062 | } |
| 4063 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4064 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 4065 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4066 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4067 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4068 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4069 | |
| 4070 | // window is granted focus. |
| 4071 | window->setFocusable(true); |
| 4072 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4073 | setFocusedWindow(window); |
| 4074 | window->consumeFocusEvent(true); |
| 4075 | |
| 4076 | // When a display is removed window loses focus. |
| 4077 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 4078 | window->consumeFocusEvent(false); |
| 4079 | } |
| 4080 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4081 | /** |
| 4082 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 4083 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 4084 | * of the 'slipperyEnterWindow'. |
| 4085 | * |
| 4086 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 4087 | * a way so that the touched location is no longer covered by the top window. |
| 4088 | * |
| 4089 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 4090 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 4091 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 4092 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 4093 | * with ACTION_DOWN). |
| 4094 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 4095 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 4096 | * |
| 4097 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 4098 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 4099 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 4100 | * |
| 4101 | * In this test, we ensure that the event received by the bottom window has |
| 4102 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 4103 | */ |
| 4104 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4105 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 4106 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4107 | |
| 4108 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4109 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4110 | |
| 4111 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4112 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4113 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4114 | // Make sure this one overlaps the bottom window |
| 4115 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 4116 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 4117 | // one. Windows with the same owner are not considered to be occluding each other. |
| 4118 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 4119 | |
| 4120 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4121 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4122 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4123 | |
| 4124 | mDispatcher->setInputWindows( |
| 4125 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4126 | |
| 4127 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 4128 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4129 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 4130 | mDispatcher->notifyMotion(&args); |
| 4131 | slipperyExitWindow->consumeMotionDown(); |
| 4132 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 4133 | mDispatcher->setInputWindows( |
| 4134 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4135 | |
| 4136 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4137 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 4138 | mDispatcher->notifyMotion(&args); |
| 4139 | |
| 4140 | slipperyExitWindow->consumeMotionCancel(); |
| 4141 | |
| 4142 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 4143 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 4144 | } |
| 4145 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4146 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 4147 | protected: |
| 4148 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 4149 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 4150 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4151 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4152 | sp<FakeWindowHandle> mWindow; |
| 4153 | |
| 4154 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4155 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4156 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4157 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4158 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 4159 | ASSERT_EQ(OK, mDispatcher->start()); |
| 4160 | |
| 4161 | setUpWindow(); |
| 4162 | } |
| 4163 | |
| 4164 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4165 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4166 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4167 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4168 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4169 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4170 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4171 | mWindow->consumeFocusEvent(true); |
| 4172 | } |
| 4173 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4174 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4175 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4176 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4177 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 4178 | mDispatcher->notifyKey(&keyArgs); |
| 4179 | |
| 4180 | // Window should receive key down event. |
| 4181 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4182 | } |
| 4183 | |
| 4184 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 4185 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 4186 | InputEvent* repeatEvent = mWindow->consume(); |
| 4187 | ASSERT_NE(nullptr, repeatEvent); |
| 4188 | |
| 4189 | uint32_t eventType = repeatEvent->getType(); |
| 4190 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 4191 | |
| 4192 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 4193 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 4194 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 4195 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 4196 | } |
| 4197 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4198 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4199 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4200 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4201 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 4202 | mDispatcher->notifyKey(&keyArgs); |
| 4203 | |
| 4204 | // Window should receive key down event. |
| 4205 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 4206 | 0 /*expectedFlags*/); |
| 4207 | } |
| 4208 | }; |
| 4209 | |
| 4210 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4211 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4212 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4213 | expectKeyRepeatOnce(repeatCount); |
| 4214 | } |
| 4215 | } |
| 4216 | |
| 4217 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 4218 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4219 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4220 | expectKeyRepeatOnce(repeatCount); |
| 4221 | } |
| 4222 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4223 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4224 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4225 | expectKeyRepeatOnce(repeatCount); |
| 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4230 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4231 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4232 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 4233 | mWindow->assertNoEvents(); |
| 4234 | } |
| 4235 | |
| 4236 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 4237 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4238 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4239 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4240 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4241 | // Stale key up from device 1. |
| 4242 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 4243 | // Device 2 is still down, keep repeating |
| 4244 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 4245 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 4246 | // Device 2 key up |
| 4247 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 4248 | mWindow->assertNoEvents(); |
| 4249 | } |
| 4250 | |
| 4251 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 4252 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4253 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4254 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4255 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4256 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 4257 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 4258 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4259 | mWindow->assertNoEvents(); |
| 4260 | } |
| 4261 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4262 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 4263 | sendAndConsumeKeyDown(DEVICE_ID); |
| 4264 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4265 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
| 4266 | mDispatcher->notifyDeviceReset(&args); |
| 4267 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 4268 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 4269 | mWindow->assertNoEvents(); |
| 4270 | } |
| 4271 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4272 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4273 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4274 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4275 | InputEvent* repeatEvent = mWindow->consume(); |
| 4276 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4277 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 4278 | IdGenerator::getSource(repeatEvent->getId())); |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4283 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4284 | |
| 4285 | std::unordered_set<int32_t> idSet; |
| 4286 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4287 | InputEvent* repeatEvent = mWindow->consume(); |
| 4288 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4289 | int32_t id = repeatEvent->getId(); |
| 4290 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 4291 | idSet.insert(id); |
| 4292 | } |
| 4293 | } |
| 4294 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4295 | /* Test InputDispatcher for MultiDisplay */ |
| 4296 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 4297 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4298 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4299 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4300 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4301 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4302 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4303 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4304 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4305 | // Set focus window for primary display, but focused display would be second one. |
| 4306 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4307 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4308 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4309 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4310 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4311 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4312 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4313 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4314 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4315 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4316 | // Set focus display to second one. |
| 4317 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 4318 | // Set focus window for second display. |
| 4319 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4320 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4321 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4322 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4323 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4324 | } |
| 4325 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4326 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4327 | InputDispatcherTest::TearDown(); |
| 4328 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4329 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4330 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4331 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4332 | windowInSecondary.clear(); |
| 4333 | } |
| 4334 | |
| 4335 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4336 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4337 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4338 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4339 | sp<FakeWindowHandle> windowInSecondary; |
| 4340 | }; |
| 4341 | |
| 4342 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 4343 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4344 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4345 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4346 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4347 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4348 | windowInSecondary->assertNoEvents(); |
| 4349 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4350 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4351 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4352 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4353 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4354 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4355 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4356 | } |
| 4357 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4358 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4359 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4361 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4362 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4363 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4364 | windowInSecondary->assertNoEvents(); |
| 4365 | |
| 4366 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4367 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4368 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4369 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4370 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4371 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4372 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4373 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4374 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4375 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4376 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 4377 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4378 | |
| 4379 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4380 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4381 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4382 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4383 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4384 | windowInSecondary->assertNoEvents(); |
| 4385 | } |
| 4386 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4387 | // Test per-display input monitors for motion event. |
| 4388 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4389 | FakeMonitorReceiver monitorInPrimary = |
| 4390 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4391 | FakeMonitorReceiver monitorInSecondary = |
| 4392 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4393 | |
| 4394 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4395 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4396 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4397 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4398 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4399 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4400 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4401 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4402 | |
| 4403 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4404 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4405 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4406 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4407 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4408 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4409 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4410 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4411 | |
| 4412 | // Test inject a non-pointer motion event. |
| 4413 | // If specific a display, it will dispatch to the focused window of particular display, |
| 4414 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4416 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 4417 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4418 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4419 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4420 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4421 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | // Test per-display input monitors for key event. |
| 4425 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4426 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4427 | FakeMonitorReceiver monitorInPrimary = |
| 4428 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4429 | FakeMonitorReceiver monitorInSecondary = |
| 4430 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4431 | |
| 4432 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4433 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4434 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4435 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4436 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4437 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4438 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4439 | } |
| 4440 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4441 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 4442 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4443 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4444 | secondWindowInPrimary->setFocusable(true); |
| 4445 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 4446 | setFocusedWindow(secondWindowInPrimary); |
| 4447 | windowInPrimary->consumeFocusEvent(false); |
| 4448 | secondWindowInPrimary->consumeFocusEvent(true); |
| 4449 | |
| 4450 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4451 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 4452 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4453 | windowInPrimary->assertNoEvents(); |
| 4454 | windowInSecondary->assertNoEvents(); |
| 4455 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4456 | } |
| 4457 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 4458 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 4459 | FakeMonitorReceiver monitorInPrimary = |
| 4460 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4461 | FakeMonitorReceiver monitorInSecondary = |
| 4462 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 4463 | |
| 4464 | // Test touch down on primary display. |
| 4465 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4466 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4467 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4468 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4469 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4470 | |
| 4471 | // Test touch down on second display. |
| 4472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4473 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4474 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4475 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4476 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 4477 | |
| 4478 | // Trigger cancel touch. |
| 4479 | mDispatcher->cancelCurrentTouch(); |
| 4480 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4481 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4482 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4483 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4484 | |
| 4485 | // Test inject a move motion event, no window/monitor should receive the event. |
| 4486 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4487 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4488 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4489 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4490 | windowInPrimary->assertNoEvents(); |
| 4491 | monitorInPrimary.assertNoEvents(); |
| 4492 | |
| 4493 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4494 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4495 | SECOND_DISPLAY_ID, {110, 200})) |
| 4496 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4497 | windowInSecondary->assertNoEvents(); |
| 4498 | monitorInSecondary.assertNoEvents(); |
| 4499 | } |
| 4500 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4501 | class InputFilterTest : public InputDispatcherTest { |
| 4502 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4503 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 4504 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4505 | NotifyMotionArgs motionArgs; |
| 4506 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4507 | motionArgs = |
| 4508 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4509 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4510 | motionArgs = |
| 4511 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4512 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4513 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4514 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4515 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 4516 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4517 | } else { |
| 4518 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | void testNotifyKey(bool expectToBeFiltered) { |
| 4523 | NotifyKeyArgs keyArgs; |
| 4524 | |
| 4525 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4526 | mDispatcher->notifyKey(&keyArgs); |
| 4527 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 4528 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4529 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4530 | |
| 4531 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 4532 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4533 | } else { |
| 4534 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4535 | } |
| 4536 | } |
| 4537 | }; |
| 4538 | |
| 4539 | // Test InputFilter for MotionEvent |
| 4540 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 4541 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 4542 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4543 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4544 | |
| 4545 | // Enable InputFilter |
| 4546 | mDispatcher->setInputFilterEnabled(true); |
| 4547 | // Test touch on both primary and second display, and check if both events are filtered. |
| 4548 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 4549 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 4550 | |
| 4551 | // Disable InputFilter |
| 4552 | mDispatcher->setInputFilterEnabled(false); |
| 4553 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 4554 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4555 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4556 | } |
| 4557 | |
| 4558 | // Test InputFilter for KeyEvent |
| 4559 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 4560 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 4561 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4562 | |
| 4563 | // Enable InputFilter |
| 4564 | mDispatcher->setInputFilterEnabled(true); |
| 4565 | // Send a key event, and check if it is filtered. |
| 4566 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 4567 | |
| 4568 | // Disable InputFilter |
| 4569 | mDispatcher->setInputFilterEnabled(false); |
| 4570 | // Send a key event, and check if it isn't filtered. |
| 4571 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4572 | } |
| 4573 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4574 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 4575 | // logical display coordinate space. |
| 4576 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 4577 | ui::Transform firstDisplayTransform; |
| 4578 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4579 | ui::Transform secondDisplayTransform; |
| 4580 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 4581 | |
| 4582 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 4583 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 4584 | displayInfos[0].transform = firstDisplayTransform; |
| 4585 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 4586 | displayInfos[1].transform = secondDisplayTransform; |
| 4587 | |
| 4588 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 4589 | |
| 4590 | // Enable InputFilter |
| 4591 | mDispatcher->setInputFilterEnabled(true); |
| 4592 | |
| 4593 | // Ensure the correct transforms are used for the displays. |
| 4594 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 4595 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 4596 | } |
| 4597 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4598 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 4599 | protected: |
| 4600 | virtual void SetUp() override { |
| 4601 | InputDispatcherTest::SetUp(); |
| 4602 | |
| 4603 | /** |
| 4604 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 4605 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 4606 | * on. |
| 4607 | */ |
| 4608 | mDispatcher->setInputFilterEnabled(true); |
| 4609 | |
| 4610 | std::shared_ptr<InputApplicationHandle> application = |
| 4611 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4612 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 4613 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4614 | |
| 4615 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4616 | mWindow->setFocusable(true); |
| 4617 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4618 | setFocusedWindow(mWindow); |
| 4619 | mWindow->consumeFocusEvent(true); |
| 4620 | } |
| 4621 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4622 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4623 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4624 | KeyEvent event; |
| 4625 | |
| 4626 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4627 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 4628 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
| 4629 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); |
| 4630 | const int32_t additionalPolicyFlags = |
| 4631 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 4632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4633 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4634 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4635 | policyFlags | additionalPolicyFlags)); |
| 4636 | |
| 4637 | InputEvent* received = mWindow->consume(); |
| 4638 | ASSERT_NE(nullptr, received); |
| 4639 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4640 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 4641 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 4642 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 4643 | } |
| 4644 | |
| 4645 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4646 | int32_t flags) { |
| 4647 | MotionEvent event; |
| 4648 | PointerProperties pointerProperties[1]; |
| 4649 | PointerCoords pointerCoords[1]; |
| 4650 | pointerProperties[0].clear(); |
| 4651 | pointerProperties[0].id = 0; |
| 4652 | pointerCoords[0].clear(); |
| 4653 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 4654 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 4655 | |
| 4656 | ui::Transform identityTransform; |
| 4657 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4658 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 4659 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 4660 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 4661 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4662 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 4663 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4664 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 4665 | |
| 4666 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 4667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4668 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4669 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4670 | policyFlags | additionalPolicyFlags)); |
| 4671 | |
| 4672 | InputEvent* received = mWindow->consume(); |
| 4673 | ASSERT_NE(nullptr, received); |
| 4674 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 4675 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 4676 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 4677 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
| 4680 | private: |
| 4681 | sp<FakeWindowHandle> mWindow; |
| 4682 | }; |
| 4683 | |
| 4684 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4685 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 4686 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 4687 | // injected device id will be overwritten. |
| 4688 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4689 | 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4692 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4693 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4694 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4695 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4696 | } |
| 4697 | |
| 4698 | TEST_F(InputFilterInjectionPolicyTest, |
| 4699 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 4700 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
| 4701 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4702 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
| 4706 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4707 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4710 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4711 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4712 | InputDispatcherTest::SetUp(); |
| 4713 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4714 | std::shared_ptr<FakeApplicationHandle> application = |
| 4715 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4716 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4717 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4718 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4719 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4720 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4721 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4722 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4723 | |
| 4724 | // Set focused application. |
| 4725 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4726 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4727 | |
| 4728 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4729 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4730 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4731 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4732 | } |
| 4733 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4734 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4735 | InputDispatcherTest::TearDown(); |
| 4736 | |
| 4737 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4738 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | protected: |
| 4742 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4743 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4744 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4745 | }; |
| 4746 | |
| 4747 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4748 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 4749 | // the onPointerDownOutsideFocus callback. |
| 4750 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4751 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4752 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4753 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4754 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4755 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4756 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4757 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4758 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 4759 | } |
| 4760 | |
| 4761 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 4762 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 4763 | // onPointerDownOutsideFocus callback. |
| 4764 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4766 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4767 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4768 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4769 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4770 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4771 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 4775 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 4776 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4777 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4778 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4779 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4780 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4781 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4782 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4783 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 4787 | // DOWN on the window that already has focus. Ensure no window received the |
| 4788 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4789 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4790 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4791 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 4792 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4793 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 4794 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4795 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4796 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4797 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4798 | } |
| 4799 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 4800 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 4801 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 4802 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 4803 | const MotionEvent event = |
| 4804 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4805 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 4806 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
| 4807 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 4808 | .build(); |
| 4809 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 4810 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4811 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 4812 | |
| 4813 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 4814 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 4815 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 4816 | mUnfocusedWindow->assertNoEvents(); |
| 4817 | } |
| 4818 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4819 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 4820 | // windows that point to the same client token. |
| 4821 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 4822 | virtual void SetUp() override { |
| 4823 | InputDispatcherTest::SetUp(); |
| 4824 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4825 | std::shared_ptr<FakeApplicationHandle> application = |
| 4826 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4827 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 4828 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4829 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 4830 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4831 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 4832 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4833 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 4834 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4835 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | protected: |
| 4839 | sp<FakeWindowHandle> mWindow1; |
| 4840 | sp<FakeWindowHandle> mWindow2; |
| 4841 | |
| 4842 | // 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] | 4843 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 4844 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 4845 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 4849 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4850 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4851 | InputEvent* event = window->consume(); |
| 4852 | |
| 4853 | ASSERT_NE(nullptr, event) << name.c_str() |
| 4854 | << ": consumer should have returned non-NULL event."; |
| 4855 | |
| 4856 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 4857 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 4858 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 4859 | |
| 4860 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4861 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 4862 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4863 | |
| 4864 | for (size_t i = 0; i < points.size(); i++) { |
| 4865 | float expectedX = points[i].x; |
| 4866 | float expectedY = points[i].y; |
| 4867 | |
| 4868 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 4869 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 4870 | << ", got " << motionEvent.getX(i); |
| 4871 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 4872 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 4873 | << ", got " << motionEvent.getY(i); |
| 4874 | } |
| 4875 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4876 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4877 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4878 | std::vector<PointF> expectedPoints) { |
| 4879 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 4880 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 4881 | mDispatcher->notifyMotion(&motionArgs); |
| 4882 | |
| 4883 | // Always consume from window1 since it's the window that has the InputReceiver |
| 4884 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 4885 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4886 | }; |
| 4887 | |
| 4888 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 4889 | // Touch Window 1 |
| 4890 | PointF touchedPoint = {10, 10}; |
| 4891 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4892 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4893 | |
| 4894 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4895 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4896 | |
| 4897 | // Touch Window 2 |
| 4898 | touchedPoint = {150, 150}; |
| 4899 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4900 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4901 | } |
| 4902 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4903 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 4904 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4905 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4906 | |
| 4907 | // Touch Window 1 |
| 4908 | PointF touchedPoint = {10, 10}; |
| 4909 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4910 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4911 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4912 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4913 | |
| 4914 | // Touch Window 2 |
| 4915 | touchedPoint = {150, 150}; |
| 4916 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4917 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 4918 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4919 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4920 | // Update the transform so rotation is set |
| 4921 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4922 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 4923 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 4924 | } |
| 4925 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4926 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4927 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4928 | |
| 4929 | // Touch Window 1 |
| 4930 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4931 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4932 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4933 | |
| 4934 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4935 | touchedPoints.push_back(PointF{150, 150}); |
| 4936 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4937 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4938 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4939 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4940 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4941 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4942 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4943 | // Update the transform so rotation is set for Window 2 |
| 4944 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4945 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4946 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4949 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4950 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 4951 | |
| 4952 | // Touch Window 1 |
| 4953 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4954 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4955 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4956 | |
| 4957 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4958 | touchedPoints.push_back(PointF{150, 150}); |
| 4959 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4960 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4961 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4962 | |
| 4963 | // Move both windows |
| 4964 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4965 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4966 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4967 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4968 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4969 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4970 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4971 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4972 | expectedPoints.pop_back(); |
| 4973 | |
| 4974 | // Touch Window 2 |
| 4975 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 4976 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4977 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4978 | |
| 4979 | // Move both windows |
| 4980 | touchedPoints = {{20, 20}, {175, 175}}; |
| 4981 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 4982 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 4983 | |
| 4984 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 4988 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 4989 | |
| 4990 | // Touch Window 1 |
| 4991 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 4992 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4993 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4994 | |
| 4995 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 4996 | touchedPoints.push_back(PointF{150, 150}); |
| 4997 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 4998 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4999 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5000 | |
| 5001 | // Move both windows |
| 5002 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5003 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5004 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5005 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5006 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5009 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 5010 | virtual void SetUp() override { |
| 5011 | InputDispatcherTest::SetUp(); |
| 5012 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5013 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5014 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5015 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 5016 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5017 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5018 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5019 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5020 | |
| 5021 | // Set focused application. |
| 5022 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 5023 | |
| 5024 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5025 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5026 | mWindow->consumeFocusEvent(true); |
| 5027 | } |
| 5028 | |
| 5029 | virtual void TearDown() override { |
| 5030 | InputDispatcherTest::TearDown(); |
| 5031 | mWindow.clear(); |
| 5032 | } |
| 5033 | |
| 5034 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5035 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5036 | sp<FakeWindowHandle> mWindow; |
| 5037 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 5038 | |
| 5039 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5041 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5042 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5043 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5044 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5045 | WINDOW_LOCATION)); |
| 5046 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5047 | |
| 5048 | sp<FakeWindowHandle> addSpyWindow() { |
| 5049 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5050 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5051 | spy->setTrustedOverlay(true); |
| 5052 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5053 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5054 | spy->setDispatchingTimeout(30ms); |
| 5055 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 5056 | return spy; |
| 5057 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5058 | }; |
| 5059 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5060 | // Send a tap and respond, which should not cause an ANR. |
| 5061 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 5062 | tapOnWindow(); |
| 5063 | mWindow->consumeMotionDown(); |
| 5064 | mWindow->consumeMotionUp(); |
| 5065 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5066 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5067 | } |
| 5068 | |
| 5069 | // Send a regular key and respond, which should not cause an ANR. |
| 5070 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5071 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5072 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5073 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5074 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5075 | } |
| 5076 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5077 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 5078 | mWindow->setFocusable(false); |
| 5079 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5080 | mWindow->consumeFocusEvent(false); |
| 5081 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5082 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5083 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5084 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 5085 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5086 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5087 | // Key will not go to window because we have no focused window. |
| 5088 | // The 'no focused window' ANR timer should start instead. |
| 5089 | |
| 5090 | // Now, the focused application goes away. |
| 5091 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 5092 | // The key should get dropped and there should be no ANR. |
| 5093 | |
| 5094 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5095 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5096 | } |
| 5097 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5098 | // 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] | 5099 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 5100 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5101 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5102 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5103 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5104 | WINDOW_LOCATION)); |
| 5105 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5106 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 5107 | ASSERT_TRUE(sequenceNum); |
| 5108 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5109 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5110 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5111 | mWindow->finishEvent(*sequenceNum); |
| 5112 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 5113 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5114 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5115 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | // Send a key to the app and have the app not respond right away. |
| 5119 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 5120 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5121 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5122 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 5123 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5124 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5125 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5126 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5127 | } |
| 5128 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5129 | // We have a focused application, but no focused window |
| 5130 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5131 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5132 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5133 | mWindow->consumeFocusEvent(false); |
| 5134 | |
| 5135 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5136 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5137 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5138 | WINDOW_LOCATION)); |
| 5139 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 5140 | mDispatcher->waitForIdle(); |
| 5141 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5142 | |
| 5143 | // Once a focused event arrives, we get an ANR for this application |
| 5144 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5145 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5146 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5147 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5148 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5149 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5150 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5151 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5152 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5153 | } |
| 5154 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5155 | /** |
| 5156 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 5157 | * there will not be an ANR. |
| 5158 | */ |
| 5159 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 5160 | mWindow->setFocusable(false); |
| 5161 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5162 | mWindow->consumeFocusEvent(false); |
| 5163 | |
| 5164 | KeyEvent event; |
| 5165 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 5166 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 5167 | |
| 5168 | // Define a valid key down event that is stale (too old). |
| 5169 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 5170 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 5171 | AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime); |
| 5172 | |
| 5173 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 5174 | |
| 5175 | InputEventInjectionResult result = |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5176 | mDispatcher->injectInputEvent(&event, {} /* targetUid */, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5177 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 5178 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 5179 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 5180 | << "Injection should fail because the event is stale"; |
| 5181 | |
| 5182 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5183 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5184 | mWindow->assertNoEvents(); |
| 5185 | } |
| 5186 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5187 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5188 | // Make sure that we don't notify policy twice about the same ANR. |
| 5189 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5190 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5191 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5192 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5193 | |
| 5194 | // Once a focused event arrives, we get an ANR for this application |
| 5195 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5196 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5197 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5198 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5199 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5200 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5201 | const std::chrono::duration appTimeout = |
| 5202 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5203 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5204 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5205 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5206 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 5207 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5208 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5209 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 5210 | // '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] | 5211 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5212 | } |
| 5213 | |
| 5214 | // We have a focused application, but no focused window |
| 5215 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5216 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5217 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5218 | mWindow->consumeFocusEvent(false); |
| 5219 | |
| 5220 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5221 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5222 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5223 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5224 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5225 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5226 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5227 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5228 | |
| 5229 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5230 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5231 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5232 | mWindow->assertNoEvents(); |
| 5233 | } |
| 5234 | |
| 5235 | /** |
| 5236 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 5237 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 5238 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 5239 | * the ANR mechanism should still work. |
| 5240 | * |
| 5241 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 5242 | * DOWN event, while not responding on the second one. |
| 5243 | */ |
| 5244 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 5245 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5246 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5247 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5248 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5249 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5250 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5251 | |
| 5252 | // Now send ACTION_UP, with identical timestamp |
| 5253 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5254 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5255 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5256 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5257 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5258 | |
| 5259 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 5260 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5261 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5262 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5263 | } |
| 5264 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5265 | // A spy window can receive an ANR |
| 5266 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 5267 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 5268 | |
| 5269 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5270 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5271 | WINDOW_LOCATION)); |
| 5272 | mWindow->consumeMotionDown(); |
| 5273 | |
| 5274 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 5275 | ASSERT_TRUE(sequenceNum); |
| 5276 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5277 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5278 | |
| 5279 | spy->finishEvent(*sequenceNum); |
| 5280 | spy->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 5281 | 0 /*flags*/); |
| 5282 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5283 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | // 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] | 5287 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5288 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 5289 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5290 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5291 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5292 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5293 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5295 | |
| 5296 | // Stuck on the ACTION_UP |
| 5297 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5298 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5299 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5300 | // 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] | 5301 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5302 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5303 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5304 | |
| 5305 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5306 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5307 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5308 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5309 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5310 | } |
| 5311 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5312 | // 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] | 5313 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5314 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 5315 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5316 | |
| 5317 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5318 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5319 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5320 | |
| 5321 | mWindow->consumeMotionDown(); |
| 5322 | // Stuck on the ACTION_UP |
| 5323 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5324 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5325 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5326 | // 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] | 5327 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5328 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5329 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5330 | |
| 5331 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5332 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5333 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5334 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5335 | spy->assertNoEvents(); |
| 5336 | } |
| 5337 | |
| 5338 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 5339 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 5340 | |
| 5341 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5342 | |
| 5343 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5344 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5345 | WINDOW_LOCATION)); |
| 5346 | |
| 5347 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5348 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 5349 | ASSERT_TRUE(consumeSeq); |
| 5350 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5351 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5352 | |
| 5353 | monitor.finishEvent(*consumeSeq); |
| 5354 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5355 | |
| 5356 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5357 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5358 | } |
| 5359 | |
| 5360 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 5361 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 5362 | // get an ANR again. |
| 5363 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 5364 | // 2. consume all pending events (= queue becomes healthy again) |
| 5365 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 5366 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 5367 | tapOnWindow(); |
| 5368 | |
| 5369 | mWindow->consumeMotionDown(); |
| 5370 | // Block on ACTION_UP |
| 5371 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5372 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5373 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 5374 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5375 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5376 | mWindow->assertNoEvents(); |
| 5377 | |
| 5378 | tapOnWindow(); |
| 5379 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5380 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5381 | mWindow->consumeMotionUp(); |
| 5382 | |
| 5383 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5384 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5385 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5386 | mWindow->assertNoEvents(); |
| 5387 | } |
| 5388 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5389 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 5390 | // it. |
| 5391 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5392 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5393 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5394 | WINDOW_LOCATION)); |
| 5395 | |
| 5396 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5397 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5398 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5399 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 5400 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5401 | // 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] | 5402 | mWindow->consumeMotionDown(); |
| 5403 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 5404 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5405 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5406 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5407 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5408 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5409 | } |
| 5410 | |
| 5411 | /** |
| 5412 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5413 | * not to to the focused window until the motion is processed. |
| 5414 | * |
| 5415 | * Warning!!! |
| 5416 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5417 | * and the injection timeout that we specify when injecting the key. |
| 5418 | * We must have the injection timeout (10ms) be smaller than |
| 5419 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5420 | * |
| 5421 | * If that value changes, this test should also change. |
| 5422 | */ |
| 5423 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 5424 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5425 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5426 | |
| 5427 | tapOnWindow(); |
| 5428 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5429 | ASSERT_TRUE(downSequenceNum); |
| 5430 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5431 | ASSERT_TRUE(upSequenceNum); |
| 5432 | // Don't finish the events yet, and send a key |
| 5433 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 5434 | // window even if motions are still being processed. But because the injection timeout is short, |
| 5435 | // we will receive INJECTION_TIMED_OUT as the result. |
| 5436 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5437 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5438 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5439 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5440 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5441 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5442 | // and the key remains pending, waiting for the touch events to be processed |
| 5443 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5444 | ASSERT_FALSE(keySequenceNum); |
| 5445 | |
| 5446 | std::this_thread::sleep_for(500ms); |
| 5447 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 5448 | // to the focused window, even though we have not yet finished the motion event |
| 5449 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5450 | mWindow->finishEvent(*downSequenceNum); |
| 5451 | mWindow->finishEvent(*upSequenceNum); |
| 5452 | } |
| 5453 | |
| 5454 | /** |
| 5455 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5456 | * not go to the focused window until the motion is processed. |
| 5457 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 5458 | * focused window right away. |
| 5459 | */ |
| 5460 | TEST_F(InputDispatcherSingleWindowAnr, |
| 5461 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 5462 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5463 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5464 | |
| 5465 | tapOnWindow(); |
| 5466 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5467 | ASSERT_TRUE(downSequenceNum); |
| 5468 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5469 | ASSERT_TRUE(upSequenceNum); |
| 5470 | // Don't finish the events yet, and send a key |
| 5471 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5473 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5474 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5475 | // At this point, key is still pending, and should not be sent to the application yet. |
| 5476 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5477 | ASSERT_FALSE(keySequenceNum); |
| 5478 | |
| 5479 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 5480 | tapOnWindow(); |
| 5481 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 5482 | // the other events yet. We can finish events in any order. |
| 5483 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 5484 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 5485 | mWindow->consumeMotionDown(); |
| 5486 | mWindow->consumeMotionUp(); |
| 5487 | mWindow->assertNoEvents(); |
| 5488 | } |
| 5489 | |
| 5490 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 5491 | virtual void SetUp() override { |
| 5492 | InputDispatcherTest::SetUp(); |
| 5493 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5494 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5495 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5496 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 5497 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5498 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5499 | // 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] | 5500 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5501 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5502 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 5503 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5504 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5505 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5506 | |
| 5507 | // Set focused application. |
| 5508 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5509 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5510 | |
| 5511 | // Expect one focus window exist in display. |
| 5512 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5513 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5514 | mFocusedWindow->consumeFocusEvent(true); |
| 5515 | } |
| 5516 | |
| 5517 | virtual void TearDown() override { |
| 5518 | InputDispatcherTest::TearDown(); |
| 5519 | |
| 5520 | mUnfocusedWindow.clear(); |
| 5521 | mFocusedWindow.clear(); |
| 5522 | } |
| 5523 | |
| 5524 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5525 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5526 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 5527 | sp<FakeWindowHandle> mFocusedWindow; |
| 5528 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 5529 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 5530 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 5531 | |
| 5532 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 5533 | |
| 5534 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 5535 | |
| 5536 | private: |
| 5537 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5539 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5540 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5541 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5542 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5543 | location)); |
| 5544 | } |
| 5545 | }; |
| 5546 | |
| 5547 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 5548 | // should be ANR'd first. |
| 5549 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5551 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5552 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5553 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5554 | mFocusedWindow->consumeMotionDown(); |
| 5555 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5556 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5557 | // We consumed all events, so no ANR |
| 5558 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5559 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5560 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5561 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5562 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5563 | FOCUSED_WINDOW_LOCATION)); |
| 5564 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5565 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5566 | |
| 5567 | const std::chrono::duration timeout = |
| 5568 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5569 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5570 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 5571 | // sequence to make it consistent |
| 5572 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5573 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5574 | mFocusedWindow->consumeMotionDown(); |
| 5575 | // This cancel is generated because the connection was unresponsive |
| 5576 | mFocusedWindow->consumeMotionCancel(); |
| 5577 | mFocusedWindow->assertNoEvents(); |
| 5578 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5579 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5580 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5581 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5582 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 5586 | // it doesn't matter which order they should have ANR. |
| 5587 | // But we should receive ANR for both. |
| 5588 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 5589 | // Set the timeout for unfocused window to match the focused window |
| 5590 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 5591 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5592 | |
| 5593 | tapOnFocusedWindow(); |
| 5594 | // 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] | 5595 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 5596 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 5597 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5598 | |
| 5599 | // 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] | 5600 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 5601 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 5602 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 5603 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5604 | |
| 5605 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5606 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5607 | |
| 5608 | mFocusedWindow->consumeMotionDown(); |
| 5609 | mFocusedWindow->consumeMotionUp(); |
| 5610 | mUnfocusedWindow->consumeMotionOutside(); |
| 5611 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5612 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 5613 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 5614 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5615 | |
| 5616 | // Both applications should be marked as responsive, in any order |
| 5617 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 5618 | mFocusedWindow->getToken() == responsiveToken2); |
| 5619 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 5620 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 5621 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 5625 | // We should also log an error to account for the dropped event (not tested here). |
| 5626 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 5627 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 5628 | tapOnFocusedWindow(); |
| 5629 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5630 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5631 | // Receive the events, but don't respond |
| 5632 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 5633 | ASSERT_TRUE(downEventSequenceNum); |
| 5634 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 5635 | ASSERT_TRUE(upEventSequenceNum); |
| 5636 | const std::chrono::duration timeout = |
| 5637 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5638 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5639 | |
| 5640 | // Tap once again |
| 5641 | // 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] | 5642 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5643 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5644 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5645 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5646 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5647 | FOCUSED_WINDOW_LOCATION)); |
| 5648 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 5649 | // valid touch target |
| 5650 | mUnfocusedWindow->assertNoEvents(); |
| 5651 | |
| 5652 | // Consume the first tap |
| 5653 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 5654 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 5655 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5656 | // The second tap did not go to the focused window |
| 5657 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5658 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5659 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5660 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5661 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5662 | } |
| 5663 | |
| 5664 | // If you tap outside of all windows, there will not be ANR |
| 5665 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5666 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5667 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5668 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 5669 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5670 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5671 | } |
| 5672 | |
| 5673 | // Since the focused window is paused, tapping on it should not produce any events |
| 5674 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 5675 | mFocusedWindow->setPaused(true); |
| 5676 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5677 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5678 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5679 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5680 | FOCUSED_WINDOW_LOCATION)); |
| 5681 | |
| 5682 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 5683 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5684 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 5685 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5686 | |
| 5687 | mFocusedWindow->assertNoEvents(); |
| 5688 | mUnfocusedWindow->assertNoEvents(); |
| 5689 | } |
| 5690 | |
| 5691 | /** |
| 5692 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5693 | * not to to the focused window until the motion is processed. |
| 5694 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 5695 | * |
| 5696 | * Warning!!! |
| 5697 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5698 | * and the injection timeout that we specify when injecting the key. |
| 5699 | * We must have the injection timeout (10ms) be smaller than |
| 5700 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5701 | * |
| 5702 | * If that value changes, this test should also change. |
| 5703 | */ |
| 5704 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 5705 | // Set a long ANR timeout to prevent it from triggering |
| 5706 | mFocusedWindow->setDispatchingTimeout(2s); |
| 5707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5708 | |
| 5709 | tapOnUnfocusedWindow(); |
| 5710 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5711 | ASSERT_TRUE(downSequenceNum); |
| 5712 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5713 | ASSERT_TRUE(upSequenceNum); |
| 5714 | // Don't finish the events yet, and send a key |
| 5715 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 5716 | // window even if motions are still being processed. |
| 5717 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5718 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5719 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5720 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 5721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5722 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5723 | // and the key remains pending, waiting for the touch events to be processed |
| 5724 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 5725 | ASSERT_FALSE(keySequenceNum); |
| 5726 | |
| 5727 | // 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] | 5728 | mFocusedWindow->setFocusable(false); |
| 5729 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5730 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5731 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5732 | |
| 5733 | // Focus events should precede the key events |
| 5734 | mUnfocusedWindow->consumeFocusEvent(true); |
| 5735 | mFocusedWindow->consumeFocusEvent(false); |
| 5736 | |
| 5737 | // Finish the tap events, which should unblock dispatcher |
| 5738 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 5739 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 5740 | |
| 5741 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 5742 | // can finally go to the newly focused "mUnfocusedWindow". |
| 5743 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5744 | mFocusedWindow->assertNoEvents(); |
| 5745 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5746 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 5750 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 5751 | // The other window should not be affected by that. |
| 5752 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 5753 | // Touch Window 1 |
| 5754 | NotifyMotionArgs motionArgs = |
| 5755 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5756 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 5757 | mDispatcher->notifyMotion(&motionArgs); |
| 5758 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5759 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5760 | |
| 5761 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5762 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5763 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5764 | mDispatcher->notifyMotion(&motionArgs); |
| 5765 | |
| 5766 | const std::chrono::duration timeout = |
| 5767 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5768 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5769 | |
| 5770 | mUnfocusedWindow->consumeMotionDown(); |
| 5771 | mFocusedWindow->consumeMotionDown(); |
| 5772 | // Focused window may or may not receive ACTION_MOVE |
| 5773 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 5774 | InputEvent* event; |
| 5775 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 5776 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 5777 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 5778 | ASSERT_NE(nullptr, event); |
| 5779 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 5780 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 5781 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 5782 | mFocusedWindow->consumeMotionCancel(); |
| 5783 | } else { |
| 5784 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 5785 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5786 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5787 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5788 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5789 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5790 | mUnfocusedWindow->assertNoEvents(); |
| 5791 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5792 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5793 | } |
| 5794 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5795 | /** |
| 5796 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 5797 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 5798 | * |
| 5799 | * If the user touches another application during this time, the key should be dropped. |
| 5800 | * Next, if a new focused window comes in, without toggling the focused application, |
| 5801 | * then no ANR should occur. |
| 5802 | * |
| 5803 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 5804 | * but in some cases the policy may not update the focused application. |
| 5805 | */ |
| 5806 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 5807 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 5808 | std::make_shared<FakeApplicationHandle>(); |
| 5809 | focusedApplication->setDispatchingTimeout(60ms); |
| 5810 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 5811 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 5812 | mFocusedWindow->setFocusable(false); |
| 5813 | |
| 5814 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5815 | mFocusedWindow->consumeFocusEvent(false); |
| 5816 | |
| 5817 | // Send a key. The ANR timer should start because there is no focused window. |
| 5818 | // 'focusedApplication' will get blamed if this timer completes. |
| 5819 | // 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] | 5820 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5821 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5822 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 5823 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 5825 | |
| 5826 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 5827 | // then the injected touches won't cause the focused event to get dropped. |
| 5828 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 5829 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 5830 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 5831 | // For this test, it means that the key would get delivered to the window once it becomes |
| 5832 | // focused. |
| 5833 | std::this_thread::sleep_for(10ms); |
| 5834 | |
| 5835 | // Touch unfocused window. This should force the pending key to get dropped. |
| 5836 | NotifyMotionArgs motionArgs = |
| 5837 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5838 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 5839 | mDispatcher->notifyMotion(&motionArgs); |
| 5840 | |
| 5841 | // We do not consume the motion right away, because that would require dispatcher to first |
| 5842 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 5843 | // Set the focused window first. |
| 5844 | mFocusedWindow->setFocusable(true); |
| 5845 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5846 | setFocusedWindow(mFocusedWindow); |
| 5847 | mFocusedWindow->consumeFocusEvent(true); |
| 5848 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 5849 | // to another application. This could be a bug / behaviour in the policy. |
| 5850 | |
| 5851 | mUnfocusedWindow->consumeMotionDown(); |
| 5852 | |
| 5853 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5854 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 5855 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 5856 | } |
| 5857 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5858 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 5859 | // that has feature NO_INPUT_CHANNEL. |
| 5860 | // Layout: |
| 5861 | // Top (closest to user) |
| 5862 | // mNoInputWindow (above all windows) |
| 5863 | // mBottomWindow |
| 5864 | // Bottom (furthest from user) |
| 5865 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 5866 | virtual void SetUp() override { |
| 5867 | InputDispatcherTest::SetUp(); |
| 5868 | |
| 5869 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5870 | mNoInputWindow = |
| 5871 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 5872 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 5873 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5874 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5875 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5876 | // It's perfectly valid for this window to not have an associated input channel |
| 5877 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5878 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 5879 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5880 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5881 | |
| 5882 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5883 | } |
| 5884 | |
| 5885 | protected: |
| 5886 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 5887 | sp<FakeWindowHandle> mNoInputWindow; |
| 5888 | sp<FakeWindowHandle> mBottomWindow; |
| 5889 | }; |
| 5890 | |
| 5891 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 5892 | PointF touchedPoint = {10, 10}; |
| 5893 | |
| 5894 | NotifyMotionArgs motionArgs = |
| 5895 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5896 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5897 | mDispatcher->notifyMotion(&motionArgs); |
| 5898 | |
| 5899 | mNoInputWindow->assertNoEvents(); |
| 5900 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 5901 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 5902 | // and therefore should prevent mBottomWindow from receiving touches |
| 5903 | mBottomWindow->assertNoEvents(); |
| 5904 | } |
| 5905 | |
| 5906 | /** |
| 5907 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 5908 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 5909 | */ |
| 5910 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 5911 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5912 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 5913 | "Window with input channel and NO_INPUT_CHANNEL", |
| 5914 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5915 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5916 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 5917 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5918 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 5919 | |
| 5920 | PointF touchedPoint = {10, 10}; |
| 5921 | |
| 5922 | NotifyMotionArgs motionArgs = |
| 5923 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5924 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 5925 | mDispatcher->notifyMotion(&motionArgs); |
| 5926 | |
| 5927 | mNoInputWindow->assertNoEvents(); |
| 5928 | mBottomWindow->assertNoEvents(); |
| 5929 | } |
| 5930 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5931 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 5932 | protected: |
| 5933 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 5934 | sp<FakeWindowHandle> mWindow; |
| 5935 | sp<FakeWindowHandle> mMirror; |
| 5936 | |
| 5937 | virtual void SetUp() override { |
| 5938 | InputDispatcherTest::SetUp(); |
| 5939 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5940 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 5941 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 5942 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5943 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 5944 | mWindow->setFocusable(true); |
| 5945 | mMirror->setFocusable(true); |
| 5946 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5947 | } |
| 5948 | }; |
| 5949 | |
| 5950 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 5951 | // Request focus on a mirrored window |
| 5952 | setFocusedWindow(mMirror); |
| 5953 | |
| 5954 | // window gets focused |
| 5955 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5956 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5957 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5958 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5959 | } |
| 5960 | |
| 5961 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 5962 | // focusable. |
| 5963 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 5964 | setFocusedWindow(mMirror); |
| 5965 | |
| 5966 | // window gets focused |
| 5967 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5968 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5969 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5970 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5971 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5972 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5973 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5974 | |
| 5975 | mMirror->setFocusable(false); |
| 5976 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 5977 | |
| 5978 | // window loses focus since one of the windows associated with the token in not focusable |
| 5979 | mWindow->consumeFocusEvent(false); |
| 5980 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5981 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5982 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5983 | mWindow->assertNoEvents(); |
| 5984 | } |
| 5985 | |
| 5986 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 5987 | // invisible. |
| 5988 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 5989 | setFocusedWindow(mMirror); |
| 5990 | |
| 5991 | // window gets focused |
| 5992 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5993 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5994 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5995 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5996 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 5997 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5998 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 5999 | |
| 6000 | mMirror->setVisible(false); |
| 6001 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6002 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6003 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6004 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6005 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6006 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6007 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6008 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6009 | |
| 6010 | mWindow->setVisible(false); |
| 6011 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6012 | |
| 6013 | // window loses focus only after all windows associated with the token become invisible. |
| 6014 | mWindow->consumeFocusEvent(false); |
| 6015 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6016 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6017 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6018 | mWindow->assertNoEvents(); |
| 6019 | } |
| 6020 | |
| 6021 | // A focused & mirrored window remains focused until both windows are removed. |
| 6022 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 6023 | setFocusedWindow(mMirror); |
| 6024 | |
| 6025 | // window gets focused |
| 6026 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6028 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6029 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6031 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6032 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6033 | |
| 6034 | // single window is removed but the window token remains focused |
| 6035 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 6036 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6037 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6038 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6039 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6041 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6042 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6043 | |
| 6044 | // Both windows are removed |
| 6045 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 6046 | mWindow->consumeFocusEvent(false); |
| 6047 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6048 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6049 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6050 | mWindow->assertNoEvents(); |
| 6051 | } |
| 6052 | |
| 6053 | // Focus request can be pending until one window becomes visible. |
| 6054 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 6055 | // Request focus on an invisible mirror. |
| 6056 | mWindow->setVisible(false); |
| 6057 | mMirror->setVisible(false); |
| 6058 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6059 | setFocusedWindow(mMirror); |
| 6060 | |
| 6061 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6062 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6063 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6064 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6065 | |
| 6066 | mMirror->setVisible(true); |
| 6067 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6068 | |
| 6069 | // window gets focused |
| 6070 | mWindow->consumeFocusEvent(true); |
| 6071 | // window gets the pending key event |
| 6072 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6073 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6074 | |
| 6075 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 6076 | protected: |
| 6077 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6078 | sp<FakeWindowHandle> mWindow; |
| 6079 | sp<FakeWindowHandle> mSecondWindow; |
| 6080 | |
| 6081 | void SetUp() override { |
| 6082 | InputDispatcherTest::SetUp(); |
| 6083 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6084 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6085 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6086 | mSecondWindow = |
| 6087 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6088 | mSecondWindow->setFocusable(true); |
| 6089 | |
| 6090 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6091 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 6092 | |
| 6093 | setFocusedWindow(mWindow); |
| 6094 | mWindow->consumeFocusEvent(true); |
| 6095 | } |
| 6096 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6097 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 6098 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6099 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 6100 | } |
| 6101 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6102 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 6103 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6104 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6105 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 6106 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6107 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6108 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6109 | } |
| 6110 | }; |
| 6111 | |
| 6112 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 6113 | // Ensure that capture cannot be obtained for unfocused windows. |
| 6114 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 6115 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6116 | mSecondWindow->assertNoEvents(); |
| 6117 | |
| 6118 | // Ensure that capture can be enabled from the focus window. |
| 6119 | requestAndVerifyPointerCapture(mWindow, true); |
| 6120 | |
| 6121 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 6122 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 6123 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6124 | |
| 6125 | // Ensure that capture can be disabled from the window with capture. |
| 6126 | requestAndVerifyPointerCapture(mWindow, false); |
| 6127 | } |
| 6128 | |
| 6129 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6130 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6131 | |
| 6132 | setFocusedWindow(mSecondWindow); |
| 6133 | |
| 6134 | // Ensure that the capture disabled event was sent first. |
| 6135 | mWindow->consumeCaptureEvent(false); |
| 6136 | mWindow->consumeFocusEvent(false); |
| 6137 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6138 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6139 | |
| 6140 | // 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] | 6141 | notifyPointerCaptureChanged({}); |
| 6142 | notifyPointerCaptureChanged(request); |
| 6143 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6144 | mWindow->assertNoEvents(); |
| 6145 | mSecondWindow->assertNoEvents(); |
| 6146 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6147 | } |
| 6148 | |
| 6149 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6150 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6151 | |
| 6152 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6153 | notifyPointerCaptureChanged({}); |
| 6154 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6155 | |
| 6156 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6157 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6158 | mWindow->consumeCaptureEvent(false); |
| 6159 | mWindow->assertNoEvents(); |
| 6160 | } |
| 6161 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6162 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 6163 | requestAndVerifyPointerCapture(mWindow, true); |
| 6164 | |
| 6165 | // The first window loses focus. |
| 6166 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6167 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6168 | mWindow->consumeCaptureEvent(false); |
| 6169 | |
| 6170 | // Request Pointer Capture from the second window before the notification from InputReader |
| 6171 | // arrives. |
| 6172 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6173 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6174 | |
| 6175 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6176 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6177 | |
| 6178 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6179 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6180 | |
| 6181 | mSecondWindow->consumeFocusEvent(true); |
| 6182 | mSecondWindow->consumeCaptureEvent(true); |
| 6183 | } |
| 6184 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6185 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 6186 | // App repeatedly enables and disables capture. |
| 6187 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6188 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6189 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6190 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6191 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6192 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6193 | |
| 6194 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 6195 | // first request is now stale, this should do nothing. |
| 6196 | notifyPointerCaptureChanged(firstRequest); |
| 6197 | mWindow->assertNoEvents(); |
| 6198 | |
| 6199 | // InputReader notifies that the second request was enabled. |
| 6200 | notifyPointerCaptureChanged(secondRequest); |
| 6201 | mWindow->consumeCaptureEvent(true); |
| 6202 | } |
| 6203 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 6204 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 6205 | requestAndVerifyPointerCapture(mWindow, true); |
| 6206 | |
| 6207 | // App toggles pointer capture off and on. |
| 6208 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6209 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6210 | |
| 6211 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6212 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6213 | |
| 6214 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 6215 | // preceding "disable" request. |
| 6216 | notifyPointerCaptureChanged(enableRequest); |
| 6217 | |
| 6218 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 6219 | // any notifications. |
| 6220 | mWindow->assertNoEvents(); |
| 6221 | } |
| 6222 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6223 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 6224 | protected: |
| 6225 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6226 | |
| 6227 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 6228 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 6229 | |
| 6230 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 6231 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6232 | |
| 6233 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 6234 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 6235 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6236 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 6237 | MAXIMUM_OBSCURING_OPACITY); |
| 6238 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6239 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6240 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6241 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6242 | |
| 6243 | sp<FakeWindowHandle> mTouchWindow; |
| 6244 | |
| 6245 | virtual void SetUp() override { |
| 6246 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6247 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6248 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 6249 | } |
| 6250 | |
| 6251 | virtual void TearDown() override { |
| 6252 | InputDispatcherTest::TearDown(); |
| 6253 | mTouchWindow.clear(); |
| 6254 | } |
| 6255 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6256 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 6257 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6258 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6259 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6260 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6261 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6262 | return window; |
| 6263 | } |
| 6264 | |
| 6265 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 6266 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 6267 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6268 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6269 | // Generate an arbitrary PID based on the UID |
| 6270 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 6271 | return window; |
| 6272 | } |
| 6273 | |
| 6274 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 6275 | NotifyMotionArgs args = |
| 6276 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6277 | ADISPLAY_ID_DEFAULT, points); |
| 6278 | mDispatcher->notifyMotion(&args); |
| 6279 | } |
| 6280 | }; |
| 6281 | |
| 6282 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6283 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6284 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6285 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6286 | |
| 6287 | touch(); |
| 6288 | |
| 6289 | mTouchWindow->assertNoEvents(); |
| 6290 | } |
| 6291 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6292 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6293 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 6294 | const sp<FakeWindowHandle>& w = |
| 6295 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 6296 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6297 | |
| 6298 | touch(); |
| 6299 | |
| 6300 | mTouchWindow->assertNoEvents(); |
| 6301 | } |
| 6302 | |
| 6303 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6304 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 6305 | const sp<FakeWindowHandle>& w = |
| 6306 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6307 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6308 | |
| 6309 | touch(); |
| 6310 | |
| 6311 | w->assertNoEvents(); |
| 6312 | } |
| 6313 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6314 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6315 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 6316 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6317 | |
| 6318 | touch(); |
| 6319 | |
| 6320 | mTouchWindow->consumeAnyMotionDown(); |
| 6321 | } |
| 6322 | |
| 6323 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6324 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6325 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6326 | w->setFrame(Rect(0, 0, 50, 50)); |
| 6327 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6328 | |
| 6329 | touch({PointF{100, 100}}); |
| 6330 | |
| 6331 | mTouchWindow->consumeAnyMotionDown(); |
| 6332 | } |
| 6333 | |
| 6334 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6335 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6336 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6337 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6338 | |
| 6339 | touch(); |
| 6340 | |
| 6341 | mTouchWindow->consumeAnyMotionDown(); |
| 6342 | } |
| 6343 | |
| 6344 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 6345 | const sp<FakeWindowHandle>& w = |
| 6346 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6347 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6348 | |
| 6349 | touch(); |
| 6350 | |
| 6351 | mTouchWindow->consumeAnyMotionDown(); |
| 6352 | } |
| 6353 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6354 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 6355 | const sp<FakeWindowHandle>& w = |
| 6356 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6357 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6358 | |
| 6359 | touch(); |
| 6360 | |
| 6361 | w->assertNoEvents(); |
| 6362 | } |
| 6363 | |
| 6364 | /** |
| 6365 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 6366 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 6367 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 6368 | */ |
| 6369 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6370 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 6371 | const sp<FakeWindowHandle>& w = |
| 6372 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6373 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6374 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6375 | |
| 6376 | touch(); |
| 6377 | |
| 6378 | w->consumeMotionOutside(); |
| 6379 | } |
| 6380 | |
| 6381 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 6382 | const sp<FakeWindowHandle>& w = |
| 6383 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6384 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6385 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6386 | |
| 6387 | touch(); |
| 6388 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6389 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6392 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6393 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6394 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6395 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6396 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6397 | |
| 6398 | touch(); |
| 6399 | |
| 6400 | mTouchWindow->consumeAnyMotionDown(); |
| 6401 | } |
| 6402 | |
| 6403 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 6404 | const sp<FakeWindowHandle>& w = |
| 6405 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6406 | MAXIMUM_OBSCURING_OPACITY); |
| 6407 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6408 | |
| 6409 | touch(); |
| 6410 | |
| 6411 | mTouchWindow->consumeAnyMotionDown(); |
| 6412 | } |
| 6413 | |
| 6414 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6415 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6416 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6417 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6418 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6419 | |
| 6420 | touch(); |
| 6421 | |
| 6422 | mTouchWindow->assertNoEvents(); |
| 6423 | } |
| 6424 | |
| 6425 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 6426 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 6427 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6428 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6429 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6430 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6431 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6432 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6433 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6434 | |
| 6435 | touch(); |
| 6436 | |
| 6437 | mTouchWindow->assertNoEvents(); |
| 6438 | } |
| 6439 | |
| 6440 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 6441 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 6442 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6443 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6444 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6445 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6446 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6447 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6448 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6449 | |
| 6450 | touch(); |
| 6451 | |
| 6452 | mTouchWindow->consumeAnyMotionDown(); |
| 6453 | } |
| 6454 | |
| 6455 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6456 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 6457 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6458 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6459 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6460 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6461 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6462 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6463 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6464 | |
| 6465 | touch(); |
| 6466 | |
| 6467 | mTouchWindow->consumeAnyMotionDown(); |
| 6468 | } |
| 6469 | |
| 6470 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 6471 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6472 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6473 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6474 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6475 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6476 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6477 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6478 | |
| 6479 | touch(); |
| 6480 | |
| 6481 | mTouchWindow->assertNoEvents(); |
| 6482 | } |
| 6483 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6484 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6485 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 6486 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6487 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6488 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6489 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6490 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6491 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6492 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6493 | |
| 6494 | touch(); |
| 6495 | |
| 6496 | mTouchWindow->assertNoEvents(); |
| 6497 | } |
| 6498 | |
| 6499 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6500 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 6501 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6502 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6503 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6504 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6505 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6506 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6507 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6508 | |
| 6509 | touch(); |
| 6510 | |
| 6511 | mTouchWindow->consumeAnyMotionDown(); |
| 6512 | } |
| 6513 | |
| 6514 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 6515 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6516 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6517 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6518 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6519 | |
| 6520 | touch(); |
| 6521 | |
| 6522 | mTouchWindow->consumeAnyMotionDown(); |
| 6523 | } |
| 6524 | |
| 6525 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 6526 | const sp<FakeWindowHandle>& w = |
| 6527 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6528 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6529 | |
| 6530 | touch(); |
| 6531 | |
| 6532 | mTouchWindow->consumeAnyMotionDown(); |
| 6533 | } |
| 6534 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6535 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6536 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 6537 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6538 | const sp<FakeWindowHandle>& w = |
| 6539 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 6540 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6541 | |
| 6542 | touch(); |
| 6543 | |
| 6544 | mTouchWindow->assertNoEvents(); |
| 6545 | } |
| 6546 | |
| 6547 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 6548 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6549 | const sp<FakeWindowHandle>& w = |
| 6550 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 6551 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6552 | |
| 6553 | touch(); |
| 6554 | |
| 6555 | mTouchWindow->consumeAnyMotionDown(); |
| 6556 | } |
| 6557 | |
| 6558 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6559 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 6560 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 6561 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6562 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6563 | OPACITY_ABOVE_THRESHOLD); |
| 6564 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6565 | |
| 6566 | touch(); |
| 6567 | |
| 6568 | mTouchWindow->consumeAnyMotionDown(); |
| 6569 | } |
| 6570 | |
| 6571 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6572 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 6573 | const sp<FakeWindowHandle>& w1 = |
| 6574 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6575 | OPACITY_BELOW_THRESHOLD); |
| 6576 | const sp<FakeWindowHandle>& w2 = |
| 6577 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6578 | OPACITY_BELOW_THRESHOLD); |
| 6579 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6580 | |
| 6581 | touch(); |
| 6582 | |
| 6583 | mTouchWindow->assertNoEvents(); |
| 6584 | } |
| 6585 | |
| 6586 | /** |
| 6587 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 6588 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 6589 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 6590 | */ |
| 6591 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6592 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 6593 | const sp<FakeWindowHandle>& wB = |
| 6594 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6595 | OPACITY_BELOW_THRESHOLD); |
| 6596 | const sp<FakeWindowHandle>& wC = |
| 6597 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6598 | OPACITY_BELOW_THRESHOLD); |
| 6599 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6600 | |
| 6601 | touch(); |
| 6602 | |
| 6603 | mTouchWindow->assertNoEvents(); |
| 6604 | } |
| 6605 | |
| 6606 | /** |
| 6607 | * This test is testing that a window from a different UID but with same application token doesn't |
| 6608 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 6609 | */ |
| 6610 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6611 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 6612 | const sp<FakeWindowHandle>& w = |
| 6613 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6614 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6615 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6616 | |
| 6617 | touch(); |
| 6618 | |
| 6619 | mTouchWindow->consumeAnyMotionDown(); |
| 6620 | } |
| 6621 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6622 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 6623 | protected: |
| 6624 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6625 | sp<FakeWindowHandle> mWindow; |
| 6626 | sp<FakeWindowHandle> mSecondWindow; |
| 6627 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6628 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6629 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 6630 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6631 | |
| 6632 | void SetUp() override { |
| 6633 | InputDispatcherTest::SetUp(); |
| 6634 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6635 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6636 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6637 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6638 | mSecondWindow = |
| 6639 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6640 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6641 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6642 | mSpyWindow = |
| 6643 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6644 | mSpyWindow->setSpy(true); |
| 6645 | mSpyWindow->setTrustedOverlay(true); |
| 6646 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 6647 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6648 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6649 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6650 | } |
| 6651 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6652 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 6653 | switch (fromSource) { |
| 6654 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 6655 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6656 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6657 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6658 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6659 | break; |
| 6660 | case AINPUT_SOURCE_STYLUS: |
| 6661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6662 | injectMotionEvent( |
| 6663 | mDispatcher, |
| 6664 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6665 | AINPUT_SOURCE_STYLUS) |
| 6666 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6667 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6668 | .x(50) |
| 6669 | .y(50)) |
| 6670 | .build())); |
| 6671 | break; |
| 6672 | case AINPUT_SOURCE_MOUSE: |
| 6673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6674 | injectMotionEvent( |
| 6675 | mDispatcher, |
| 6676 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6677 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 6678 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6679 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6680 | .x(50) |
| 6681 | .y(50)) |
| 6682 | .build())); |
| 6683 | break; |
| 6684 | default: |
| 6685 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 6686 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6687 | |
| 6688 | // Window should receive motion event. |
| 6689 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6690 | // Spy window should also receive motion event |
| 6691 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
| 6694 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 6695 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 6696 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6697 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6698 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6699 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6700 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6701 | |
| 6702 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6703 | mDragWindow = |
| 6704 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6705 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6706 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6707 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6708 | |
| 6709 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6710 | bool transferred = |
| 6711 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 6712 | true /* isDragDrop */); |
| 6713 | if (transferred) { |
| 6714 | mWindow->consumeMotionCancel(); |
| 6715 | mDragWindow->consumeMotionDown(); |
| 6716 | } |
| 6717 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6718 | } |
| 6719 | }; |
| 6720 | |
| 6721 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6722 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6723 | |
| 6724 | // Move on window. |
| 6725 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6726 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6727 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6728 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6729 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6730 | mWindow->consumeDragEvent(false, 50, 50); |
| 6731 | mSecondWindow->assertNoEvents(); |
| 6732 | |
| 6733 | // Move to another window. |
| 6734 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6735 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6736 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6737 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6738 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6739 | mWindow->consumeDragEvent(true, 150, 50); |
| 6740 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6741 | |
| 6742 | // Move back to original window. |
| 6743 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6744 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6745 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6746 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6747 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6748 | mWindow->consumeDragEvent(false, 50, 50); |
| 6749 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 6750 | |
| 6751 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6752 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6753 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6754 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6755 | mWindow->assertNoEvents(); |
| 6756 | mSecondWindow->assertNoEvents(); |
| 6757 | } |
| 6758 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6759 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6760 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6761 | |
| 6762 | // No cancel event after drag start |
| 6763 | mSpyWindow->assertNoEvents(); |
| 6764 | |
| 6765 | const MotionEvent secondFingerDownEvent = |
| 6766 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6767 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6768 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6769 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60)) |
| 6770 | .build(); |
| 6771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6772 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6773 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6774 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6775 | |
| 6776 | // Receives cancel for first pointer after next pointer down |
| 6777 | mSpyWindow->consumeMotionCancel(); |
| 6778 | mSpyWindow->consumeMotionDown(); |
| 6779 | |
| 6780 | mSpyWindow->assertNoEvents(); |
| 6781 | } |
| 6782 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6783 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6784 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 6785 | |
| 6786 | // Move on window. |
| 6787 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6788 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6789 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6790 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6791 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6792 | mWindow->consumeDragEvent(false, 50, 50); |
| 6793 | mSecondWindow->assertNoEvents(); |
| 6794 | |
| 6795 | // Move to another window. |
| 6796 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6797 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6798 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6799 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6800 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6801 | mWindow->consumeDragEvent(true, 150, 50); |
| 6802 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 6803 | |
| 6804 | // drop to another window. |
| 6805 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6806 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6807 | {150, 50})) |
| 6808 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6809 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6810 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6811 | mWindow->assertNoEvents(); |
| 6812 | mSecondWindow->assertNoEvents(); |
| 6813 | } |
| 6814 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6815 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6816 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 6817 | |
| 6818 | // Move on window and keep button pressed. |
| 6819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6820 | injectMotionEvent(mDispatcher, |
| 6821 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 6822 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6823 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6824 | .x(50) |
| 6825 | .y(50)) |
| 6826 | .build())) |
| 6827 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6828 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6829 | mWindow->consumeDragEvent(false, 50, 50); |
| 6830 | mSecondWindow->assertNoEvents(); |
| 6831 | |
| 6832 | // Move to another window and release button, expect to drop item. |
| 6833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6834 | injectMotionEvent(mDispatcher, |
| 6835 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 6836 | .buttonState(0) |
| 6837 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6838 | .x(150) |
| 6839 | .y(50)) |
| 6840 | .build())) |
| 6841 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6842 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6843 | mWindow->assertNoEvents(); |
| 6844 | mSecondWindow->assertNoEvents(); |
| 6845 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 6846 | |
| 6847 | // nothing to the window. |
| 6848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6849 | injectMotionEvent(mDispatcher, |
| 6850 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 6851 | .buttonState(0) |
| 6852 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6853 | .x(150) |
| 6854 | .y(50)) |
| 6855 | .build())) |
| 6856 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6857 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6858 | mWindow->assertNoEvents(); |
| 6859 | mSecondWindow->assertNoEvents(); |
| 6860 | } |
| 6861 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6862 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6863 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 6864 | |
| 6865 | // Set second window invisible. |
| 6866 | mSecondWindow->setVisible(false); |
| 6867 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 6868 | |
| 6869 | // Move on window. |
| 6870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6871 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6872 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6873 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6874 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6875 | mWindow->consumeDragEvent(false, 50, 50); |
| 6876 | mSecondWindow->assertNoEvents(); |
| 6877 | |
| 6878 | // Move to another window. |
| 6879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6880 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6881 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 6882 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6883 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6884 | mWindow->consumeDragEvent(true, 150, 50); |
| 6885 | mSecondWindow->assertNoEvents(); |
| 6886 | |
| 6887 | // drop to another window. |
| 6888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6889 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6890 | {150, 50})) |
| 6891 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6892 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6893 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 6894 | mWindow->assertNoEvents(); |
| 6895 | mSecondWindow->assertNoEvents(); |
| 6896 | } |
| 6897 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6898 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6899 | // Ensure window could track pointerIds if it didn't support split touch. |
| 6900 | mWindow->setPreventSplitting(true); |
| 6901 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6903 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6904 | {50, 50})) |
| 6905 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6906 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6907 | |
| 6908 | const MotionEvent secondFingerDownEvent = |
| 6909 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6910 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6911 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6912 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6913 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50)) |
| 6914 | .build(); |
| 6915 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6916 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6917 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6918 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6919 | mWindow->consumeMotionPointerDown(1 /* pointerIndex */); |
| 6920 | |
| 6921 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6922 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
| 6925 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 6926 | // First down on second window. |
| 6927 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6928 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6929 | {150, 50})) |
| 6930 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6931 | |
| 6932 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6933 | |
| 6934 | // Second down on first window. |
| 6935 | const MotionEvent secondFingerDownEvent = |
| 6936 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6937 | .displayId(ADISPLAY_ID_DEFAULT) |
| 6938 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6939 | .pointer( |
| 6940 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6941 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6942 | .build(); |
| 6943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6944 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 6945 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 6946 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6947 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6948 | |
| 6949 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6950 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6951 | |
| 6952 | // Move on window. |
| 6953 | const MotionEvent secondFingerMoveEvent = |
| 6954 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6955 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6956 | .pointer( |
| 6957 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6958 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6959 | .build(); |
| 6960 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6961 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 6962 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 6963 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6964 | mWindow->consumeDragEvent(false, 50, 50); |
| 6965 | mSecondWindow->consumeMotionMove(); |
| 6966 | |
| 6967 | // Release the drag pointer should perform drop. |
| 6968 | const MotionEvent secondFingerUpEvent = |
| 6969 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6970 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 6971 | .pointer( |
| 6972 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 6973 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 6974 | .build(); |
| 6975 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6976 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 6977 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 6978 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6979 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 6980 | mWindow->assertNoEvents(); |
| 6981 | mSecondWindow->consumeMotionMove(); |
| 6982 | } |
| 6983 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6984 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6985 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6986 | |
| 6987 | // Update window of second display. |
| 6988 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6989 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 6990 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 6991 | |
| 6992 | // Let second display has a touch state. |
| 6993 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6994 | injectMotionEvent(mDispatcher, |
| 6995 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6996 | AINPUT_SOURCE_TOUCHSCREEN) |
| 6997 | .displayId(SECOND_DISPLAY_ID) |
| 6998 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 6999 | .x(100) |
| 7000 | .y(100)) |
| 7001 | .build())); |
| 7002 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 7003 | SECOND_DISPLAY_ID, 0 /* expectedFlag */); |
| 7004 | // Update window again. |
| 7005 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 7006 | |
| 7007 | // Move on window. |
| 7008 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7009 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7010 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7011 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7012 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7013 | mWindow->consumeDragEvent(false, 50, 50); |
| 7014 | mSecondWindow->assertNoEvents(); |
| 7015 | |
| 7016 | // Move to another window. |
| 7017 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7018 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7019 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7020 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7021 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7022 | mWindow->consumeDragEvent(true, 150, 50); |
| 7023 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7024 | |
| 7025 | // drop to another window. |
| 7026 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7027 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7028 | {150, 50})) |
| 7029 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7030 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7031 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7032 | mWindow->assertNoEvents(); |
| 7033 | mSecondWindow->assertNoEvents(); |
| 7034 | } |
| 7035 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7036 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 7037 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 7038 | // Move on window. |
| 7039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7040 | injectMotionEvent(mDispatcher, |
| 7041 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 7042 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7043 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7044 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7045 | .x(50) |
| 7046 | .y(50)) |
| 7047 | .build())) |
| 7048 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7049 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7050 | mWindow->consumeDragEvent(false, 50, 50); |
| 7051 | mSecondWindow->assertNoEvents(); |
| 7052 | |
| 7053 | // Move to another window. |
| 7054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7055 | injectMotionEvent(mDispatcher, |
| 7056 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 7057 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7058 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7059 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7060 | .x(150) |
| 7061 | .y(50)) |
| 7062 | .build())) |
| 7063 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7064 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7065 | mWindow->consumeDragEvent(true, 150, 50); |
| 7066 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7067 | |
| 7068 | // drop to another window. |
| 7069 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7070 | injectMotionEvent(mDispatcher, |
| 7071 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 7072 | .buttonState(0) |
| 7073 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7074 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7075 | .x(150) |
| 7076 | .y(50)) |
| 7077 | .build())) |
| 7078 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7079 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7080 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7081 | mWindow->assertNoEvents(); |
| 7082 | mSecondWindow->assertNoEvents(); |
| 7083 | } |
| 7084 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7085 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 7086 | |
| 7087 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 7088 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7089 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7090 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7091 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7092 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7093 | window->setFocusable(true); |
| 7094 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7095 | setFocusedWindow(window); |
| 7096 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7097 | |
| 7098 | // With the flag set, window should not get any input |
| 7099 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7100 | mDispatcher->notifyKey(&keyArgs); |
| 7101 | window->assertNoEvents(); |
| 7102 | |
| 7103 | NotifyMotionArgs motionArgs = |
| 7104 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7105 | ADISPLAY_ID_DEFAULT); |
| 7106 | mDispatcher->notifyMotion(&motionArgs); |
| 7107 | window->assertNoEvents(); |
| 7108 | |
| 7109 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7110 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7111 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7112 | |
| 7113 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7114 | mDispatcher->notifyKey(&keyArgs); |
| 7115 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7116 | |
| 7117 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7118 | ADISPLAY_ID_DEFAULT); |
| 7119 | mDispatcher->notifyMotion(&motionArgs); |
| 7120 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7121 | window->assertNoEvents(); |
| 7122 | } |
| 7123 | |
| 7124 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 7125 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7126 | std::make_shared<FakeApplicationHandle>(); |
| 7127 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7128 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7129 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7130 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7131 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7132 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7133 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7134 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7135 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7136 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7137 | window->setOwnerInfo(222, 222); |
| 7138 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7139 | window->setFocusable(true); |
| 7140 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7141 | setFocusedWindow(window); |
| 7142 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7143 | |
| 7144 | // With the flag set, window should not get any input |
| 7145 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7146 | mDispatcher->notifyKey(&keyArgs); |
| 7147 | window->assertNoEvents(); |
| 7148 | |
| 7149 | NotifyMotionArgs motionArgs = |
| 7150 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7151 | ADISPLAY_ID_DEFAULT); |
| 7152 | mDispatcher->notifyMotion(&motionArgs); |
| 7153 | window->assertNoEvents(); |
| 7154 | |
| 7155 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7156 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7157 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7158 | |
| 7159 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7160 | mDispatcher->notifyKey(&keyArgs); |
| 7161 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7162 | |
| 7163 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7164 | ADISPLAY_ID_DEFAULT); |
| 7165 | mDispatcher->notifyMotion(&motionArgs); |
| 7166 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7167 | window->assertNoEvents(); |
| 7168 | } |
| 7169 | |
| 7170 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 7171 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7172 | std::make_shared<FakeApplicationHandle>(); |
| 7173 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7174 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7175 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7176 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7177 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7178 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7179 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7180 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7181 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7182 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7183 | window->setOwnerInfo(222, 222); |
| 7184 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7185 | window->setFocusable(true); |
| 7186 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7187 | setFocusedWindow(window); |
| 7188 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7189 | |
| 7190 | // With the flag set, window should not get any input |
| 7191 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7192 | mDispatcher->notifyKey(&keyArgs); |
| 7193 | window->assertNoEvents(); |
| 7194 | |
| 7195 | NotifyMotionArgs motionArgs = |
| 7196 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7197 | ADISPLAY_ID_DEFAULT); |
| 7198 | mDispatcher->notifyMotion(&motionArgs); |
| 7199 | window->assertNoEvents(); |
| 7200 | |
| 7201 | // When the window is no longer obscured because it went on top, it should get input |
| 7202 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 7203 | |
| 7204 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7205 | mDispatcher->notifyKey(&keyArgs); |
| 7206 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7207 | |
| 7208 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7209 | ADISPLAY_ID_DEFAULT); |
| 7210 | mDispatcher->notifyMotion(&motionArgs); |
| 7211 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7212 | window->assertNoEvents(); |
| 7213 | } |
| 7214 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7215 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 7216 | protected: |
| 7217 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7218 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7219 | sp<FakeWindowHandle> mWindow; |
| 7220 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7221 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7222 | |
| 7223 | void SetUp() override { |
| 7224 | InputDispatcherTest::SetUp(); |
| 7225 | |
| 7226 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7227 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7228 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7229 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7230 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7231 | mSecondWindow = |
| 7232 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7233 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7234 | mThirdWindow = |
| 7235 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 7236 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 7237 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7238 | |
| 7239 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7240 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 7241 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 7242 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7243 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7244 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7245 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7246 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7247 | WINDOW_UID, true /* hasPermission */, |
| 7248 | ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7249 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 7250 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7251 | mThirdWindow->assertNoEvents(); |
| 7252 | } |
| 7253 | |
| 7254 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 7255 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
| 7256 | SECONDARY_WINDOW_UID, true /* hasPermission */, |
| 7257 | SECOND_DISPLAY_ID)) { |
| 7258 | mWindow->assertNoEvents(); |
| 7259 | mSecondWindow->assertNoEvents(); |
| 7260 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7261 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7264 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 7265 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7266 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 7267 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7268 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 7269 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7270 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7271 | } |
| 7272 | }; |
| 7273 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7274 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7275 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7276 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 7277 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 7278 | false /* hasPermission */); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7279 | } |
| 7280 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7281 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 7282 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7283 | int32_t ownerPid = windowInfo.ownerPid; |
| 7284 | int32_t ownerUid = windowInfo.ownerUid; |
| 7285 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 7286 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7287 | ownerUid, false /*hasPermission*/, |
| 7288 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7289 | mWindow->assertNoEvents(); |
| 7290 | mSecondWindow->assertNoEvents(); |
| 7291 | } |
| 7292 | |
| 7293 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 7294 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7295 | int32_t ownerPid = windowInfo.ownerPid; |
| 7296 | int32_t ownerUid = windowInfo.ownerUid; |
| 7297 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7298 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
| 7299 | ownerUid, true /*hasPermission*/); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7300 | } |
| 7301 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7302 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7303 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7304 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 7305 | windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7306 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7307 | mWindow->assertNoEvents(); |
| 7308 | mSecondWindow->assertNoEvents(); |
| 7309 | } |
| 7310 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7311 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 7312 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 7313 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7314 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 7315 | true /*hasPermission*/, SECOND_DISPLAY_ID)); |
| 7316 | mWindow->assertNoEvents(); |
| 7317 | mSecondWindow->assertNoEvents(); |
| 7318 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 7319 | } |
| 7320 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7321 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 7322 | // Interact with the window first. |
| 7323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 7324 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7325 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7326 | |
| 7327 | // Then remove focus. |
| 7328 | mWindow->setFocusable(false); |
| 7329 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7330 | |
| 7331 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 7332 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7333 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7334 | windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7335 | false /*hasPermission*/, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7336 | } |
| 7337 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7338 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 7339 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7340 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7341 | std::shared_ptr<FakeApplicationHandle> application = |
| 7342 | std::make_shared<FakeApplicationHandle>(); |
| 7343 | std::string name = "Fake Spy "; |
| 7344 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7345 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7346 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7347 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7348 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7349 | return spy; |
| 7350 | } |
| 7351 | |
| 7352 | sp<FakeWindowHandle> createForeground() { |
| 7353 | std::shared_ptr<FakeApplicationHandle> application = |
| 7354 | std::make_shared<FakeApplicationHandle>(); |
| 7355 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7356 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7357 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7358 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7359 | return window; |
| 7360 | } |
| 7361 | |
| 7362 | private: |
| 7363 | int mSpyCount{0}; |
| 7364 | }; |
| 7365 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7366 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7367 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7368 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 7369 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7370 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 7371 | ScopedSilentDeath _silentDeath; |
| 7372 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7373 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7374 | spy->setTrustedOverlay(false); |
| 7375 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 7376 | ".* not a trusted overlay"); |
| 7377 | } |
| 7378 | |
| 7379 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7380 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 7381 | */ |
| 7382 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7383 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7384 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 7385 | |
| 7386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7387 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7388 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7389 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7390 | } |
| 7391 | |
| 7392 | /** |
| 7393 | * Verify the order in which different input windows receive events. The touched foreground window |
| 7394 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 7395 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 7396 | * receive events before ones belows it. |
| 7397 | * |
| 7398 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 7399 | * spy1, spy2, window, spy3. |
| 7400 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 7401 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 7402 | * window. |
| 7403 | */ |
| 7404 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 7405 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7406 | auto spy1 = createSpy(); |
| 7407 | auto spy2 = createSpy(); |
| 7408 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7409 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 7410 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 7411 | const size_t numChannels = channels.size(); |
| 7412 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 7413 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7414 | if (!epollFd.ok()) { |
| 7415 | FAIL() << "Failed to create epoll fd"; |
| 7416 | } |
| 7417 | |
| 7418 | for (size_t i = 0; i < numChannels; i++) { |
| 7419 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 7420 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 7421 | FAIL() << "Failed to add fd to epoll"; |
| 7422 | } |
| 7423 | } |
| 7424 | |
| 7425 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7426 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7427 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7428 | |
| 7429 | std::vector<size_t> eventOrder; |
| 7430 | std::vector<struct epoll_event> events(numChannels); |
| 7431 | for (;;) { |
| 7432 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 7433 | (100ms).count()); |
| 7434 | if (nFds < 0) { |
| 7435 | FAIL() << "Failed to call epoll_wait"; |
| 7436 | } |
| 7437 | if (nFds == 0) { |
| 7438 | break; // epoll_wait timed out |
| 7439 | } |
| 7440 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 7441 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 7442 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7443 | channels[i]->consumeMotionDown(); |
| 7444 | } |
| 7445 | } |
| 7446 | |
| 7447 | // Verify the order in which the events were received. |
| 7448 | EXPECT_EQ(3u, eventOrder.size()); |
| 7449 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 7450 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 7451 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 7452 | } |
| 7453 | |
| 7454 | /** |
| 7455 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 7456 | */ |
| 7457 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 7458 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7459 | auto spy = createSpy(); |
| 7460 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7461 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7462 | |
| 7463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7464 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7465 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7466 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7467 | spy->assertNoEvents(); |
| 7468 | } |
| 7469 | |
| 7470 | /** |
| 7471 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 7472 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 7473 | * to the window. |
| 7474 | */ |
| 7475 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 7476 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7477 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7478 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 7479 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7480 | |
| 7481 | // Inject an event outside the spy window's touchable region. |
| 7482 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7483 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7484 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7485 | window->consumeMotionDown(); |
| 7486 | spy->assertNoEvents(); |
| 7487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7488 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7489 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7490 | window->consumeMotionUp(); |
| 7491 | spy->assertNoEvents(); |
| 7492 | |
| 7493 | // Inject an event inside the spy window's touchable region. |
| 7494 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7495 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7496 | {5, 10})) |
| 7497 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7498 | window->consumeMotionDown(); |
| 7499 | spy->consumeMotionDown(); |
| 7500 | } |
| 7501 | |
| 7502 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7503 | * 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] | 7504 | * 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] | 7505 | */ |
| 7506 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 7507 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7508 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7509 | auto spy = createSpy(); |
| 7510 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7511 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7512 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 7513 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7514 | |
| 7515 | // Inject an event outside the spy window's frame and touchable region. |
| 7516 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7517 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7518 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7519 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7520 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7521 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7522 | } |
| 7523 | |
| 7524 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7525 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 7526 | * pointers that are down within its bounds. |
| 7527 | */ |
| 7528 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 7529 | auto windowLeft = createForeground(); |
| 7530 | windowLeft->setFrame({0, 0, 100, 200}); |
| 7531 | auto windowRight = createForeground(); |
| 7532 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7533 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7534 | spy->setFrame({0, 0, 200, 200}); |
| 7535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 7536 | |
| 7537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7538 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7539 | {50, 50})) |
| 7540 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7541 | windowLeft->consumeMotionDown(); |
| 7542 | spy->consumeMotionDown(); |
| 7543 | |
| 7544 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7545 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7546 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7547 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7548 | .pointer( |
| 7549 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7550 | .build(); |
| 7551 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7552 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7553 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7554 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7555 | windowRight->consumeMotionDown(); |
| 7556 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7557 | } |
| 7558 | |
| 7559 | /** |
| 7560 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 7561 | * the spy should receive the second pointer with ACTION_DOWN. |
| 7562 | */ |
| 7563 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 7564 | auto window = createForeground(); |
| 7565 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7566 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7567 | spyRight->setFrame({100, 0, 200, 200}); |
| 7568 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 7569 | |
| 7570 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7571 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7572 | {50, 50})) |
| 7573 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7574 | window->consumeMotionDown(); |
| 7575 | spyRight->assertNoEvents(); |
| 7576 | |
| 7577 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7578 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7579 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7580 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7581 | .pointer( |
| 7582 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7583 | .build(); |
| 7584 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7585 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7586 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7587 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7588 | window->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7589 | spyRight->consumeMotionDown(); |
| 7590 | } |
| 7591 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7592 | /** |
| 7593 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 7594 | * windows should be allowed to control split touch. |
| 7595 | */ |
| 7596 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7597 | // 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] | 7598 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7599 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7600 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7601 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7602 | auto window = createForeground(); |
| 7603 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7604 | |
| 7605 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7606 | |
| 7607 | // First finger down, no window touched. |
| 7608 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7609 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7610 | {100, 200})) |
| 7611 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7612 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7613 | window->assertNoEvents(); |
| 7614 | |
| 7615 | // Second finger down on window, the window should receive touch down. |
| 7616 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7617 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7618 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7619 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7620 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7621 | .x(100) |
| 7622 | .y(200)) |
| 7623 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7624 | .build(); |
| 7625 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7626 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7627 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7628 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7629 | |
| 7630 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7631 | spy->consumeMotionPointerDown(1 /* pointerIndex */); |
| 7632 | } |
| 7633 | |
| 7634 | /** |
| 7635 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 7636 | * do not receive key events. |
| 7637 | */ |
| 7638 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7639 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7640 | spy->setFocusable(false); |
| 7641 | |
| 7642 | auto window = createForeground(); |
| 7643 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7644 | setFocusedWindow(window); |
| 7645 | window->consumeFocusEvent(true); |
| 7646 | |
| 7647 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7648 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7649 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7650 | |
| 7651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7652 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7653 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7654 | |
| 7655 | spy->assertNoEvents(); |
| 7656 | } |
| 7657 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 7658 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 7659 | |
| 7660 | /** |
| 7661 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 7662 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 7663 | */ |
| 7664 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 7665 | auto window = createForeground(); |
| 7666 | auto spy1 = createSpy(); |
| 7667 | auto spy2 = createSpy(); |
| 7668 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 7669 | |
| 7670 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7671 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7672 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7673 | window->consumeMotionDown(); |
| 7674 | spy1->consumeMotionDown(); |
| 7675 | spy2->consumeMotionDown(); |
| 7676 | |
| 7677 | // Pilfer pointers from the second spy window. |
| 7678 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 7679 | spy2->assertNoEvents(); |
| 7680 | spy1->consumeMotionCancel(); |
| 7681 | window->consumeMotionCancel(); |
| 7682 | |
| 7683 | // The rest of the gesture should only be sent to the second spy window. |
| 7684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7685 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7686 | ADISPLAY_ID_DEFAULT)) |
| 7687 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7688 | spy2->consumeMotionMove(); |
| 7689 | spy1->assertNoEvents(); |
| 7690 | window->assertNoEvents(); |
| 7691 | } |
| 7692 | |
| 7693 | /** |
| 7694 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 7695 | * in the middle of the gesture. |
| 7696 | */ |
| 7697 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 7698 | auto window = createForeground(); |
| 7699 | auto spy = createSpy(); |
| 7700 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7701 | |
| 7702 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7703 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7704 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7705 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7706 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7707 | |
| 7708 | window->releaseChannel(); |
| 7709 | |
| 7710 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7711 | |
| 7712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7713 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7714 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7715 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7716 | } |
| 7717 | |
| 7718 | /** |
| 7719 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 7720 | * the spy, but not to any other windows. |
| 7721 | */ |
| 7722 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 7723 | auto spy = createSpy(); |
| 7724 | auto window = createForeground(); |
| 7725 | |
| 7726 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7727 | |
| 7728 | // First finger down on the window and the spy. |
| 7729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7730 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7731 | {100, 200})) |
| 7732 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7733 | spy->consumeMotionDown(); |
| 7734 | window->consumeMotionDown(); |
| 7735 | |
| 7736 | // Spy window pilfers the pointers. |
| 7737 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7738 | window->consumeMotionCancel(); |
| 7739 | |
| 7740 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 7741 | const MotionEvent secondFingerDownEvent = |
| 7742 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7743 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7744 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7745 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7746 | .x(100) |
| 7747 | .y(200)) |
| 7748 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7749 | .build(); |
| 7750 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7751 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7752 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7753 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7754 | |
| 7755 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7756 | |
| 7757 | // Third finger goes down outside all windows, so injection should fail. |
| 7758 | const MotionEvent thirdFingerDownEvent = |
| 7759 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7760 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7761 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7762 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7763 | .x(100) |
| 7764 | .y(200)) |
| 7765 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7766 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) |
| 7767 | .build(); |
| 7768 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7769 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7770 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7771 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7772 | |
| 7773 | spy->assertNoEvents(); |
| 7774 | window->assertNoEvents(); |
| 7775 | } |
| 7776 | |
| 7777 | /** |
| 7778 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 7779 | */ |
| 7780 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 7781 | auto spy = createSpy(); |
| 7782 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7783 | auto window = createForeground(); |
| 7784 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7785 | |
| 7786 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7787 | |
| 7788 | // First finger down on the window only |
| 7789 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7790 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7791 | {150, 150})) |
| 7792 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7793 | window->consumeMotionDown(); |
| 7794 | |
| 7795 | // Second finger down on the spy and window |
| 7796 | const MotionEvent secondFingerDownEvent = |
| 7797 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7798 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7799 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7800 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7801 | .x(150) |
| 7802 | .y(150)) |
| 7803 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7804 | .build(); |
| 7805 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7806 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7807 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7808 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7809 | spy->consumeMotionDown(); |
| 7810 | window->consumeMotionPointerDown(1); |
| 7811 | |
| 7812 | // Third finger down on the spy and window |
| 7813 | const MotionEvent thirdFingerDownEvent = |
| 7814 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7815 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7816 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7817 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7818 | .x(150) |
| 7819 | .y(150)) |
| 7820 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7821 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7822 | .build(); |
| 7823 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7824 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7825 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7826 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7827 | spy->consumeMotionPointerDown(1); |
| 7828 | window->consumeMotionPointerDown(2); |
| 7829 | |
| 7830 | // Spy window pilfers the pointers. |
| 7831 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7832 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 7833 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 7834 | |
| 7835 | spy->assertNoEvents(); |
| 7836 | window->assertNoEvents(); |
| 7837 | } |
| 7838 | |
| 7839 | /** |
| 7840 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 7841 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 7842 | * window, then that window should receive ACTION_CANCEL. |
| 7843 | */ |
| 7844 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 7845 | auto spy = createSpy(); |
| 7846 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7847 | auto window = createForeground(); |
| 7848 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7849 | |
| 7850 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7851 | |
| 7852 | // First finger down on both spy and window |
| 7853 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7854 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7855 | {10, 10})) |
| 7856 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7857 | window->consumeMotionDown(); |
| 7858 | spy->consumeMotionDown(); |
| 7859 | |
| 7860 | // Second finger down on the spy and window |
| 7861 | const MotionEvent secondFingerDownEvent = |
| 7862 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7863 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7864 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7865 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7866 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7867 | .build(); |
| 7868 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7869 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7870 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7871 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7872 | spy->consumeMotionPointerDown(1); |
| 7873 | window->consumeMotionPointerDown(1); |
| 7874 | |
| 7875 | // Spy window pilfers the pointers. |
| 7876 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7877 | window->consumeMotionCancel(); |
| 7878 | |
| 7879 | spy->assertNoEvents(); |
| 7880 | window->assertNoEvents(); |
| 7881 | } |
| 7882 | |
| 7883 | /** |
| 7884 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 7885 | * be sent to other windows |
| 7886 | */ |
| 7887 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 7888 | auto spy = createSpy(); |
| 7889 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 7890 | auto window = createForeground(); |
| 7891 | window->setFrame(Rect(0, 0, 200, 200)); |
| 7892 | |
| 7893 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7894 | |
| 7895 | // First finger down on both window and spy |
| 7896 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7897 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7898 | {10, 10})) |
| 7899 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7900 | window->consumeMotionDown(); |
| 7901 | spy->consumeMotionDown(); |
| 7902 | |
| 7903 | // Spy window pilfers the pointers. |
| 7904 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7905 | window->consumeMotionCancel(); |
| 7906 | |
| 7907 | // Second finger down on the window only |
| 7908 | const MotionEvent secondFingerDownEvent = |
| 7909 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7910 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7911 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7912 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 7913 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7914 | .x(150) |
| 7915 | .y(150)) |
| 7916 | .build(); |
| 7917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7918 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7919 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7920 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7921 | window->consumeMotionDown(); |
| 7922 | window->assertNoEvents(); |
| 7923 | |
| 7924 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 7925 | spy->consumeMotionMove(); |
| 7926 | spy->assertNoEvents(); |
| 7927 | } |
| 7928 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7929 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 7930 | public: |
| 7931 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 7932 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 7933 | std::make_shared<FakeApplicationHandle>(); |
| 7934 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7935 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 7936 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7937 | overlay->setFocusable(false); |
| 7938 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7939 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7940 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7941 | overlay->setTrustedOverlay(true); |
| 7942 | |
| 7943 | std::shared_ptr<FakeApplicationHandle> application = |
| 7944 | std::make_shared<FakeApplicationHandle>(); |
| 7945 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 7947 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7948 | window->setFocusable(true); |
| 7949 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7950 | |
| 7951 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7952 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7953 | setFocusedWindow(window); |
| 7954 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7955 | return {std::move(overlay), std::move(window)}; |
| 7956 | } |
| 7957 | |
| 7958 | void sendFingerEvent(int32_t action) { |
| 7959 | NotifyMotionArgs motionArgs = |
| 7960 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 7961 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 7962 | mDispatcher->notifyMotion(&motionArgs); |
| 7963 | } |
| 7964 | |
| 7965 | void sendStylusEvent(int32_t action) { |
| 7966 | NotifyMotionArgs motionArgs = |
| 7967 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 7968 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 7969 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 7970 | mDispatcher->notifyMotion(&motionArgs); |
| 7971 | } |
| 7972 | }; |
| 7973 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7974 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 7975 | |
| 7976 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 7977 | ScopedSilentDeath _silentDeath; |
| 7978 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 7979 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7980 | overlay->setTrustedOverlay(false); |
| 7981 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 7982 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 7983 | ".* not a trusted overlay"); |
| 7984 | } |
| 7985 | |
| 7986 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 7987 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 7988 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 7989 | |
| 7990 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7991 | overlay->consumeMotionDown(); |
| 7992 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 7993 | overlay->consumeMotionUp(); |
| 7994 | |
| 7995 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 7996 | window->consumeMotionDown(); |
| 7997 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 7998 | window->consumeMotionUp(); |
| 7999 | |
| 8000 | overlay->assertNoEvents(); |
| 8001 | window->assertNoEvents(); |
| 8002 | } |
| 8003 | |
| 8004 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 8005 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8006 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8007 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8008 | |
| 8009 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8010 | overlay->consumeMotionDown(); |
| 8011 | window->consumeMotionDown(); |
| 8012 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8013 | overlay->consumeMotionUp(); |
| 8014 | window->consumeMotionUp(); |
| 8015 | |
| 8016 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8017 | window->consumeMotionDown(); |
| 8018 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 8019 | window->consumeMotionUp(); |
| 8020 | |
| 8021 | overlay->assertNoEvents(); |
| 8022 | window->assertNoEvents(); |
| 8023 | } |
| 8024 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 8025 | /** |
| 8026 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 8027 | * The scenario is as follows: |
| 8028 | * - The stylus interceptor overlay is configured as a spy window. |
| 8029 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 8030 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 8031 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 8032 | */ |
| 8033 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 8034 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8035 | overlay->setSpy(true); |
| 8036 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8037 | |
| 8038 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8039 | overlay->consumeMotionDown(); |
| 8040 | window->consumeMotionDown(); |
| 8041 | |
| 8042 | // The interceptor pilfers the pointers. |
| 8043 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 8044 | window->consumeMotionCancel(); |
| 8045 | |
| 8046 | // The interceptor configures itself so that it is no longer a spy. |
| 8047 | overlay->setSpy(false); |
| 8048 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8049 | |
| 8050 | // It continues to receive the rest of the stylus gesture. |
| 8051 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 8052 | overlay->consumeMotionMove(); |
| 8053 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8054 | overlay->consumeMotionUp(); |
| 8055 | |
| 8056 | window->assertNoEvents(); |
| 8057 | } |
| 8058 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8059 | struct User { |
| 8060 | int32_t mPid; |
| 8061 | int32_t mUid; |
| 8062 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 8063 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 8064 | |
| 8065 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 8066 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 8067 | |
| 8068 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 8069 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 8070 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 8071 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8072 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 8073 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8074 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 8075 | } |
| 8076 | |
| 8077 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
| 8078 | return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE, |
| 8079 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8080 | INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid}, |
| 8081 | mPolicyFlags); |
| 8082 | } |
| 8083 | |
| 8084 | sp<FakeWindowHandle> createWindow() const { |
| 8085 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8086 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8087 | sp<FakeWindowHandle> window = |
| 8088 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 8089 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8090 | window->setOwnerInfo(mPid, mUid); |
| 8091 | return window; |
| 8092 | } |
| 8093 | }; |
| 8094 | |
| 8095 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 8096 | |
| 8097 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 8098 | auto owner = User(mDispatcher, 10, 11); |
| 8099 | auto window = owner.createWindow(); |
| 8100 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8101 | |
| 8102 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8103 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8104 | window->consumeMotionDown(); |
| 8105 | |
| 8106 | setFocusedWindow(window); |
| 8107 | window->consumeFocusEvent(true); |
| 8108 | |
| 8109 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8110 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8111 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8112 | } |
| 8113 | |
| 8114 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 8115 | auto owner = User(mDispatcher, 10, 11); |
| 8116 | auto window = owner.createWindow(); |
| 8117 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8118 | |
| 8119 | auto rando = User(mDispatcher, 20, 21); |
| 8120 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8121 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8122 | |
| 8123 | setFocusedWindow(window); |
| 8124 | window->consumeFocusEvent(true); |
| 8125 | |
| 8126 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8127 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8128 | window->assertNoEvents(); |
| 8129 | } |
| 8130 | |
| 8131 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 8132 | auto owner = User(mDispatcher, 10, 11); |
| 8133 | auto window = owner.createWindow(); |
| 8134 | auto spy = owner.createWindow(); |
| 8135 | spy->setSpy(true); |
| 8136 | spy->setTrustedOverlay(true); |
| 8137 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8138 | |
| 8139 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8140 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8141 | spy->consumeMotionDown(); |
| 8142 | window->consumeMotionDown(); |
| 8143 | } |
| 8144 | |
| 8145 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 8146 | auto owner = User(mDispatcher, 10, 11); |
| 8147 | auto window = owner.createWindow(); |
| 8148 | |
| 8149 | auto rando = User(mDispatcher, 20, 21); |
| 8150 | auto randosSpy = rando.createWindow(); |
| 8151 | randosSpy->setSpy(true); |
| 8152 | randosSpy->setTrustedOverlay(true); |
| 8153 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8154 | |
| 8155 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 8156 | // not receive the event. |
| 8157 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8158 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8159 | randosSpy->assertNoEvents(); |
| 8160 | window->consumeMotionDown(); |
| 8161 | } |
| 8162 | |
| 8163 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 8164 | auto owner = User(mDispatcher, 10, 11); |
| 8165 | auto window = owner.createWindow(); |
| 8166 | |
| 8167 | auto rando = User(mDispatcher, 20, 21); |
| 8168 | auto randosSpy = rando.createWindow(); |
| 8169 | randosSpy->setSpy(true); |
| 8170 | randosSpy->setTrustedOverlay(true); |
| 8171 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8172 | |
| 8173 | // A user that has injection permission can inject into any window. |
| 8174 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8175 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8176 | ADISPLAY_ID_DEFAULT)); |
| 8177 | randosSpy->consumeMotionDown(); |
| 8178 | window->consumeMotionDown(); |
| 8179 | |
| 8180 | setFocusedWindow(randosSpy); |
| 8181 | randosSpy->consumeFocusEvent(true); |
| 8182 | |
| 8183 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 8184 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8185 | window->assertNoEvents(); |
| 8186 | } |
| 8187 | |
| 8188 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 8189 | auto owner = User(mDispatcher, 10, 11); |
| 8190 | auto window = owner.createWindow(); |
| 8191 | |
| 8192 | auto rando = User(mDispatcher, 20, 21); |
| 8193 | auto randosWindow = rando.createWindow(); |
| 8194 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 8195 | randosWindow->setWatchOutsideTouch(true); |
| 8196 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 8197 | |
| 8198 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 8199 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8200 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8201 | window->consumeMotionDown(); |
| 8202 | randosWindow->consumeMotionOutside(); |
| 8203 | } |
| 8204 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 8205 | } // namespace android::inputdispatcher |