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; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 55 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 56 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 57 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 58 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 59 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 60 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 61 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 62 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 63 | static constexpr int32_t POINTER_1_DOWN = |
| 64 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 65 | static constexpr int32_t POINTER_2_DOWN = |
| 66 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 67 | static constexpr int32_t POINTER_3_DOWN = |
| 68 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 69 | static constexpr int32_t POINTER_0_UP = |
| 70 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 71 | static constexpr int32_t POINTER_1_UP = |
| 72 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 73 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 74 | // 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] | 75 | static constexpr int32_t WINDOW_PID = 999; |
| 76 | static constexpr int32_t WINDOW_UID = 1001; |
| 77 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 78 | // The default pid and uid for the windows created on the secondary display by the test. |
| 79 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; |
| 80 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; |
| 81 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 82 | // The default policy flags to use for event injection by tests. |
| 83 | 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] | 84 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 85 | // An arbitrary pid of the gesture monitor window |
| 86 | static constexpr int32_t MONITOR_PID = 2001; |
| 87 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 88 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 89 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 90 | static constexpr int expectedWallpaperFlags = |
| 91 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 92 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 93 | struct PointF { |
| 94 | float x; |
| 95 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 96 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 97 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 99 | /** |
| 100 | * Return a DOWN key event with KEYCODE_A. |
| 101 | */ |
| 102 | static KeyEvent getTestKeyEvent() { |
| 103 | KeyEvent event; |
| 104 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 105 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 106 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 107 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 108 | return event; |
| 109 | } |
| 110 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 111 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 112 | ASSERT_EQ(expectedAction, receivedAction) |
| 113 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 114 | << MotionEvent::actionToString(receivedAction); |
| 115 | } |
| 116 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 117 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 118 | bool matches = action == arg.getAction(); |
| 119 | if (!matches) { |
| 120 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 121 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 122 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 123 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 124 | if (!matches) { |
| 125 | *result_listener << "; "; |
| 126 | } |
| 127 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 128 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 129 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 130 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 131 | if (!matches) { |
| 132 | *result_listener << "; "; |
| 133 | } |
| 134 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 135 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 136 | } |
| 137 | return matches; |
| 138 | } |
| 139 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 140 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 141 | return arg.getDownTime() == downTime; |
| 142 | } |
| 143 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 144 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { |
| 145 | return arg.getDisplayId() == displayId; |
| 146 | } |
| 147 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 148 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 149 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 150 | << inputEventSourceToString(arg.getSource()); |
| 151 | return arg.getSource() == source; |
| 152 | } |
| 153 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 154 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { |
| 155 | return arg.getFlags() == flags; |
| 156 | } |
| 157 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 158 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 159 | if (arg.getPointerCount() != 1) { |
| 160 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 161 | return false; |
| 162 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 163 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 167 | // Build a map for the received pointers, by pointer id |
| 168 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 169 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 170 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 171 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 172 | } |
| 173 | return pointers == actualPointers; |
| 174 | } |
| 175 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 176 | // --- FakeInputDispatcherPolicy --- |
| 177 | |
| 178 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 179 | InputDispatcherConfiguration mConfig; |
| 180 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 181 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 182 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 183 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 184 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 185 | |
| 186 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 187 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 188 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 189 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 190 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
| 191 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); |
| 192 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 193 | |
| 194 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 195 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 196 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 197 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 200 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 201 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
| 202 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); |
| 203 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 204 | |
| 205 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 206 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 207 | EXPECT_EQ(motionEvent.getAction(), args.action); |
| 208 | EXPECT_EQ(motionEvent.getX(0), point.x); |
| 209 | EXPECT_EQ(motionEvent.getY(0), point.y); |
| 210 | EXPECT_EQ(motionEvent.getRawX(0), point.x); |
| 211 | EXPECT_EQ(motionEvent.getRawY(0), point.y); |
| 212 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 213 | } |
| 214 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 215 | void assertFilterInputEventWasNotCalled() { |
| 216 | std::scoped_lock lock(mLock); |
| 217 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 218 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 219 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 220 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 221 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 222 | ASSERT_TRUE(mConfigurationChangedTime) |
| 223 | << "Timed out waiting for configuration changed call"; |
| 224 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 225 | mConfigurationChangedTime = std::nullopt; |
| 226 | } |
| 227 | |
| 228 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 229 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 230 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 231 | // 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] | 232 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 233 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 234 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 235 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 236 | mLastNotifySwitch = std::nullopt; |
| 237 | } |
| 238 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 239 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 240 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 241 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 242 | mOnPointerDownToken.clear(); |
| 243 | } |
| 244 | |
| 245 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 246 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 247 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 248 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 251 | // This function must be called soon after the expected ANR timer starts, |
| 252 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 253 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 254 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 255 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 256 | std::unique_lock lock(mLock); |
| 257 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 258 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 259 | ASSERT_NO_FATAL_FAILURE( |
| 260 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 261 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 264 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 265 | const sp<WindowInfoHandle>& window) { |
| 266 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 267 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 268 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 271 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 272 | const sp<IBinder>& expectedToken, |
| 273 | int32_t expectedPid) { |
| 274 | std::unique_lock lock(mLock); |
| 275 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 276 | AnrResult result; |
| 277 | ASSERT_NO_FATAL_FAILURE(result = |
| 278 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 279 | const auto& [token, pid] = result; |
| 280 | ASSERT_EQ(expectedToken, token); |
| 281 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 284 | /** 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] | 285 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 286 | std::unique_lock lock(mLock); |
| 287 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 288 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 289 | const auto& [token, _] = result; |
| 290 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 293 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 294 | int32_t expectedPid) { |
| 295 | std::unique_lock lock(mLock); |
| 296 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 297 | AnrResult result; |
| 298 | ASSERT_NO_FATAL_FAILURE( |
| 299 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 300 | const auto& [token, pid] = result; |
| 301 | ASSERT_EQ(expectedToken, token); |
| 302 | ASSERT_EQ(expectedPid, pid); |
| 303 | } |
| 304 | |
| 305 | /** 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] | 306 | sp<IBinder> getResponsiveWindowToken() { |
| 307 | std::unique_lock lock(mLock); |
| 308 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 309 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 310 | const auto& [token, _] = result; |
| 311 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void assertNotifyAnrWasNotCalled() { |
| 315 | std::scoped_lock lock(mLock); |
| 316 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 317 | ASSERT_TRUE(mAnrWindows.empty()); |
| 318 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 319 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 320 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 323 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 324 | mConfig.keyRepeatTimeout = timeout; |
| 325 | mConfig.keyRepeatDelay = delay; |
| 326 | } |
| 327 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 328 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 329 | std::unique_lock lock(mLock); |
| 330 | base::ScopedLockAssertion assumeLocked(mLock); |
| 331 | |
| 332 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 333 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 334 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 335 | enabled; |
| 336 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 337 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 338 | << ") to be called."; |
| 339 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 340 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 341 | auto request = *mPointerCaptureRequest; |
| 342 | mPointerCaptureRequest.reset(); |
| 343 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void assertSetPointerCaptureNotCalled() { |
| 347 | std::unique_lock lock(mLock); |
| 348 | base::ScopedLockAssertion assumeLocked(mLock); |
| 349 | |
| 350 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 351 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 352 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 353 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 354 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 355 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 356 | } |
| 357 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 358 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 359 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 360 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 361 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 362 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 363 | } |
| 364 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 365 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 366 | std::unique_lock lock(mLock); |
| 367 | base::ScopedLockAssertion assumeLocked(mLock); |
| 368 | std::optional<sp<IBinder>> receivedToken = |
| 369 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 370 | mNotifyInputChannelBroken); |
| 371 | ASSERT_TRUE(receivedToken.has_value()); |
| 372 | ASSERT_EQ(token, *receivedToken); |
| 373 | } |
| 374 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 375 | /** |
| 376 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 377 | */ |
| 378 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 379 | mInterceptKeyTimeout = timeout; |
| 380 | } |
| 381 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 382 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 383 | std::mutex mLock; |
| 384 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 385 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 386 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 387 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 388 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 389 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 390 | |
| 391 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 392 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 393 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 394 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 395 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 396 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 397 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 398 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 399 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 400 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 401 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 402 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 403 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 404 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 405 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 406 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 407 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 408 | // first entry from the container and erase it. |
| 409 | template <class T> |
| 410 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 411 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 412 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 413 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 414 | // before checking if ANR was called. |
| 415 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 416 | // to provide it some time to act. 100ms seems reasonable. |
| 417 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 418 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 419 | std::optional<T> token = |
| 420 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 421 | if (!token.has_value()) { |
| 422 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 423 | return {}; |
| 424 | } |
| 425 | |
| 426 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 427 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 428 | // the dispatcher started counting before this function was called |
| 429 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 430 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 431 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 432 | << "ms, but waited " |
| 433 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 434 | << "ms instead"; |
| 435 | } |
| 436 | return *token; |
| 437 | } |
| 438 | |
| 439 | template <class T> |
| 440 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 441 | std::queue<T>& storage, |
| 442 | std::unique_lock<std::mutex>& lock, |
| 443 | std::condition_variable& condition) |
| 444 | REQUIRES(mLock) { |
| 445 | condition.wait_for(lock, timeout, |
| 446 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 447 | if (storage.empty()) { |
| 448 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 449 | return std::nullopt; |
| 450 | } |
| 451 | T item = storage.front(); |
| 452 | storage.pop(); |
| 453 | return std::make_optional(item); |
| 454 | } |
| 455 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 456 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 457 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 458 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 461 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 462 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 463 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 464 | ASSERT_TRUE(pid.has_value()); |
| 465 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 466 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 467 | } |
| 468 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 469 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 470 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 471 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 472 | ASSERT_TRUE(pid.has_value()); |
| 473 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 474 | mNotifyAnr.notify_all(); |
| 475 | } |
| 476 | |
| 477 | void notifyNoFocusedWindowAnr( |
| 478 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 479 | std::scoped_lock lock(mLock); |
| 480 | mAnrApplications.push(applicationHandle); |
| 481 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 484 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 485 | std::scoped_lock lock(mLock); |
| 486 | mBrokenInputChannels.push(connectionToken); |
| 487 | mNotifyInputChannelBroken.notify_all(); |
| 488 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 489 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 490 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 491 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 492 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 493 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 494 | const std::vector<float>& values) override {} |
| 495 | |
| 496 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 497 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 498 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 499 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 500 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 501 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 502 | *outConfig = mConfig; |
| 503 | } |
| 504 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 505 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 506 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 507 | switch (inputEvent->getType()) { |
| 508 | case AINPUT_EVENT_TYPE_KEY: { |
| 509 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 510 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 511 | break; |
| 512 | } |
| 513 | |
| 514 | case AINPUT_EVENT_TYPE_MOTION: { |
| 515 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 516 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 520 | return true; |
| 521 | } |
| 522 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 523 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { |
| 524 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { |
| 525 | // Clear intercept state when we handled the event. |
| 526 | mInterceptKeyTimeout = 0ms; |
| 527 | } |
| 528 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 529 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 530 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 532 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 533 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 534 | // Clear intercept state so we could dispatch the event in next wake. |
| 535 | mInterceptKeyTimeout = 0ms; |
| 536 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 539 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 543 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 544 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 545 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 546 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 547 | * essentially a passthrough for notifySwitch. |
| 548 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 549 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 552 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 554 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 555 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 556 | mOnPointerDownToken = newToken; |
| 557 | } |
| 558 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 559 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 560 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 561 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 562 | mPointerCaptureChangedCondition.notify_all(); |
| 563 | } |
| 564 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 565 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 566 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 567 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 568 | mDropTargetWindowToken = token; |
| 569 | } |
| 570 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 571 | void assertFilterInputEventWasCalledInternal( |
| 572 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 573 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 574 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 575 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 576 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 577 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 578 | }; |
| 579 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | // --- InputDispatcherTest --- |
| 581 | |
| 582 | class InputDispatcherTest : public testing::Test { |
| 583 | protected: |
| 584 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 585 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 586 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 587 | void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 588 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 589 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 590 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 591 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 592 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 595 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 596 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 597 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 598 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 599 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 600 | |
| 601 | /** |
| 602 | * Used for debugging when writing the test |
| 603 | */ |
| 604 | void dumpDispatcherState() { |
| 605 | std::string dump; |
| 606 | mDispatcher->dump(dump); |
| 607 | std::stringstream ss(dump); |
| 608 | std::string to; |
| 609 | |
| 610 | while (std::getline(ss, to, '\n')) { |
| 611 | ALOGE("%s", to.c_str()); |
| 612 | } |
| 613 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 614 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 615 | void setFocusedWindow(const sp<WindowInfoHandle>& window, |
| 616 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 617 | FocusRequest request; |
| 618 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 619 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 620 | if (focusedWindow) { |
| 621 | request.focusedToken = focusedWindow->getToken(); |
| 622 | } |
| 623 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 624 | request.displayId = window->getInfo()->displayId; |
| 625 | mDispatcher->setFocusedWindow(request); |
| 626 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | }; |
| 628 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 630 | KeyEvent event; |
| 631 | |
| 632 | // Rejects undefined key actions. |
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, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 635 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 636 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 637 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 638 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 639 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 640 | << "Should reject key events with undefined action."; |
| 641 | |
| 642 | // 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] | 643 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 644 | 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] | 645 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 646 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 647 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 648 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 649 | << "Should reject key events with ACTION_MULTIPLE."; |
| 650 | } |
| 651 | |
| 652 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 653 | MotionEvent event; |
| 654 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 655 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 656 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 657 | pointerProperties[i].clear(); |
| 658 | pointerProperties[i].id = i; |
| 659 | pointerCoords[i].clear(); |
| 660 | } |
| 661 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 662 | // Some constants commonly used below |
| 663 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 664 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 665 | constexpr int32_t metaState = AMETA_NONE; |
| 666 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 667 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 668 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 669 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 670 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 671 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 672 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 673 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 674 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 675 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 676 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 677 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 678 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | << "Should reject motion events with undefined action."; |
| 680 | |
| 681 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 682 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 683 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 684 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 685 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 686 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 687 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 688 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 689 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 690 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 691 | << "Should reject motion events with pointer down index too large."; |
| 692 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 693 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 694 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 695 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 696 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 697 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 698 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 699 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 700 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 701 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 702 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 703 | << "Should reject motion events with pointer down index too small."; |
| 704 | |
| 705 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 706 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 707 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 708 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 709 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 710 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 711 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 712 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 713 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 714 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 715 | << "Should reject motion events with pointer up index too large."; |
| 716 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 717 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 718 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 719 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 720 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 721 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 722 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 723 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 724 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 725 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 726 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 727 | << "Should reject motion events with pointer up index too small."; |
| 728 | |
| 729 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 730 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 731 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 732 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 733 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 734 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 735 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 736 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 737 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 738 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | << "Should reject motion events with 0 pointers."; |
| 740 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 741 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 742 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 743 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 744 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 745 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 746 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 747 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 748 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 749 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 750 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 751 | |
| 752 | // Rejects motion events with invalid pointer ids. |
| 753 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 754 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 755 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 756 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 757 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 758 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 759 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 760 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 761 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 762 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 763 | << "Should reject motion events with pointer ids less than 0."; |
| 764 | |
| 765 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 766 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 767 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 768 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 769 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 770 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 771 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 772 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 773 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 774 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 776 | |
| 777 | // Rejects motion events with duplicate pointer ids. |
| 778 | pointerProperties[0].id = 1; |
| 779 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 780 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 781 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 782 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 783 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 784 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 785 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 786 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 787 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 788 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | << "Should reject motion events with duplicate pointer ids."; |
| 790 | } |
| 791 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 792 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 793 | |
| 794 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 795 | constexpr nsecs_t eventTime = 20; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 796 | NotifyConfigurationChangedArgs args(/*id=*/10, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 797 | mDispatcher->notifyConfigurationChanged(&args); |
| 798 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 799 | |
| 800 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 801 | } |
| 802 | |
| 803 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 804 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 805 | /*switchMask=*/2); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 806 | mDispatcher->notifySwitch(&args); |
| 807 | |
| 808 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 809 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 810 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 811 | } |
| 812 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 813 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 814 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 815 | // Default input dispatching timeout if there is no focused application or paused window |
| 816 | // from which to determine an appropriate dispatching timeout. |
| 817 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 818 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 819 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 820 | |
| 821 | class FakeApplicationHandle : public InputApplicationHandle { |
| 822 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 823 | FakeApplicationHandle() { |
| 824 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 825 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 826 | mInfo.dispatchingTimeoutMillis = |
| 827 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 828 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 829 | virtual ~FakeApplicationHandle() {} |
| 830 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 831 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 832 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 833 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 834 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 835 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 836 | }; |
| 837 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 838 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 839 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 840 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 841 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 842 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 843 | } |
| 844 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 845 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 846 | InputEvent* event; |
| 847 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 848 | if (!consumeSeq) { |
| 849 | return nullptr; |
| 850 | } |
| 851 | finishEvent(*consumeSeq); |
| 852 | return event; |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Receive an event without acknowledging it. |
| 857 | * Return the sequence number that could later be used to send finished signal. |
| 858 | */ |
| 859 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 860 | uint32_t consumeSeq; |
| 861 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 862 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 863 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 864 | status_t status = WOULD_BLOCK; |
| 865 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 866 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 867 | &event); |
| 868 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 869 | if (elapsed > 100ms) { |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | if (status == WOULD_BLOCK) { |
| 875 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 876 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | if (status != OK) { |
| 880 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 881 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 882 | } |
| 883 | if (event == nullptr) { |
| 884 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 885 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 886 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 887 | if (outEvent != nullptr) { |
| 888 | *outEvent = event; |
| 889 | } |
| 890 | return consumeSeq; |
| 891 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 892 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 893 | /** |
| 894 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 895 | */ |
| 896 | void finishEvent(uint32_t consumeSeq) { |
| 897 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 898 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 901 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 902 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 903 | ASSERT_EQ(OK, status); |
| 904 | } |
| 905 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 906 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 907 | std::optional<int32_t> expectedDisplayId, |
| 908 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 909 | InputEvent* event = consume(); |
| 910 | |
| 911 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 912 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 913 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 914 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 915 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 916 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 917 | if (expectedDisplayId.has_value()) { |
| 918 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 919 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 920 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 921 | switch (expectedEventType) { |
| 922 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 923 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 924 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 925 | if (expectedFlags.has_value()) { |
| 926 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 927 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 928 | break; |
| 929 | } |
| 930 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 931 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 932 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 933 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 934 | if (expectedFlags.has_value()) { |
| 935 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 936 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 937 | break; |
| 938 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 939 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 940 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 941 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 942 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 943 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 944 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 945 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { |
| 946 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 947 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 948 | case AINPUT_EVENT_TYPE_DRAG: { |
| 949 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 950 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 951 | default: { |
| 952 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 953 | } |
| 954 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 955 | } |
| 956 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 957 | MotionEvent* consumeMotion() { |
| 958 | InputEvent* event = consume(); |
| 959 | |
| 960 | if (event == nullptr) { |
| 961 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 962 | return nullptr; |
| 963 | } |
| 964 | |
| 965 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 966 | ADD_FAILURE() << mName << " expected a MotionEvent, got " |
| 967 | << inputEventTypeToString(event->getType()) << " event"; |
| 968 | return nullptr; |
| 969 | } |
| 970 | return static_cast<MotionEvent*>(event); |
| 971 | } |
| 972 | |
| 973 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 974 | MotionEvent* motionEvent = consumeMotion(); |
| 975 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 976 | ASSERT_THAT(*motionEvent, matcher); |
| 977 | } |
| 978 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 979 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 980 | InputEvent* event = consume(); |
| 981 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 982 | << ": consumer should have returned non-NULL event."; |
| 983 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 984 | << "Got " << inputEventTypeToString(event->getType()) |
| 985 | << " event instead of FOCUS event"; |
| 986 | |
| 987 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 988 | << mName.c_str() << ": event displayId should always be NONE."; |
| 989 | |
| 990 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 991 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 994 | void consumeCaptureEvent(bool hasCapture) { |
| 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_CAPTURE, event->getType()) |
| 999 | << "Got " << inputEventTypeToString(event->getType()) |
| 1000 | << " event instead of CAPTURE event"; |
| 1001 | |
| 1002 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1003 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1004 | |
| 1005 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1006 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1007 | } |
| 1008 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1009 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1010 | const InputEvent* event = consume(); |
| 1011 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1012 | << ": consumer should have returned non-NULL event."; |
| 1013 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 1014 | << "Got " << inputEventTypeToString(event->getType()) |
| 1015 | << " event instead of DRAG event"; |
| 1016 | |
| 1017 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1018 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1019 | |
| 1020 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1021 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1022 | EXPECT_EQ(x, dragEvent.getX()); |
| 1023 | EXPECT_EQ(y, dragEvent.getY()); |
| 1024 | } |
| 1025 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1026 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1027 | const InputEvent* event = consume(); |
| 1028 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1029 | << ": consumer should have returned non-NULL event."; |
| 1030 | ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType()) |
| 1031 | << "Got " << inputEventTypeToString(event->getType()) |
| 1032 | << " event instead of TOUCH_MODE event"; |
| 1033 | |
| 1034 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1035 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1036 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1037 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1038 | } |
| 1039 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1040 | void assertNoEvents() { |
| 1041 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1042 | if (event == nullptr) { |
| 1043 | return; |
| 1044 | } |
| 1045 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 1046 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1047 | ADD_FAILURE() << "Received key event " |
| 1048 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 1049 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 1050 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1051 | ADD_FAILURE() << "Received motion event " |
| 1052 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 1053 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 1054 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1055 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1056 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1057 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 1058 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1059 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1060 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1061 | } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) { |
| 1062 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1063 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1064 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1065 | } |
| 1066 | FAIL() << mName.c_str() |
| 1067 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1071 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1072 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1073 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1074 | protected: |
| 1075 | std::unique_ptr<InputConsumer> mConsumer; |
| 1076 | PreallocatedInputEventFactory mEventFactory; |
| 1077 | |
| 1078 | std::string mName; |
| 1079 | }; |
| 1080 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1081 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1082 | public: |
| 1083 | static const int32_t WIDTH = 600; |
| 1084 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1085 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1086 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1087 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1088 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1089 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1090 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1091 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1092 | dispatcher->createInputChannel(name); |
| 1093 | token = (*channel)->getConnectionToken(); |
| 1094 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | inputApplicationHandle->updateInfo(); |
| 1098 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1099 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1100 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1101 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1102 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1103 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1104 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1105 | mInfo.frameLeft = 0; |
| 1106 | mInfo.frameTop = 0; |
| 1107 | mInfo.frameRight = WIDTH; |
| 1108 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1109 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1110 | mInfo.globalScaleFactor = 1.0; |
| 1111 | mInfo.touchableRegion.clear(); |
| 1112 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1113 | mInfo.ownerPid = WINDOW_PID; |
| 1114 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1115 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1116 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1119 | sp<FakeWindowHandle> clone( |
| 1120 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1121 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1122 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1123 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1124 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1125 | return handle; |
| 1126 | } |
| 1127 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1128 | void setTouchable(bool touchable) { |
| 1129 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1130 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1131 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1132 | void setFocusable(bool focusable) { |
| 1133 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1134 | } |
| 1135 | |
| 1136 | void setVisible(bool visible) { |
| 1137 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1138 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1139 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1140 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1141 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1144 | void setPaused(bool paused) { |
| 1145 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1146 | } |
| 1147 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1148 | void setPreventSplitting(bool preventSplitting) { |
| 1149 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | void setSlippery(bool slippery) { |
| 1153 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1154 | } |
| 1155 | |
| 1156 | void setWatchOutsideTouch(bool watchOutside) { |
| 1157 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1158 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1159 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1160 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1161 | |
| 1162 | void setInterceptsStylus(bool interceptsStylus) { |
| 1163 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1164 | } |
| 1165 | |
| 1166 | void setDropInput(bool dropInput) { |
| 1167 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1168 | } |
| 1169 | |
| 1170 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1171 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1172 | } |
| 1173 | |
| 1174 | void setNoInputChannel(bool noInputChannel) { |
| 1175 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1176 | } |
| 1177 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1178 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1179 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1180 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1181 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1182 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1183 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1184 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1185 | mInfo.frameLeft = frame.left; |
| 1186 | mInfo.frameTop = frame.top; |
| 1187 | mInfo.frameRight = frame.right; |
| 1188 | mInfo.frameBottom = frame.bottom; |
| 1189 | mInfo.touchableRegion.clear(); |
| 1190 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1191 | |
| 1192 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1193 | ui::Transform translate; |
| 1194 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1195 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1198 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1199 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1200 | void setIsWallpaper(bool isWallpaper) { |
| 1201 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1202 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1203 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1204 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1205 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1206 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1207 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1208 | void setTrustedOverlay(bool trustedOverlay) { |
| 1209 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1210 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1211 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1212 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1213 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1214 | } |
| 1215 | |
| 1216 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1217 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1218 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1219 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1220 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1221 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1222 | expectedFlags); |
| 1223 | } |
| 1224 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1225 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1226 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 1227 | } |
| 1228 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1229 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1230 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1231 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1232 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1236 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1237 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1238 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1242 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1243 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1244 | } |
| 1245 | |
| 1246 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1247 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1248 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1249 | expectedFlags); |
| 1250 | } |
| 1251 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1252 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1253 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1254 | int32_t expectedFlags = 0) { |
| 1255 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1256 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1257 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1258 | } |
| 1259 | |
| 1260 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1261 | int32_t expectedFlags = 0) { |
| 1262 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1263 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1264 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1265 | } |
| 1266 | |
| 1267 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1268 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1269 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1270 | expectedFlags); |
| 1271 | } |
| 1272 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1273 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1274 | int32_t expectedFlags = 0) { |
| 1275 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1276 | expectedFlags); |
| 1277 | } |
| 1278 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1279 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1280 | int32_t expectedFlags = 0) { |
| 1281 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1282 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1283 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 1284 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1285 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1286 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1287 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1288 | } |
| 1289 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1290 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1291 | ASSERT_NE(mInputReceiver, nullptr) |
| 1292 | << "Cannot consume events from a window with no receiver"; |
| 1293 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1294 | } |
| 1295 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1296 | void consumeCaptureEvent(bool hasCapture) { |
| 1297 | ASSERT_NE(mInputReceiver, nullptr) |
| 1298 | << "Cannot consume events from a window with no receiver"; |
| 1299 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1300 | } |
| 1301 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1302 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1303 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1304 | 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] | 1305 | ASSERT_THAT(*motionEvent, matcher); |
| 1306 | } |
| 1307 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1308 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1309 | std::optional<int32_t> expectedDisplayId, |
| 1310 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1311 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1312 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1313 | expectedFlags); |
| 1314 | } |
| 1315 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1316 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1317 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1318 | } |
| 1319 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1320 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1321 | ASSERT_NE(mInputReceiver, nullptr) |
| 1322 | << "Cannot consume events from a window with no receiver"; |
| 1323 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1324 | } |
| 1325 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1326 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1327 | if (mInputReceiver == nullptr) { |
| 1328 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1329 | return std::nullopt; |
| 1330 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1331 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | void finishEvent(uint32_t sequenceNum) { |
| 1335 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1336 | mInputReceiver->finishEvent(sequenceNum); |
| 1337 | } |
| 1338 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1339 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1340 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1341 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1342 | } |
| 1343 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1344 | InputEvent* consume() { |
| 1345 | if (mInputReceiver == nullptr) { |
| 1346 | return nullptr; |
| 1347 | } |
| 1348 | return mInputReceiver->consume(); |
| 1349 | } |
| 1350 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1351 | MotionEvent* consumeMotion() { |
| 1352 | InputEvent* event = consume(); |
| 1353 | if (event == nullptr) { |
| 1354 | ADD_FAILURE() << "Consume failed : no event"; |
| 1355 | return nullptr; |
| 1356 | } |
| 1357 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 1358 | ADD_FAILURE() << "Instead of motion event, got " |
| 1359 | << inputEventTypeToString(event->getType()); |
| 1360 | return nullptr; |
| 1361 | } |
| 1362 | return static_cast<MotionEvent*>(event); |
| 1363 | } |
| 1364 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1365 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1366 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1367 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1368 | return; // Can't receive events if the window does not have input channel |
| 1369 | } |
| 1370 | ASSERT_NE(nullptr, mInputReceiver) |
| 1371 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1372 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1373 | } |
| 1374 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1375 | sp<IBinder> getToken() { return mInfo.token; } |
| 1376 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1377 | const std::string& getName() { return mName; } |
| 1378 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1379 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1380 | mInfo.ownerPid = ownerPid; |
| 1381 | mInfo.ownerUid = ownerUid; |
| 1382 | } |
| 1383 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1384 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1385 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1386 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1387 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1388 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1389 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1390 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1391 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1392 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1393 | 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] | 1394 | }; |
| 1395 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1396 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1397 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1398 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1399 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1400 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1401 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1402 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1403 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1404 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1405 | KeyEvent event; |
| 1406 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1407 | |
| 1408 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1409 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1410 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1411 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1412 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1413 | if (!allowKeyRepeat) { |
| 1414 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1415 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1416 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1417 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1420 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1421 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1422 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1425 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1426 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1427 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1428 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1429 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1430 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1431 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1432 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1435 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1436 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1437 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1440 | class PointerBuilder { |
| 1441 | public: |
| 1442 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1443 | mProperties.clear(); |
| 1444 | mProperties.id = id; |
| 1445 | mProperties.toolType = toolType; |
| 1446 | mCoords.clear(); |
| 1447 | } |
| 1448 | |
| 1449 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1450 | |
| 1451 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1452 | |
| 1453 | PointerBuilder& axis(int32_t axis, float value) { |
| 1454 | mCoords.setAxisValue(axis, value); |
| 1455 | return *this; |
| 1456 | } |
| 1457 | |
| 1458 | PointerProperties buildProperties() const { return mProperties; } |
| 1459 | |
| 1460 | PointerCoords buildCoords() const { return mCoords; } |
| 1461 | |
| 1462 | private: |
| 1463 | PointerProperties mProperties; |
| 1464 | PointerCoords mCoords; |
| 1465 | }; |
| 1466 | |
| 1467 | class MotionEventBuilder { |
| 1468 | public: |
| 1469 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1470 | mAction = action; |
| 1471 | mSource = source; |
| 1472 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1473 | mDownTime = mEventTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1476 | MotionEventBuilder& deviceId(int32_t deviceId) { |
| 1477 | mDeviceId = deviceId; |
| 1478 | return *this; |
| 1479 | } |
| 1480 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1481 | MotionEventBuilder& downTime(nsecs_t downTime) { |
| 1482 | mDownTime = downTime; |
| 1483 | return *this; |
| 1484 | } |
| 1485 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1486 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1487 | mEventTime = eventTime; |
| 1488 | return *this; |
| 1489 | } |
| 1490 | |
| 1491 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1492 | mDisplayId = displayId; |
| 1493 | return *this; |
| 1494 | } |
| 1495 | |
| 1496 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1497 | mActionButton = actionButton; |
| 1498 | return *this; |
| 1499 | } |
| 1500 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1501 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1502 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1503 | return *this; |
| 1504 | } |
| 1505 | |
| 1506 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1507 | mRawXCursorPosition = rawXCursorPosition; |
| 1508 | return *this; |
| 1509 | } |
| 1510 | |
| 1511 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1512 | mRawYCursorPosition = rawYCursorPosition; |
| 1513 | return *this; |
| 1514 | } |
| 1515 | |
| 1516 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1517 | mPointers.push_back(pointer); |
| 1518 | return *this; |
| 1519 | } |
| 1520 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1521 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1522 | mFlags |= flags; |
| 1523 | return *this; |
| 1524 | } |
| 1525 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1526 | MotionEvent build() { |
| 1527 | std::vector<PointerProperties> pointerProperties; |
| 1528 | std::vector<PointerCoords> pointerCoords; |
| 1529 | for (const PointerBuilder& pointer : mPointers) { |
| 1530 | pointerProperties.push_back(pointer.buildProperties()); |
| 1531 | pointerCoords.push_back(pointer.buildCoords()); |
| 1532 | } |
| 1533 | |
| 1534 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1535 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1536 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1537 | mPointers.size() == 1) { |
| 1538 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1539 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1540 | } |
| 1541 | |
| 1542 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1543 | ui::Transform identityTransform; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1544 | event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1545 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1546 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1547 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1548 | mRawYCursorPosition, identityTransform, mDownTime, mEventTime, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1549 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1550 | |
| 1551 | return event; |
| 1552 | } |
| 1553 | |
| 1554 | private: |
| 1555 | int32_t mAction; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1556 | int32_t mDeviceId = DEVICE_ID; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1557 | int32_t mSource; |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1558 | nsecs_t mDownTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1559 | nsecs_t mEventTime; |
| 1560 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1561 | int32_t mActionButton{0}; |
| 1562 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1563 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1564 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1565 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1566 | |
| 1567 | std::vector<PointerBuilder> mPointers; |
| 1568 | }; |
| 1569 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1570 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1571 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1572 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1573 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1574 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1575 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1576 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1579 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1580 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1581 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1582 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1583 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1584 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1585 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1586 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1587 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1588 | MotionEvent event = MotionEventBuilder(action, source) |
| 1589 | .displayId(displayId) |
| 1590 | .eventTime(eventTime) |
| 1591 | .rawXCursorPosition(cursorPosition.x) |
| 1592 | .rawYCursorPosition(cursorPosition.y) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1593 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1594 | .x(position.x) |
| 1595 | .y(position.y)) |
| 1596 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1597 | |
| 1598 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1599 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, |
| 1600 | policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1601 | } |
| 1602 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1603 | static InputEventInjectionResult injectMotionDown( |
| 1604 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1605 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1606 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1609 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1610 | int32_t source, int32_t displayId, |
| 1611 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1612 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1615 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1616 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1617 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1618 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1619 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1620 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1621 | |
| 1622 | return args; |
| 1623 | } |
| 1624 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1625 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1626 | const std::vector<PointF>& points) { |
| 1627 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1628 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1629 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1630 | } |
| 1631 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1632 | PointerProperties pointerProperties[pointerCount]; |
| 1633 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1634 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1635 | for (size_t i = 0; i < pointerCount; i++) { |
| 1636 | pointerProperties[i].clear(); |
| 1637 | pointerProperties[i].id = i; |
| 1638 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1639 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1640 | pointerCoords[i].clear(); |
| 1641 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1642 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1643 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1644 | |
| 1645 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1646 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1647 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1648 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1649 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1650 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1651 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1652 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1653 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1654 | |
| 1655 | return args; |
| 1656 | } |
| 1657 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1658 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1659 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1660 | } |
| 1661 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1662 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1663 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1664 | } |
| 1665 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1666 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1667 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1668 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1671 | /** |
| 1672 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1673 | * broken channel. |
| 1674 | */ |
| 1675 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1676 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1677 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1678 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1679 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1680 | |
| 1681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1682 | |
| 1683 | // Window closes its channel, but the window remains. |
| 1684 | window->destroyReceiver(); |
| 1685 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1686 | } |
| 1687 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1688 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1689 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1690 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1691 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1692 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1693 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1694 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1695 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1696 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1697 | |
| 1698 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1699 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1700 | } |
| 1701 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1702 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1704 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1705 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1706 | |
| 1707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1708 | // Inject a MotionEvent to an unknown display. |
| 1709 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1710 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1711 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1712 | |
| 1713 | // Window should receive motion event. |
| 1714 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1715 | } |
| 1716 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1717 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1718 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1719 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1720 | * called twice. |
| 1721 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1722 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1723 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1724 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1725 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1726 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1727 | |
| 1728 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1730 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1731 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1732 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1733 | |
| 1734 | // Window should receive motion event. |
| 1735 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1736 | } |
| 1737 | |
| 1738 | /** |
| 1739 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1740 | */ |
| 1741 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1742 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1743 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1744 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1745 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1746 | |
| 1747 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1748 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1749 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1750 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1751 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1752 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1753 | |
| 1754 | // Window should receive motion event. |
| 1755 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1756 | } |
| 1757 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1758 | // The foreground window should receive the first touch down event. |
| 1759 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1760 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1761 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1762 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1763 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1764 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1765 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1766 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1768 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1769 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1770 | |
| 1771 | // 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] | 1772 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1773 | windowSecond->assertNoEvents(); |
| 1774 | } |
| 1775 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1776 | /** |
| 1777 | * Two windows: A top window, and a wallpaper behind the window. |
| 1778 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1779 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1780 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1781 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1782 | */ |
| 1783 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1784 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1785 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1786 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1787 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1788 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1789 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1790 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1791 | |
| 1792 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1794 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1795 | {100, 200})) |
| 1796 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1797 | |
| 1798 | // Both foreground window and its wallpaper should receive the touch down |
| 1799 | foregroundWindow->consumeMotionDown(); |
| 1800 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1801 | |
| 1802 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1803 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1804 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1805 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1806 | |
| 1807 | foregroundWindow->consumeMotionMove(); |
| 1808 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1809 | |
| 1810 | // Now the foreground window goes away, but the wallpaper stays |
| 1811 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1812 | foregroundWindow->consumeMotionCancel(); |
| 1813 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1814 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1815 | } |
| 1816 | |
| 1817 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1818 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1819 | * with the following differences: |
| 1820 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1821 | * clean up the connection. |
| 1822 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1823 | * Ensure that there's no crash in the dispatcher. |
| 1824 | */ |
| 1825 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1826 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1827 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1828 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1829 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1830 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1831 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1832 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1833 | |
| 1834 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1835 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1836 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1837 | {100, 200})) |
| 1838 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1839 | |
| 1840 | // Both foreground window and its wallpaper should receive the touch down |
| 1841 | foregroundWindow->consumeMotionDown(); |
| 1842 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1843 | |
| 1844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1845 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1846 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1847 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1848 | |
| 1849 | foregroundWindow->consumeMotionMove(); |
| 1850 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1851 | |
| 1852 | // Wallpaper closes its channel, but the window remains. |
| 1853 | wallpaperWindow->destroyReceiver(); |
| 1854 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1855 | |
| 1856 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1857 | // is no longer valid. |
| 1858 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1859 | foregroundWindow->consumeMotionCancel(); |
| 1860 | } |
| 1861 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1862 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1863 | public ::testing::WithParamInterface<bool> {}; |
| 1864 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1865 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1866 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1867 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1868 | * The top window gets a multitouch gesture. |
| 1869 | * Ensure that wallpaper gets the same gesture. |
| 1870 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1871 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1872 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1873 | sp<FakeWindowHandle> foregroundWindow = |
| 1874 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1875 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1876 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1877 | |
| 1878 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1879 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1880 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1881 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1882 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1883 | |
| 1884 | // Touch down on top window |
| 1885 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1886 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1887 | {100, 100})) |
| 1888 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1889 | |
| 1890 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1891 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1892 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1893 | |
| 1894 | // Second finger down on the top window |
| 1895 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1896 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1897 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1898 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 1899 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1900 | .build(); |
| 1901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1902 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1903 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1904 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1905 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1906 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1907 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1908 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1909 | |
| 1910 | const MotionEvent secondFingerUpEvent = |
| 1911 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1912 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1913 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1914 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 1915 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1916 | .build(); |
| 1917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1918 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1919 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1920 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1921 | foregroundWindow->consumeMotionPointerUp(0); |
| 1922 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1923 | |
| 1924 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1925 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1926 | {100, 100})) |
| 1927 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1928 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1929 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | /** |
| 1933 | * Two windows: a window on the left and window on the right. |
| 1934 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1935 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1936 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1937 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1938 | * ACTION_POINTER_DOWN(1). |
| 1939 | */ |
| 1940 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1941 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1942 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1943 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1944 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1945 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1946 | |
| 1947 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1948 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1949 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1950 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1951 | |
| 1952 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1953 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1954 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1955 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1956 | |
| 1957 | mDispatcher->setInputWindows( |
| 1958 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1959 | |
| 1960 | // Touch down on left window |
| 1961 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1962 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1963 | {100, 100})) |
| 1964 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1965 | |
| 1966 | // Both foreground window and its wallpaper should receive the touch down |
| 1967 | leftWindow->consumeMotionDown(); |
| 1968 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1969 | |
| 1970 | // Second finger down on the right window |
| 1971 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1972 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1973 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1974 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 1975 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1976 | .build(); |
| 1977 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1978 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1979 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1980 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1981 | |
| 1982 | leftWindow->consumeMotionMove(); |
| 1983 | // Since the touch is split, right window gets ACTION_DOWN |
| 1984 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1985 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1986 | expectedWallpaperFlags); |
| 1987 | |
| 1988 | // Now, leftWindow, which received the first finger, disappears. |
| 1989 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1990 | leftWindow->consumeMotionCancel(); |
| 1991 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1992 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1993 | |
| 1994 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1995 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1996 | const MotionEvent secondFingerMoveEvent = |
| 1997 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1998 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1999 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2000 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2001 | .build(); |
| 2002 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2003 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2004 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2005 | rightWindow->consumeMotionMove(); |
| 2006 | |
| 2007 | leftWindow->assertNoEvents(); |
| 2008 | rightWindow->assertNoEvents(); |
| 2009 | wallpaperWindow->assertNoEvents(); |
| 2010 | } |
| 2011 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2012 | /** |
| 2013 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2014 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2015 | * The right window should receive ACTION_DOWN. |
| 2016 | */ |
| 2017 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2018 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2019 | sp<FakeWindowHandle> leftWindow = |
| 2020 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2021 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2022 | leftWindow->setDupTouchToWallpaper(true); |
| 2023 | leftWindow->setSlippery(true); |
| 2024 | |
| 2025 | sp<FakeWindowHandle> rightWindow = |
| 2026 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2027 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2028 | |
| 2029 | sp<FakeWindowHandle> wallpaperWindow = |
| 2030 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2031 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2032 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2033 | mDispatcher->setInputWindows( |
| 2034 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2035 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2036 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2037 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2038 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2039 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2040 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2041 | |
| 2042 | // Both foreground window and its wallpaper should receive the touch down |
| 2043 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2044 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2045 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2046 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2048 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2049 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2050 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2051 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2052 | leftWindow->consumeMotionCancel(); |
| 2053 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2054 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2057 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2058 | * Two windows: a window on the left and a window on the right. |
| 2059 | * Mouse is hovered from the right window into the left window. |
| 2060 | * Next, we tap on the left window, where the cursor was last seen. |
| 2061 | * The second tap is done onto the right window. |
| 2062 | * The mouse and tap are from two different devices. |
| 2063 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2064 | * explicitly helps during debugging. |
| 2065 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2066 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2067 | */ |
| 2068 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2069 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2070 | sp<FakeWindowHandle> leftWindow = |
| 2071 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2072 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2073 | |
| 2074 | sp<FakeWindowHandle> rightWindow = |
| 2075 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2076 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2077 | |
| 2078 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2079 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2080 | // stale. |
| 2081 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2082 | const int32_t mouseDeviceId = 6; |
| 2083 | const int32_t touchDeviceId = 4; |
| 2084 | // Move the cursor from right |
| 2085 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2086 | injectMotionEvent(mDispatcher, |
| 2087 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2088 | AINPUT_SOURCE_MOUSE) |
| 2089 | .deviceId(mouseDeviceId) |
| 2090 | .downTime(baseTime + 10) |
| 2091 | .eventTime(baseTime + 20) |
| 2092 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2093 | .x(300) |
| 2094 | .y(100)) |
| 2095 | .build())); |
| 2096 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2097 | |
| 2098 | // .. to the left window |
| 2099 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2100 | injectMotionEvent(mDispatcher, |
| 2101 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2102 | AINPUT_SOURCE_MOUSE) |
| 2103 | .deviceId(mouseDeviceId) |
| 2104 | .downTime(baseTime + 10) |
| 2105 | .eventTime(baseTime + 30) |
| 2106 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2107 | .x(110) |
| 2108 | .y(100)) |
| 2109 | .build())); |
| 2110 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2111 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2112 | // Now tap the left window |
| 2113 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2114 | injectMotionEvent(mDispatcher, |
| 2115 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2116 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2117 | .deviceId(touchDeviceId) |
| 2118 | .downTime(baseTime + 40) |
| 2119 | .eventTime(baseTime + 40) |
| 2120 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2121 | .x(100) |
| 2122 | .y(100)) |
| 2123 | .build())); |
| 2124 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2125 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2126 | |
| 2127 | // release tap |
| 2128 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2129 | injectMotionEvent(mDispatcher, |
| 2130 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2131 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2132 | .deviceId(touchDeviceId) |
| 2133 | .downTime(baseTime + 40) |
| 2134 | .eventTime(baseTime + 50) |
| 2135 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2136 | .x(100) |
| 2137 | .y(100)) |
| 2138 | .build())); |
| 2139 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2140 | |
| 2141 | // Tap the window on the right |
| 2142 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2143 | injectMotionEvent(mDispatcher, |
| 2144 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2145 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2146 | .deviceId(touchDeviceId) |
| 2147 | .downTime(baseTime + 60) |
| 2148 | .eventTime(baseTime + 60) |
| 2149 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2150 | .x(300) |
| 2151 | .y(100)) |
| 2152 | .build())); |
| 2153 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2154 | |
| 2155 | // release tap |
| 2156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2157 | injectMotionEvent(mDispatcher, |
| 2158 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2159 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2160 | .deviceId(touchDeviceId) |
| 2161 | .downTime(baseTime + 60) |
| 2162 | .eventTime(baseTime + 70) |
| 2163 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2164 | .x(300) |
| 2165 | .y(100)) |
| 2166 | .build())); |
| 2167 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2168 | |
| 2169 | // No more events |
| 2170 | leftWindow->assertNoEvents(); |
| 2171 | rightWindow->assertNoEvents(); |
| 2172 | } |
| 2173 | |
| 2174 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame^] | 2175 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2176 | * |
| 2177 | * Two windows: a window on the left and a window on the right. |
| 2178 | * Mouse is hovered over the left window. |
| 2179 | * Next, we tap on the left window, where the cursor was last seen. |
| 2180 | * |
| 2181 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2182 | * the left window. |
| 2183 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2184 | * window (first), and then another on the left window (second). |
| 2185 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2186 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2187 | */ |
| 2188 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2189 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2190 | sp<FakeWindowHandle> leftWindow = |
| 2191 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2192 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2193 | |
| 2194 | sp<FakeWindowHandle> rightWindow = |
| 2195 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2196 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2197 | |
| 2198 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2199 | |
| 2200 | const int32_t mouseDeviceId = 6; |
| 2201 | const int32_t touchDeviceId = 4; |
| 2202 | // Hover over the left window. Keep the cursor there. |
| 2203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2204 | injectMotionEvent(mDispatcher, |
| 2205 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2206 | AINPUT_SOURCE_MOUSE) |
| 2207 | .deviceId(mouseDeviceId) |
| 2208 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2209 | .x(50) |
| 2210 | .y(50)) |
| 2211 | .build())); |
| 2212 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2213 | |
| 2214 | // Tap on left window |
| 2215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2216 | injectMotionEvent(mDispatcher, |
| 2217 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2218 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2219 | .deviceId(touchDeviceId) |
| 2220 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2221 | .x(100) |
| 2222 | .y(100)) |
| 2223 | .build())); |
| 2224 | |
| 2225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2226 | injectMotionEvent(mDispatcher, |
| 2227 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2228 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2229 | .deviceId(touchDeviceId) |
| 2230 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2231 | .x(100) |
| 2232 | .y(100)) |
| 2233 | .build())); |
| 2234 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2235 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2236 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2237 | |
| 2238 | // First finger down on right window |
| 2239 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2240 | injectMotionEvent(mDispatcher, |
| 2241 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2242 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2243 | .deviceId(touchDeviceId) |
| 2244 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2245 | .x(300) |
| 2246 | .y(100)) |
| 2247 | .build())); |
| 2248 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2249 | |
| 2250 | // Second finger down on the left window |
| 2251 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2252 | injectMotionEvent(mDispatcher, |
| 2253 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2254 | .deviceId(touchDeviceId) |
| 2255 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2256 | .x(300) |
| 2257 | .y(100)) |
| 2258 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2259 | .x(100) |
| 2260 | .y(100)) |
| 2261 | .build())); |
| 2262 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2263 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2264 | |
| 2265 | // No more events |
| 2266 | leftWindow->assertNoEvents(); |
| 2267 | rightWindow->assertNoEvents(); |
| 2268 | } |
| 2269 | |
| 2270 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2271 | * On the display, have a single window, and also an area where there's no window. |
| 2272 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2273 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2274 | */ |
| 2275 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2276 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2277 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2278 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2279 | |
| 2280 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 2281 | NotifyMotionArgs args; |
| 2282 | |
| 2283 | // Touch down on the empty space |
| 2284 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 2285 | |
| 2286 | mDispatcher->waitForIdle(); |
| 2287 | window->assertNoEvents(); |
| 2288 | |
| 2289 | // Now touch down on the window with another pointer |
| 2290 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 2291 | mDispatcher->waitForIdle(); |
| 2292 | window->consumeMotionDown(); |
| 2293 | } |
| 2294 | |
| 2295 | /** |
| 2296 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2297 | * non-touchable window. |
| 2298 | */ |
| 2299 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2300 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2301 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2302 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2303 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2304 | window1->setTouchable(false); |
| 2305 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2306 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2307 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2308 | |
| 2309 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2310 | |
| 2311 | NotifyMotionArgs args; |
| 2312 | // Touch down on the non-touchable window |
| 2313 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2314 | |
| 2315 | mDispatcher->waitForIdle(); |
| 2316 | window1->assertNoEvents(); |
| 2317 | window2->assertNoEvents(); |
| 2318 | |
| 2319 | // Now touch down on the window with another pointer |
| 2320 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2321 | mDispatcher->waitForIdle(); |
| 2322 | window2->consumeMotionDown(); |
| 2323 | } |
| 2324 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2325 | /** |
| 2326 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2327 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2328 | */ |
| 2329 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2330 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2331 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2332 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2333 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2334 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2335 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2336 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2337 | |
| 2338 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2339 | |
| 2340 | NotifyMotionArgs args; |
| 2341 | // Touch down on the first window |
| 2342 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2343 | |
| 2344 | mDispatcher->waitForIdle(); |
| 2345 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2346 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2347 | window2->assertNoEvents(); |
| 2348 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2349 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2350 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2351 | |
| 2352 | // Now touch down on the window with another pointer |
| 2353 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2354 | mDispatcher->waitForIdle(); |
| 2355 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2356 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2357 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2358 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2359 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2360 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2361 | |
| 2362 | // Now move the pointer on the second window |
| 2363 | mDispatcher->notifyMotion( |
| 2364 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); |
| 2365 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2366 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2367 | |
| 2368 | // Now add new touch down on the second window |
| 2369 | mDispatcher->notifyMotion( |
| 2370 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); |
| 2371 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2372 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2373 | |
| 2374 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2375 | window1->consumeMotionMove(); |
| 2376 | window1->assertNoEvents(); |
| 2377 | |
| 2378 | // Now move the pointer on the first window |
| 2379 | mDispatcher->notifyMotion( |
| 2380 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}}))); |
| 2381 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2382 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2383 | |
| 2384 | mDispatcher->notifyMotion(&( |
| 2385 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); |
| 2386 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2387 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2390 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2391 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2392 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2393 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2394 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2395 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2396 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2397 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2398 | |
| 2399 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2400 | |
| 2401 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 2402 | |
| 2403 | // 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] | 2404 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2405 | injectMotionEvent(mDispatcher, |
| 2406 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2407 | AINPUT_SOURCE_MOUSE) |
| 2408 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2409 | .x(900) |
| 2410 | .y(400)) |
| 2411 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2412 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2413 | |
| 2414 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2416 | injectMotionEvent(mDispatcher, |
| 2417 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2418 | AINPUT_SOURCE_MOUSE) |
| 2419 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2420 | .x(300) |
| 2421 | .y(400)) |
| 2422 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2423 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2424 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2425 | |
| 2426 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2428 | injectMotionEvent(mDispatcher, |
| 2429 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2430 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2431 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2432 | .x(300) |
| 2433 | .y(400)) |
| 2434 | .build())); |
| 2435 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2436 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2437 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2438 | injectMotionEvent(mDispatcher, |
| 2439 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2440 | AINPUT_SOURCE_MOUSE) |
| 2441 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2442 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2443 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2444 | .x(300) |
| 2445 | .y(400)) |
| 2446 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2447 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2448 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2449 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2450 | injectMotionEvent(mDispatcher, |
| 2451 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2452 | AINPUT_SOURCE_MOUSE) |
| 2453 | .buttonState(0) |
| 2454 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2455 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2456 | .x(300) |
| 2457 | .y(400)) |
| 2458 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2459 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2460 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2461 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2462 | injectMotionEvent(mDispatcher, |
| 2463 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2464 | .buttonState(0) |
| 2465 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2466 | .x(300) |
| 2467 | .y(400)) |
| 2468 | .build())); |
| 2469 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2470 | |
| 2471 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2473 | injectMotionEvent(mDispatcher, |
| 2474 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2475 | AINPUT_SOURCE_MOUSE) |
| 2476 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2477 | .x(900) |
| 2478 | .y(400)) |
| 2479 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2480 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2481 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2482 | |
| 2483 | // No more events |
| 2484 | windowLeft->assertNoEvents(); |
| 2485 | windowRight->assertNoEvents(); |
| 2486 | } |
| 2487 | |
| 2488 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 2489 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2490 | |
| 2491 | sp<FakeWindowHandle> spyWindow = |
| 2492 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2493 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2494 | spyWindow->setTrustedOverlay(true); |
| 2495 | spyWindow->setSpy(true); |
| 2496 | sp<FakeWindowHandle> window = |
| 2497 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2498 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2499 | |
| 2500 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2501 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2502 | |
| 2503 | // Send mouse cursor to the window |
| 2504 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2505 | injectMotionEvent(mDispatcher, |
| 2506 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2507 | AINPUT_SOURCE_MOUSE) |
| 2508 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2509 | .x(100) |
| 2510 | .y(100)) |
| 2511 | .build())); |
| 2512 | |
| 2513 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2514 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2515 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2516 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2517 | |
| 2518 | window->assertNoEvents(); |
| 2519 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2520 | } |
| 2521 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2522 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 2523 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2524 | |
| 2525 | sp<FakeWindowHandle> spyWindow = |
| 2526 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2527 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2528 | spyWindow->setTrustedOverlay(true); |
| 2529 | spyWindow->setSpy(true); |
| 2530 | sp<FakeWindowHandle> window = |
| 2531 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2532 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2533 | |
| 2534 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2536 | |
| 2537 | // Send mouse cursor to the window |
| 2538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2539 | injectMotionEvent(mDispatcher, |
| 2540 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2541 | AINPUT_SOURCE_MOUSE) |
| 2542 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2543 | .x(100) |
| 2544 | .y(100)) |
| 2545 | .build())); |
| 2546 | |
| 2547 | // Move mouse cursor |
| 2548 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2549 | injectMotionEvent(mDispatcher, |
| 2550 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2551 | AINPUT_SOURCE_MOUSE) |
| 2552 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2553 | .x(110) |
| 2554 | .y(110)) |
| 2555 | .build())); |
| 2556 | |
| 2557 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2558 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2559 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2560 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2561 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2562 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2563 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2564 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2565 | // Touch down on the window |
| 2566 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2567 | injectMotionEvent(mDispatcher, |
| 2568 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2569 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2570 | .deviceId(SECOND_DEVICE_ID) |
| 2571 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2572 | .x(200) |
| 2573 | .y(200)) |
| 2574 | .build())); |
| 2575 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2576 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2577 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2578 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2579 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2580 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2581 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2582 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2583 | |
| 2584 | // pilfer the motion, retaining the gesture on the spy window. |
| 2585 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2586 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 2587 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2588 | |
| 2589 | // Touch UP on the window |
| 2590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2591 | injectMotionEvent(mDispatcher, |
| 2592 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2593 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2594 | .deviceId(SECOND_DEVICE_ID) |
| 2595 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2596 | .x(200) |
| 2597 | .y(200)) |
| 2598 | .build())); |
| 2599 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2600 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2601 | |
| 2602 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 2603 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 2604 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 2605 | |
| 2606 | // One more tap - DOWN |
| 2607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2608 | injectMotionEvent(mDispatcher, |
| 2609 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2610 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2611 | .deviceId(SECOND_DEVICE_ID) |
| 2612 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2613 | .x(250) |
| 2614 | .y(250)) |
| 2615 | .build())); |
| 2616 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2617 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2618 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2619 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2620 | |
| 2621 | // Touch UP on the window |
| 2622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2623 | injectMotionEvent(mDispatcher, |
| 2624 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2625 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2626 | .deviceId(SECOND_DEVICE_ID) |
| 2627 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2628 | .x(250) |
| 2629 | .y(250)) |
| 2630 | .build())); |
| 2631 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2632 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2633 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2634 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2635 | |
| 2636 | window->assertNoEvents(); |
| 2637 | spyWindow->assertNoEvents(); |
| 2638 | } |
| 2639 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2640 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 2641 | // directly in this test. |
| 2642 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2643 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2644 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2645 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2646 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2647 | |
| 2648 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2649 | |
| 2650 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2651 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2653 | injectMotionEvent(mDispatcher, |
| 2654 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2655 | AINPUT_SOURCE_MOUSE) |
| 2656 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2657 | .x(300) |
| 2658 | .y(400)) |
| 2659 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2660 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2661 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2663 | injectMotionEvent(mDispatcher, |
| 2664 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2665 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2666 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2667 | .x(300) |
| 2668 | .y(400)) |
| 2669 | .build())); |
| 2670 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2671 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2672 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2673 | injectMotionEvent(mDispatcher, |
| 2674 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2675 | AINPUT_SOURCE_MOUSE) |
| 2676 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2677 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2678 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2679 | .x(300) |
| 2680 | .y(400)) |
| 2681 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2682 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2683 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2685 | injectMotionEvent(mDispatcher, |
| 2686 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2687 | AINPUT_SOURCE_MOUSE) |
| 2688 | .buttonState(0) |
| 2689 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2690 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2691 | .x(300) |
| 2692 | .y(400)) |
| 2693 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2694 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2695 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2697 | injectMotionEvent(mDispatcher, |
| 2698 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2699 | .buttonState(0) |
| 2700 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2701 | .x(300) |
| 2702 | .y(400)) |
| 2703 | .build())); |
| 2704 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2705 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2706 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2707 | injectMotionEvent(mDispatcher, |
| 2708 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2709 | AINPUT_SOURCE_MOUSE) |
| 2710 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2711 | .x(300) |
| 2712 | .y(400)) |
| 2713 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2714 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2715 | } |
| 2716 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2717 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2718 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 2719 | * is generated. |
| 2720 | */ |
| 2721 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 2722 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2723 | sp<FakeWindowHandle> window = |
| 2724 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2725 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 2726 | |
| 2727 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2728 | |
| 2729 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2730 | |
| 2731 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2732 | injectMotionEvent(mDispatcher, |
| 2733 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2734 | AINPUT_SOURCE_MOUSE) |
| 2735 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2736 | .x(300) |
| 2737 | .y(400)) |
| 2738 | .build())); |
| 2739 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2740 | |
| 2741 | // Remove the window, but keep the channel. |
| 2742 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 2743 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2744 | } |
| 2745 | |
| 2746 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2747 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2748 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 2749 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2750 | */ |
| 2751 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 2752 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2753 | sp<FakeWindowHandle> window = |
| 2754 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2755 | window->setFrame(Rect(0, 0, 100, 100)); |
| 2756 | |
| 2757 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2758 | |
| 2759 | // Inject a hover_move from mouse. |
| 2760 | NotifyMotionArgs motionArgs = |
| 2761 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 2762 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2763 | motionArgs.xCursorPosition = 50; |
| 2764 | motionArgs.yCursorPosition = 50; |
| 2765 | mDispatcher->notifyMotion(&motionArgs); |
| 2766 | ASSERT_NO_FATAL_FAILURE( |
| 2767 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2768 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2769 | |
| 2770 | // Tap on the window |
| 2771 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2772 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2773 | mDispatcher->notifyMotion(&motionArgs); |
| 2774 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2775 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2776 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 2777 | |
| 2778 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2779 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2780 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2781 | |
| 2782 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2783 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2784 | mDispatcher->notifyMotion(&motionArgs); |
| 2785 | ASSERT_NO_FATAL_FAILURE( |
| 2786 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2787 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2788 | } |
| 2789 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2790 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 2791 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2792 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 2793 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 2794 | ADISPLAY_ID_DEFAULT); |
| 2795 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2796 | sp<FakeWindowHandle> windowSecondDisplay = |
| 2797 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 2798 | SECOND_DISPLAY_ID); |
| 2799 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2800 | |
| 2801 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2802 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2803 | |
| 2804 | // Set cursor position in window in default display and check that hover enter and move |
| 2805 | // events are generated. |
| 2806 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2807 | injectMotionEvent(mDispatcher, |
| 2808 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2809 | AINPUT_SOURCE_MOUSE) |
| 2810 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2811 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2812 | .x(300) |
| 2813 | .y(600)) |
| 2814 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2815 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2816 | |
| 2817 | // Remove all windows in secondary display and check that no event happens on window in |
| 2818 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2819 | mDispatcher->setInputWindows( |
| 2820 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2821 | windowDefaultDisplay->assertNoEvents(); |
| 2822 | |
| 2823 | // Move cursor position in window in default display and check that only hover move |
| 2824 | // event is generated and not hover enter event. |
| 2825 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2826 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2827 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2828 | injectMotionEvent(mDispatcher, |
| 2829 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2830 | AINPUT_SOURCE_MOUSE) |
| 2831 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2832 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2833 | .x(400) |
| 2834 | .y(700)) |
| 2835 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2836 | windowDefaultDisplay->consumeMotionEvent( |
| 2837 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2838 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2839 | windowDefaultDisplay->assertNoEvents(); |
| 2840 | } |
| 2841 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2842 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2843 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2844 | |
| 2845 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2846 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2847 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2848 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2849 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2850 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2851 | |
| 2852 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2853 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2854 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2855 | |
| 2856 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 2857 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2858 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2859 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2860 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2861 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2862 | windowRight->assertNoEvents(); |
| 2863 | } |
| 2864 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2865 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2866 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2867 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2868 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2869 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2870 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2871 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2872 | setFocusedWindow(window); |
| 2873 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2874 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2875 | |
| 2876 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2877 | mDispatcher->notifyKey(&keyArgs); |
| 2878 | |
| 2879 | // Window should receive key down event. |
| 2880 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2881 | |
| 2882 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 2883 | // on the app side. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2884 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2885 | mDispatcher->notifyDeviceReset(&args); |
| 2886 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2887 | AKEY_EVENT_FLAG_CANCELED); |
| 2888 | } |
| 2889 | |
| 2890 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2891 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2892 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2893 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2894 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2895 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2896 | |
| 2897 | NotifyMotionArgs motionArgs = |
| 2898 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2899 | ADISPLAY_ID_DEFAULT); |
| 2900 | mDispatcher->notifyMotion(&motionArgs); |
| 2901 | |
| 2902 | // Window should receive motion down event. |
| 2903 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2904 | |
| 2905 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 2906 | // on the app side. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2907 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2908 | mDispatcher->notifyDeviceReset(&args); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 2909 | window->consumeMotionEvent( |
| 2910 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2911 | } |
| 2912 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2913 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 2914 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2915 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2916 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2917 | window->setFocusable(true); |
| 2918 | |
| 2919 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2920 | setFocusedWindow(window); |
| 2921 | |
| 2922 | window->consumeFocusEvent(true); |
| 2923 | |
| 2924 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2925 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 2926 | const nsecs_t injectTime = keyArgs.eventTime; |
| 2927 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 2928 | mDispatcher->notifyKey(&keyArgs); |
| 2929 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 2930 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2931 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 2932 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 2933 | } |
| 2934 | |
| 2935 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 2936 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2937 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2938 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2939 | window->setFocusable(true); |
| 2940 | |
| 2941 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2942 | setFocusedWindow(window); |
| 2943 | |
| 2944 | window->consumeFocusEvent(true); |
| 2945 | |
| 2946 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2947 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2948 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 2949 | mDispatcher->notifyKey(&keyDown); |
| 2950 | mDispatcher->notifyKey(&keyUp); |
| 2951 | |
| 2952 | // Window should receive key event immediately when same key up. |
| 2953 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2954 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2955 | } |
| 2956 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2957 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 2958 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 2959 | * WATCH_OUTSIDE_TOUCH. |
| 2960 | * Both windows are owned by the same UID. |
| 2961 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 2962 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 2963 | */ |
| 2964 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 2965 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2966 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2967 | "First Window", ADISPLAY_ID_DEFAULT); |
| 2968 | window->setFrame(Rect{0, 0, 100, 100}); |
| 2969 | |
| 2970 | sp<FakeWindowHandle> outsideWindow = |
| 2971 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2972 | ADISPLAY_ID_DEFAULT); |
| 2973 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 2974 | outsideWindow->setWatchOutsideTouch(true); |
| 2975 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 2976 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 2977 | |
| 2978 | // Tap on first window. |
| 2979 | NotifyMotionArgs motionArgs = |
| 2980 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2981 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 2982 | mDispatcher->notifyMotion(&motionArgs); |
| 2983 | window->consumeMotionDown(); |
| 2984 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 2985 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 2986 | outsideWindow->consumeMotionEvent( |
| 2987 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 2988 | } |
| 2989 | |
| 2990 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2991 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 2992 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 2993 | * ACTION_OUTSIDE event is sent per gesture. |
| 2994 | */ |
| 2995 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 2996 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 2997 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2998 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2999 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3000 | window->setWatchOutsideTouch(true); |
| 3001 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3002 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3003 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3004 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3005 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3006 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3007 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3008 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3009 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3010 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3011 | |
| 3012 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 3013 | NotifyMotionArgs motionArgs = |
| 3014 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3015 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 3016 | mDispatcher->notifyMotion(&motionArgs); |
| 3017 | window->assertNoEvents(); |
| 3018 | secondWindow->assertNoEvents(); |
| 3019 | |
| 3020 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3021 | // Now, `window` should get ACTION_OUTSIDE. |
| 3022 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3023 | {PointF{-10, -10}, PointF{105, 105}}); |
| 3024 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3025 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3026 | window->consumeMotionEvent( |
| 3027 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3028 | secondWindow->consumeMotionDown(); |
| 3029 | thirdWindow->assertNoEvents(); |
| 3030 | |
| 3031 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3032 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 3033 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3034 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 3035 | mDispatcher->notifyMotion(&motionArgs); |
| 3036 | window->assertNoEvents(); |
| 3037 | secondWindow->consumeMotionMove(); |
| 3038 | thirdWindow->consumeMotionDown(); |
| 3039 | } |
| 3040 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3041 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3042 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3043 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3044 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3045 | window->setFocusable(true); |
| 3046 | |
| 3047 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 3048 | setFocusedWindow(window); |
| 3049 | |
| 3050 | window->consumeFocusEvent(true); |
| 3051 | |
| 3052 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3053 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3054 | mDispatcher->notifyKey(&keyDown); |
| 3055 | mDispatcher->notifyKey(&keyUp); |
| 3056 | |
| 3057 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3058 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3059 | |
| 3060 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
| 3061 | mDispatcher->onWindowInfosChanged({}, {}); |
| 3062 | |
| 3063 | window->consumeFocusEvent(false); |
| 3064 | |
| 3065 | mDispatcher->notifyKey(&keyDown); |
| 3066 | mDispatcher->notifyKey(&keyUp); |
| 3067 | window->assertNoEvents(); |
| 3068 | } |
| 3069 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3070 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3071 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3072 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3073 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3074 | // Ensure window is non-split and have some transform. |
| 3075 | window->setPreventSplitting(true); |
| 3076 | window->setWindowOffset(20, 40); |
| 3077 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 3078 | |
| 3079 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3080 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3081 | {50, 50})) |
| 3082 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3083 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3084 | |
| 3085 | const MotionEvent secondFingerDownEvent = |
| 3086 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3087 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3088 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3089 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 3090 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3091 | .build(); |
| 3092 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3093 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3094 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3095 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3096 | |
| 3097 | const MotionEvent* event = window->consumeMotion(); |
| 3098 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3099 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3100 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3101 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3102 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3103 | } |
| 3104 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3105 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3106 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 3107 | * |
| 3108 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 3109 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 3110 | * space. |
| 3111 | */ |
| 3112 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 3113 | public: |
| 3114 | void SetUp() override { |
| 3115 | InputDispatcherTest::SetUp(); |
| 3116 | mDisplayInfos.clear(); |
| 3117 | mWindowInfos.clear(); |
| 3118 | } |
| 3119 | |
| 3120 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 3121 | gui::DisplayInfo info; |
| 3122 | info.displayId = displayId; |
| 3123 | info.transform = transform; |
| 3124 | mDisplayInfos.push_back(std::move(info)); |
| 3125 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3126 | } |
| 3127 | |
| 3128 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 3129 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 3130 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3131 | } |
| 3132 | |
| 3133 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 3134 | // on the display. |
| 3135 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 3136 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 3137 | // respectively. |
| 3138 | ui::Transform displayTransform; |
| 3139 | displayTransform.set(2, 0, 0, 4); |
| 3140 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 3141 | |
| 3142 | std::shared_ptr<FakeApplicationHandle> application = |
| 3143 | std::make_shared<FakeApplicationHandle>(); |
| 3144 | |
| 3145 | // Add two windows to the display. Their frames are represented in the display space. |
| 3146 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3147 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3148 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3149 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 3150 | addWindow(firstWindow); |
| 3151 | |
| 3152 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3153 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3154 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3155 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 3156 | addWindow(secondWindow); |
| 3157 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 3158 | } |
| 3159 | |
| 3160 | private: |
| 3161 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 3162 | std::vector<gui::WindowInfo> mWindowInfos; |
| 3163 | }; |
| 3164 | |
| 3165 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 3166 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3167 | // Send down to the first window. The point is represented in the display space. The point is |
| 3168 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 3169 | // end up in the incorrect window. |
| 3170 | NotifyMotionArgs downMotionArgs = |
| 3171 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3172 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 3173 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3174 | |
| 3175 | firstWindow->consumeMotionDown(); |
| 3176 | secondWindow->assertNoEvents(); |
| 3177 | } |
| 3178 | |
| 3179 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 3180 | // the event should be treated as being in the logical display space. |
| 3181 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 3182 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3183 | // Send down to the first window. The point is represented in the logical display space. The |
| 3184 | // point is selected so that if the hit test was done in logical display space, then it would |
| 3185 | // end up in the incorrect window. |
| 3186 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3187 | PointF{75 * 2, 55 * 4}); |
| 3188 | |
| 3189 | firstWindow->consumeMotionDown(); |
| 3190 | secondWindow->assertNoEvents(); |
| 3191 | } |
| 3192 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3193 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 3194 | // event should be treated as being in the logical display space. |
| 3195 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 3196 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3197 | |
| 3198 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 3199 | ui::Transform injectedEventTransform; |
| 3200 | injectedEventTransform.set(matrix); |
| 3201 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 3202 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 3203 | |
| 3204 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3205 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3206 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3207 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3208 | .x(untransformedPoint.x) |
| 3209 | .y(untransformedPoint.y)) |
| 3210 | .build(); |
| 3211 | event.transform(matrix); |
| 3212 | |
| 3213 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 3214 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 3215 | |
| 3216 | firstWindow->consumeMotionDown(); |
| 3217 | secondWindow->assertNoEvents(); |
| 3218 | } |
| 3219 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3220 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 3221 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3222 | |
| 3223 | // Send down to the second window. |
| 3224 | NotifyMotionArgs downMotionArgs = |
| 3225 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3226 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 3227 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3228 | |
| 3229 | firstWindow->assertNoEvents(); |
| 3230 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 3231 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 3232 | |
| 3233 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 3234 | EXPECT_EQ(300, event->getRawX(0)); |
| 3235 | EXPECT_EQ(880, event->getRawY(0)); |
| 3236 | |
| 3237 | // Ensure that the x and y values are in the window's coordinate space. |
| 3238 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 3239 | // the logical display space. This will be the origin of the window space. |
| 3240 | EXPECT_EQ(100, event->getX(0)); |
| 3241 | EXPECT_EQ(80, event->getY(0)); |
| 3242 | } |
| 3243 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3244 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3245 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3246 | |
| 3247 | class TransferTouchFixture : public InputDispatcherTest, |
| 3248 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 3249 | |
| 3250 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3251 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3252 | |
| 3253 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3254 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3255 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3256 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3257 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3258 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3259 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3260 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3261 | sp<FakeWindowHandle> wallpaper = |
| 3262 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 3263 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3264 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3265 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3266 | |
| 3267 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3268 | NotifyMotionArgs downMotionArgs = |
| 3269 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3270 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3271 | mDispatcher->notifyMotion(&downMotionArgs); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3272 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3273 | // Only the first window should get the down event |
| 3274 | firstWindow->consumeMotionDown(); |
| 3275 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3276 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3277 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3278 | // Transfer touch to the second window |
| 3279 | TransferFunction f = GetParam(); |
| 3280 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3281 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3282 | // The first window gets cancel and the second gets down |
| 3283 | firstWindow->consumeMotionCancel(); |
| 3284 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3285 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3286 | |
| 3287 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3288 | NotifyMotionArgs upMotionArgs = |
| 3289 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3290 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3291 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3292 | // The first window gets no events and the second gets up |
| 3293 | firstWindow->assertNoEvents(); |
| 3294 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3295 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3296 | } |
| 3297 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3298 | /** |
| 3299 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 3300 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 3301 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 3302 | * natural to the user. |
| 3303 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 3304 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 3305 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 3306 | * the first window to the second. |
| 3307 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 3308 | * the other API, as well. |
| 3309 | */ |
| 3310 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 3311 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3312 | |
| 3313 | // Create a couple of windows + a spy window |
| 3314 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3315 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3316 | spyWindow->setTrustedOverlay(true); |
| 3317 | spyWindow->setSpy(true); |
| 3318 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3319 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3320 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3321 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3322 | |
| 3323 | // Add the windows to the dispatcher |
| 3324 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 3325 | |
| 3326 | // Send down to the first window |
| 3327 | NotifyMotionArgs downMotionArgs = |
| 3328 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3329 | ADISPLAY_ID_DEFAULT); |
| 3330 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3331 | // Only the first window and spy should get the down event |
| 3332 | spyWindow->consumeMotionDown(); |
| 3333 | firstWindow->consumeMotionDown(); |
| 3334 | |
| 3335 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 3336 | // if f === 'transferTouch'. |
| 3337 | TransferFunction f = GetParam(); |
| 3338 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3339 | ASSERT_TRUE(success); |
| 3340 | // The first window gets cancel and the second gets down |
| 3341 | firstWindow->consumeMotionCancel(); |
| 3342 | secondWindow->consumeMotionDown(); |
| 3343 | |
| 3344 | // Send up event to the second window |
| 3345 | NotifyMotionArgs upMotionArgs = |
| 3346 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3347 | ADISPLAY_ID_DEFAULT); |
| 3348 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3349 | // The first window gets no events and the second+spy get up |
| 3350 | firstWindow->assertNoEvents(); |
| 3351 | spyWindow->consumeMotionUp(); |
| 3352 | secondWindow->consumeMotionUp(); |
| 3353 | } |
| 3354 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3355 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3356 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3357 | |
| 3358 | PointF touchPoint = {10, 10}; |
| 3359 | |
| 3360 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3361 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3362 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3363 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3364 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3365 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3366 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3367 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3368 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3369 | |
| 3370 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3371 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3372 | |
| 3373 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3374 | NotifyMotionArgs downMotionArgs = |
| 3375 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3376 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3377 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3378 | // Only the first window should get the down event |
| 3379 | firstWindow->consumeMotionDown(); |
| 3380 | secondWindow->assertNoEvents(); |
| 3381 | |
| 3382 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3383 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3384 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3385 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3386 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 3387 | // Only the first window should get the pointer down event |
| 3388 | firstWindow->consumeMotionPointerDown(1); |
| 3389 | secondWindow->assertNoEvents(); |
| 3390 | |
| 3391 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3392 | TransferFunction f = GetParam(); |
| 3393 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3394 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3395 | // The first window gets cancel and the second gets down and pointer down |
| 3396 | firstWindow->consumeMotionCancel(); |
| 3397 | secondWindow->consumeMotionDown(); |
| 3398 | secondWindow->consumeMotionPointerDown(1); |
| 3399 | |
| 3400 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3401 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3402 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3403 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3404 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3405 | // The first window gets nothing and the second gets pointer up |
| 3406 | firstWindow->assertNoEvents(); |
| 3407 | secondWindow->consumeMotionPointerUp(1); |
| 3408 | |
| 3409 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3410 | NotifyMotionArgs upMotionArgs = |
| 3411 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3412 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3413 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3414 | // The first window gets nothing and the second gets up |
| 3415 | firstWindow->assertNoEvents(); |
| 3416 | secondWindow->consumeMotionUp(); |
| 3417 | } |
| 3418 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3419 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 3420 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3421 | |
| 3422 | // Create a couple of windows |
| 3423 | sp<FakeWindowHandle> firstWindow = |
| 3424 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3425 | ADISPLAY_ID_DEFAULT); |
| 3426 | firstWindow->setDupTouchToWallpaper(true); |
| 3427 | sp<FakeWindowHandle> secondWindow = |
| 3428 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3429 | ADISPLAY_ID_DEFAULT); |
| 3430 | secondWindow->setDupTouchToWallpaper(true); |
| 3431 | |
| 3432 | sp<FakeWindowHandle> wallpaper1 = |
| 3433 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 3434 | wallpaper1->setIsWallpaper(true); |
| 3435 | |
| 3436 | sp<FakeWindowHandle> wallpaper2 = |
| 3437 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 3438 | wallpaper2->setIsWallpaper(true); |
| 3439 | // Add the windows to the dispatcher |
| 3440 | mDispatcher->setInputWindows( |
| 3441 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 3442 | |
| 3443 | // Send down to the first window |
| 3444 | NotifyMotionArgs downMotionArgs = |
| 3445 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3446 | ADISPLAY_ID_DEFAULT); |
| 3447 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3448 | |
| 3449 | // Only the first window should get the down event |
| 3450 | firstWindow->consumeMotionDown(); |
| 3451 | secondWindow->assertNoEvents(); |
| 3452 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3453 | wallpaper2->assertNoEvents(); |
| 3454 | |
| 3455 | // Transfer touch focus to the second window |
| 3456 | TransferFunction f = GetParam(); |
| 3457 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3458 | ASSERT_TRUE(success); |
| 3459 | |
| 3460 | // The first window gets cancel and the second gets down |
| 3461 | firstWindow->consumeMotionCancel(); |
| 3462 | secondWindow->consumeMotionDown(); |
| 3463 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3464 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3465 | |
| 3466 | // Send up event to the second window |
| 3467 | NotifyMotionArgs upMotionArgs = |
| 3468 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3469 | ADISPLAY_ID_DEFAULT); |
| 3470 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3471 | // The first window gets no events and the second gets up |
| 3472 | firstWindow->assertNoEvents(); |
| 3473 | secondWindow->consumeMotionUp(); |
| 3474 | wallpaper1->assertNoEvents(); |
| 3475 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3476 | } |
| 3477 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3478 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 3479 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 3480 | // for the case where there are multiple pointers split across several windows. |
| 3481 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 3482 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3483 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3484 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3485 | return dispatcher->transferTouch(destChannelToken, |
| 3486 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3487 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3488 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3489 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3490 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3491 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3492 | })); |
| 3493 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3494 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3495 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3496 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3497 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3498 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3499 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3500 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3501 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3502 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3503 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3504 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3505 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3506 | |
| 3507 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3508 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3509 | |
| 3510 | PointF pointInFirst = {300, 200}; |
| 3511 | PointF pointInSecond = {300, 600}; |
| 3512 | |
| 3513 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3514 | NotifyMotionArgs firstDownMotionArgs = |
| 3515 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3516 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3517 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3518 | // Only the first window should get the down event |
| 3519 | firstWindow->consumeMotionDown(); |
| 3520 | secondWindow->assertNoEvents(); |
| 3521 | |
| 3522 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3523 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3524 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3525 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3526 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3527 | // The first window gets a move and the second a down |
| 3528 | firstWindow->consumeMotionMove(); |
| 3529 | secondWindow->consumeMotionDown(); |
| 3530 | |
| 3531 | // Transfer touch focus to the second window |
| 3532 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 3533 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 3534 | firstWindow->consumeMotionCancel(); |
| 3535 | secondWindow->consumeMotionPointerDown(1); |
| 3536 | |
| 3537 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3538 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3539 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3540 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3541 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3542 | // The first window gets nothing and the second gets pointer up |
| 3543 | firstWindow->assertNoEvents(); |
| 3544 | secondWindow->consumeMotionPointerUp(1); |
| 3545 | |
| 3546 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3547 | NotifyMotionArgs upMotionArgs = |
| 3548 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3549 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3550 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3551 | // The first window gets nothing and the second gets up |
| 3552 | firstWindow->assertNoEvents(); |
| 3553 | secondWindow->consumeMotionUp(); |
| 3554 | } |
| 3555 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3556 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 3557 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 3558 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 3559 | // window should get nothing. |
| 3560 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 3561 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3562 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3563 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3564 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3565 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3566 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3567 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3568 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3569 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3570 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3571 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3572 | |
| 3573 | // Add the windows to the dispatcher |
| 3574 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3575 | |
| 3576 | PointF pointInFirst = {300, 200}; |
| 3577 | PointF pointInSecond = {300, 600}; |
| 3578 | |
| 3579 | // Send down to the first window |
| 3580 | NotifyMotionArgs firstDownMotionArgs = |
| 3581 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3582 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3583 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3584 | // Only the first window should get the down event |
| 3585 | firstWindow->consumeMotionDown(); |
| 3586 | secondWindow->assertNoEvents(); |
| 3587 | |
| 3588 | // Send down to the second window |
| 3589 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3590 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3591 | {pointInFirst, pointInSecond}); |
| 3592 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3593 | // The first window gets a move and the second a down |
| 3594 | firstWindow->consumeMotionMove(); |
| 3595 | secondWindow->consumeMotionDown(); |
| 3596 | |
| 3597 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3598 | const bool transferred = |
| 3599 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3600 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 3601 | ASSERT_FALSE(transferred); |
| 3602 | firstWindow->assertNoEvents(); |
| 3603 | secondWindow->assertNoEvents(); |
| 3604 | |
| 3605 | // The rest of the dispatch should proceed as normal |
| 3606 | // Send pointer up to the second window |
| 3607 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3608 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3609 | {pointInFirst, pointInSecond}); |
| 3610 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3611 | // The first window gets MOVE and the second gets pointer up |
| 3612 | firstWindow->consumeMotionMove(); |
| 3613 | secondWindow->consumeMotionUp(); |
| 3614 | |
| 3615 | // Send up event to the first window |
| 3616 | NotifyMotionArgs upMotionArgs = |
| 3617 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3618 | ADISPLAY_ID_DEFAULT); |
| 3619 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3620 | // The first window gets nothing and the second gets up |
| 3621 | firstWindow->consumeMotionUp(); |
| 3622 | secondWindow->assertNoEvents(); |
| 3623 | } |
| 3624 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3625 | // This case will create two windows and one mirrored window on the default display and mirror |
| 3626 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 3627 | // the windows info of second display before default display. |
| 3628 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 3629 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3630 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3631 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3632 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3633 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3634 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3635 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3636 | |
| 3637 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3638 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3639 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3640 | |
| 3641 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3642 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3643 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3644 | |
| 3645 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3646 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3647 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3648 | |
| 3649 | // Update window info, let it find window handle of second display first. |
| 3650 | mDispatcher->setInputWindows( |
| 3651 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3652 | {ADISPLAY_ID_DEFAULT, |
| 3653 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3654 | |
| 3655 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3656 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3657 | {50, 50})) |
| 3658 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3659 | |
| 3660 | // Window should receive motion event. |
| 3661 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3662 | |
| 3663 | // Transfer touch focus |
| 3664 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 3665 | secondWindowInPrimary->getToken())); |
| 3666 | // The first window gets cancel. |
| 3667 | firstWindowInPrimary->consumeMotionCancel(); |
| 3668 | secondWindowInPrimary->consumeMotionDown(); |
| 3669 | |
| 3670 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3671 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3672 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 3673 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3674 | firstWindowInPrimary->assertNoEvents(); |
| 3675 | secondWindowInPrimary->consumeMotionMove(); |
| 3676 | |
| 3677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3678 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3679 | {150, 50})) |
| 3680 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3681 | firstWindowInPrimary->assertNoEvents(); |
| 3682 | secondWindowInPrimary->consumeMotionUp(); |
| 3683 | } |
| 3684 | |
| 3685 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 3686 | // 'transferTouch' api. |
| 3687 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 3688 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3689 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3690 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3691 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3692 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3693 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3694 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3695 | |
| 3696 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3697 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3698 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3699 | |
| 3700 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3701 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3702 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3703 | |
| 3704 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3705 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3706 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3707 | |
| 3708 | // Update window info, let it find window handle of second display first. |
| 3709 | mDispatcher->setInputWindows( |
| 3710 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3711 | {ADISPLAY_ID_DEFAULT, |
| 3712 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3713 | |
| 3714 | // Touch on second display. |
| 3715 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3716 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 3717 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3718 | |
| 3719 | // Window should receive motion event. |
| 3720 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3721 | |
| 3722 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3723 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3724 | |
| 3725 | // The first window gets cancel. |
| 3726 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3727 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3728 | |
| 3729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3730 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3731 | SECOND_DISPLAY_ID, {150, 50})) |
| 3732 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3733 | firstWindowInPrimary->assertNoEvents(); |
| 3734 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 3735 | |
| 3736 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3737 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 3738 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3739 | firstWindowInPrimary->assertNoEvents(); |
| 3740 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 3741 | } |
| 3742 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3743 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3744 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3745 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3746 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3747 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3748 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3749 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3750 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3751 | |
| 3752 | window->consumeFocusEvent(true); |
| 3753 | |
| 3754 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3755 | mDispatcher->notifyKey(&keyArgs); |
| 3756 | |
| 3757 | // Window should receive key down event. |
| 3758 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3759 | } |
| 3760 | |
| 3761 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3762 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3763 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3764 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3765 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3766 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3767 | |
| 3768 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3769 | mDispatcher->notifyKey(&keyArgs); |
| 3770 | mDispatcher->waitForIdle(); |
| 3771 | |
| 3772 | window->assertNoEvents(); |
| 3773 | } |
| 3774 | |
| 3775 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 3776 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
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 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3780 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3781 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3782 | |
| 3783 | // Send key |
| 3784 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3785 | mDispatcher->notifyKey(&keyArgs); |
| 3786 | // Send motion |
| 3787 | NotifyMotionArgs motionArgs = |
| 3788 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3789 | ADISPLAY_ID_DEFAULT); |
| 3790 | mDispatcher->notifyMotion(&motionArgs); |
| 3791 | |
| 3792 | // Window should receive only the motion event |
| 3793 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3794 | window->assertNoEvents(); // Key event or focus event will not be received |
| 3795 | } |
| 3796 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3797 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 3798 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3799 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3800 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3801 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3802 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3803 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3804 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3805 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3806 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3807 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3808 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3809 | |
| 3810 | // Add the windows to the dispatcher |
| 3811 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3812 | |
| 3813 | PointF pointInFirst = {300, 200}; |
| 3814 | PointF pointInSecond = {300, 600}; |
| 3815 | |
| 3816 | // Send down to the first window |
| 3817 | NotifyMotionArgs firstDownMotionArgs = |
| 3818 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3819 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3820 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3821 | // Only the first window should get the down event |
| 3822 | firstWindow->consumeMotionDown(); |
| 3823 | secondWindow->assertNoEvents(); |
| 3824 | |
| 3825 | // Send down to the second window |
| 3826 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3827 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3828 | {pointInFirst, pointInSecond}); |
| 3829 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3830 | // The first window gets a move and the second a down |
| 3831 | firstWindow->consumeMotionMove(); |
| 3832 | secondWindow->consumeMotionDown(); |
| 3833 | |
| 3834 | // Send pointer cancel to the second window |
| 3835 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3836 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3837 | {pointInFirst, pointInSecond}); |
| 3838 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3839 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3840 | // The first window gets move and the second gets cancel. |
| 3841 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3842 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3843 | |
| 3844 | // Send up event. |
| 3845 | NotifyMotionArgs upMotionArgs = |
| 3846 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3847 | ADISPLAY_ID_DEFAULT); |
| 3848 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3849 | // The first window gets up and the second gets nothing. |
| 3850 | firstWindow->consumeMotionUp(); |
| 3851 | secondWindow->assertNoEvents(); |
| 3852 | } |
| 3853 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3854 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 3855 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3856 | |
| 3857 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3858 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3859 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3860 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 3861 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 3862 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 3863 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3864 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3865 | window->assertNoEvents(); |
| 3866 | mDispatcher->waitForIdle(); |
| 3867 | } |
| 3868 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3869 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3870 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3871 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3872 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3873 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3874 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3875 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3878 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 3879 | |
| 3880 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3881 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 3882 | expectedDisplayId, expectedFlags); |
| 3883 | } |
| 3884 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3885 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 3886 | |
| 3887 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 3888 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3889 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3890 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 3891 | expectedDisplayId, expectedFlags); |
| 3892 | } |
| 3893 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3894 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3895 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 3896 | expectedDisplayId, expectedFlags); |
| 3897 | } |
| 3898 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3899 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3900 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 3901 | expectedDisplayId, expectedFlags); |
| 3902 | } |
| 3903 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3904 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3905 | mInputReceiver->consumeMotionEvent( |
| 3906 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3907 | WithDisplayId(expectedDisplayId), |
| 3908 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3911 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 3912 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 3913 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3914 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3915 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3918 | MotionEvent* consumeMotion() { |
| 3919 | InputEvent* event = mInputReceiver->consume(); |
| 3920 | if (!event) { |
| 3921 | ADD_FAILURE() << "No event was produced"; |
| 3922 | return nullptr; |
| 3923 | } |
| 3924 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 3925 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 3926 | return nullptr; |
| 3927 | } |
| 3928 | return static_cast<MotionEvent*>(event); |
| 3929 | } |
| 3930 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3931 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 3932 | |
| 3933 | private: |
| 3934 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3935 | }; |
| 3936 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3937 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 3938 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3939 | /** |
| 3940 | * Two entities that receive touch: A window, and a global monitor. |
| 3941 | * The touch goes to the window, and then the window disappears. |
| 3942 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 3943 | * for the monitor, as well. |
| 3944 | * 1. foregroundWindow |
| 3945 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 3946 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3947 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3948 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3949 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3950 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3951 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3952 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3953 | |
| 3954 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3955 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3956 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3957 | {100, 200})) |
| 3958 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3959 | |
| 3960 | // Both the foreground window and the global monitor should receive the touch down |
| 3961 | window->consumeMotionDown(); |
| 3962 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3963 | |
| 3964 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3965 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3966 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 3967 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3968 | |
| 3969 | window->consumeMotionMove(); |
| 3970 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3971 | |
| 3972 | // Now the foreground window goes away |
| 3973 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3974 | window->consumeMotionCancel(); |
| 3975 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 3976 | |
| 3977 | // If more events come in, there will be no more foreground window to send them to. This will |
| 3978 | // cause a cancel for the monitor, as well. |
| 3979 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3980 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3981 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 3982 | << "Injection should fail because the window was removed"; |
| 3983 | window->assertNoEvents(); |
| 3984 | // Global monitor now gets the cancel |
| 3985 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3986 | } |
| 3987 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3988 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3989 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3990 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3991 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3992 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3993 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3994 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3995 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3996 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3997 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3998 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3999 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4000 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4003 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 4004 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4005 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4006 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4007 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4008 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4009 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4010 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4011 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4012 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4013 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4014 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4015 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4016 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4017 | // Pilfer pointers from the monitor. |
| 4018 | // This should not do anything and the window should continue to receive events. |
| 4019 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4020 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4021 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4022 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4023 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4024 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4025 | |
| 4026 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4027 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4030 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4031 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4032 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4033 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4034 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4035 | window->setWindowOffset(20, 40); |
| 4036 | window->setWindowTransform(0, 1, -1, 0); |
| 4037 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4038 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4039 | |
| 4040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4041 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4042 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4043 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4044 | MotionEvent* event = monitor.consumeMotion(); |
| 4045 | // Even though window has transform, gesture monitor must not. |
| 4046 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 4047 | } |
| 4048 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4049 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4050 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4051 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4052 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4053 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4054 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4055 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 4056 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4059 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4060 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4061 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4062 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4063 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4064 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4065 | |
| 4066 | NotifyMotionArgs motionArgs = |
| 4067 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4068 | ADISPLAY_ID_DEFAULT); |
| 4069 | |
| 4070 | mDispatcher->notifyMotion(&motionArgs); |
| 4071 | // Window should receive motion down event. |
| 4072 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4073 | |
| 4074 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4075 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4076 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4077 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4078 | motionArgs.pointerCoords[0].getX() - 10); |
| 4079 | |
| 4080 | mDispatcher->notifyMotion(&motionArgs); |
| 4081 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4082 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4083 | } |
| 4084 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4085 | /** |
| 4086 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 4087 | * the device default right away. In the test scenario, we check both the default value, |
| 4088 | * and the action of enabling / disabling. |
| 4089 | */ |
| 4090 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4091 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4092 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4093 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4094 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4095 | |
| 4096 | // Set focused application. |
| 4097 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4098 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4099 | |
| 4100 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4101 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4102 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4103 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4104 | |
| 4105 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4106 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4107 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4108 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4109 | |
| 4110 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4111 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4112 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4113 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4114 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4115 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4116 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4117 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4118 | |
| 4119 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4120 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4121 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4122 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4123 | |
| 4124 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4125 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4126 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4127 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4128 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4129 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4130 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4131 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4132 | |
| 4133 | window->assertNoEvents(); |
| 4134 | } |
| 4135 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4136 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4137 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4138 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4139 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4140 | |
| 4141 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4142 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4143 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4144 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4145 | setFocusedWindow(window); |
| 4146 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4147 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4148 | |
| 4149 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4150 | mDispatcher->notifyKey(&keyArgs); |
| 4151 | |
| 4152 | InputEvent* event = window->consume(); |
| 4153 | ASSERT_NE(event, nullptr); |
| 4154 | |
| 4155 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4156 | ASSERT_NE(verified, nullptr); |
| 4157 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 4158 | |
| 4159 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 4160 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 4161 | ASSERT_EQ(keyArgs.source, verified->source); |
| 4162 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 4163 | |
| 4164 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 4165 | |
| 4166 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4167 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4168 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4169 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 4170 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 4171 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 4172 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 4173 | } |
| 4174 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4175 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4176 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4177 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4178 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4179 | |
| 4180 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4181 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4182 | ui::Transform transform; |
| 4183 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4184 | |
| 4185 | gui::DisplayInfo displayInfo; |
| 4186 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 4187 | displayInfo.transform = transform; |
| 4188 | |
| 4189 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4190 | |
| 4191 | NotifyMotionArgs motionArgs = |
| 4192 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4193 | ADISPLAY_ID_DEFAULT); |
| 4194 | mDispatcher->notifyMotion(&motionArgs); |
| 4195 | |
| 4196 | InputEvent* event = window->consume(); |
| 4197 | ASSERT_NE(event, nullptr); |
| 4198 | |
| 4199 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4200 | ASSERT_NE(verified, nullptr); |
| 4201 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 4202 | |
| 4203 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 4204 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 4205 | EXPECT_EQ(motionArgs.source, verified->source); |
| 4206 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 4207 | |
| 4208 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 4209 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4210 | const vec2 rawXY = |
| 4211 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 4212 | motionArgs.pointerCoords[0].getXYValue()); |
| 4213 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 4214 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4215 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4216 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4217 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4218 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 4219 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 4220 | } |
| 4221 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4222 | /** |
| 4223 | * Ensure that separate calls to sign the same data are generating the same key. |
| 4224 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 4225 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 4226 | * tests. |
| 4227 | */ |
| 4228 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 4229 | KeyEvent event = getTestKeyEvent(); |
| 4230 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4231 | |
| 4232 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 4233 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 4234 | ASSERT_EQ(hmac1, hmac2); |
| 4235 | } |
| 4236 | |
| 4237 | /** |
| 4238 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 4239 | */ |
| 4240 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 4241 | KeyEvent event = getTestKeyEvent(); |
| 4242 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4243 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 4244 | |
| 4245 | verifiedEvent.deviceId += 1; |
| 4246 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4247 | |
| 4248 | verifiedEvent.source += 1; |
| 4249 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4250 | |
| 4251 | verifiedEvent.eventTimeNanos += 1; |
| 4252 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4253 | |
| 4254 | verifiedEvent.displayId += 1; |
| 4255 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4256 | |
| 4257 | verifiedEvent.action += 1; |
| 4258 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4259 | |
| 4260 | verifiedEvent.downTimeNanos += 1; |
| 4261 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4262 | |
| 4263 | verifiedEvent.flags += 1; |
| 4264 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4265 | |
| 4266 | verifiedEvent.keyCode += 1; |
| 4267 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4268 | |
| 4269 | verifiedEvent.scanCode += 1; |
| 4270 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4271 | |
| 4272 | verifiedEvent.metaState += 1; |
| 4273 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4274 | |
| 4275 | verifiedEvent.repeatCount += 1; |
| 4276 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4277 | } |
| 4278 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4279 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 4280 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4281 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4282 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4283 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4284 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4285 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4286 | |
| 4287 | // Top window is also focusable but is not granted focus. |
| 4288 | windowTop->setFocusable(true); |
| 4289 | windowSecond->setFocusable(true); |
| 4290 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4291 | setFocusedWindow(windowSecond); |
| 4292 | |
| 4293 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4295 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4296 | |
| 4297 | // Focused window should receive event. |
| 4298 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4299 | windowTop->assertNoEvents(); |
| 4300 | } |
| 4301 | |
| 4302 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 4303 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4304 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4305 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4306 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4307 | |
| 4308 | window->setFocusable(true); |
| 4309 | // Release channel for window is no longer valid. |
| 4310 | window->releaseChannel(); |
| 4311 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4312 | setFocusedWindow(window); |
| 4313 | |
| 4314 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4315 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4316 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4317 | |
| 4318 | // window channel is invalid, so it should not receive any input event. |
| 4319 | window->assertNoEvents(); |
| 4320 | } |
| 4321 | |
| 4322 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 4323 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4324 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4325 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4326 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4327 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4328 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4329 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4330 | setFocusedWindow(window); |
| 4331 | |
| 4332 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4333 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4334 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4335 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4336 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4337 | window->assertNoEvents(); |
| 4338 | } |
| 4339 | |
| 4340 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 4341 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4342 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4343 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4344 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4345 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4346 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4347 | |
| 4348 | windowTop->setFocusable(true); |
| 4349 | windowSecond->setFocusable(true); |
| 4350 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4351 | setFocusedWindow(windowTop); |
| 4352 | windowTop->consumeFocusEvent(true); |
| 4353 | |
| 4354 | setFocusedWindow(windowSecond, windowTop); |
| 4355 | windowSecond->consumeFocusEvent(true); |
| 4356 | windowTop->consumeFocusEvent(false); |
| 4357 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4358 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4359 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4360 | |
| 4361 | // Focused window should receive event. |
| 4362 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4363 | } |
| 4364 | |
| 4365 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 4366 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4367 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4368 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4369 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4370 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4371 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4372 | |
| 4373 | windowTop->setFocusable(true); |
| 4374 | windowSecond->setFocusable(true); |
| 4375 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4376 | setFocusedWindow(windowSecond, windowTop); |
| 4377 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4378 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4379 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4380 | |
| 4381 | // Event should be dropped. |
| 4382 | windowTop->assertNoEvents(); |
| 4383 | windowSecond->assertNoEvents(); |
| 4384 | } |
| 4385 | |
| 4386 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 4387 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4388 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4389 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4390 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4391 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 4392 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4393 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4394 | |
| 4395 | window->setFocusable(true); |
| 4396 | previousFocusedWindow->setFocusable(true); |
| 4397 | window->setVisible(false); |
| 4398 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 4399 | setFocusedWindow(previousFocusedWindow); |
| 4400 | previousFocusedWindow->consumeFocusEvent(true); |
| 4401 | |
| 4402 | // Requesting focus on invisible window takes focus from currently focused window. |
| 4403 | setFocusedWindow(window); |
| 4404 | previousFocusedWindow->consumeFocusEvent(false); |
| 4405 | |
| 4406 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4407 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4408 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 4409 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4410 | |
| 4411 | // Window does not get focus event or key down. |
| 4412 | window->assertNoEvents(); |
| 4413 | |
| 4414 | // Window becomes visible. |
| 4415 | window->setVisible(true); |
| 4416 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4417 | |
| 4418 | // Window receives focus event. |
| 4419 | window->consumeFocusEvent(true); |
| 4420 | // Focused window receives key down. |
| 4421 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4422 | } |
| 4423 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4424 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 4425 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4426 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4427 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4428 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4429 | |
| 4430 | // window is granted focus. |
| 4431 | window->setFocusable(true); |
| 4432 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4433 | setFocusedWindow(window); |
| 4434 | window->consumeFocusEvent(true); |
| 4435 | |
| 4436 | // When a display is removed window loses focus. |
| 4437 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 4438 | window->consumeFocusEvent(false); |
| 4439 | } |
| 4440 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4441 | /** |
| 4442 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 4443 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 4444 | * of the 'slipperyEnterWindow'. |
| 4445 | * |
| 4446 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 4447 | * a way so that the touched location is no longer covered by the top window. |
| 4448 | * |
| 4449 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 4450 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 4451 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 4452 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 4453 | * with ACTION_DOWN). |
| 4454 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 4455 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 4456 | * |
| 4457 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 4458 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 4459 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 4460 | * |
| 4461 | * In this test, we ensure that the event received by the bottom window has |
| 4462 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 4463 | */ |
| 4464 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4465 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 4466 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4467 | |
| 4468 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4469 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4470 | |
| 4471 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4472 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4473 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4474 | // Make sure this one overlaps the bottom window |
| 4475 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 4476 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 4477 | // one. Windows with the same owner are not considered to be occluding each other. |
| 4478 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 4479 | |
| 4480 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4481 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4482 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4483 | |
| 4484 | mDispatcher->setInputWindows( |
| 4485 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4486 | |
| 4487 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 4488 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4489 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 4490 | mDispatcher->notifyMotion(&args); |
| 4491 | slipperyExitWindow->consumeMotionDown(); |
| 4492 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 4493 | mDispatcher->setInputWindows( |
| 4494 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4495 | |
| 4496 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4497 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 4498 | mDispatcher->notifyMotion(&args); |
| 4499 | |
| 4500 | slipperyExitWindow->consumeMotionCancel(); |
| 4501 | |
| 4502 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 4503 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 4504 | } |
| 4505 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4506 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 4507 | protected: |
| 4508 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 4509 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 4510 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4511 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4512 | sp<FakeWindowHandle> mWindow; |
| 4513 | |
| 4514 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4515 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4516 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4517 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4518 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 4519 | ASSERT_EQ(OK, mDispatcher->start()); |
| 4520 | |
| 4521 | setUpWindow(); |
| 4522 | } |
| 4523 | |
| 4524 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4525 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4526 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4527 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4528 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4529 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4530 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4531 | mWindow->consumeFocusEvent(true); |
| 4532 | } |
| 4533 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4534 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4535 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4536 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4537 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 4538 | mDispatcher->notifyKey(&keyArgs); |
| 4539 | |
| 4540 | // Window should receive key down event. |
| 4541 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4542 | } |
| 4543 | |
| 4544 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 4545 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 4546 | InputEvent* repeatEvent = mWindow->consume(); |
| 4547 | ASSERT_NE(nullptr, repeatEvent); |
| 4548 | |
| 4549 | uint32_t eventType = repeatEvent->getType(); |
| 4550 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 4551 | |
| 4552 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 4553 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 4554 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 4555 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 4556 | } |
| 4557 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4558 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4559 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4560 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4561 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 4562 | mDispatcher->notifyKey(&keyArgs); |
| 4563 | |
| 4564 | // Window should receive key down event. |
| 4565 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4566 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4567 | } |
| 4568 | }; |
| 4569 | |
| 4570 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4571 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4572 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4573 | expectKeyRepeatOnce(repeatCount); |
| 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4578 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4579 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4580 | expectKeyRepeatOnce(repeatCount); |
| 4581 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4582 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4583 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4584 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4585 | expectKeyRepeatOnce(repeatCount); |
| 4586 | } |
| 4587 | } |
| 4588 | |
| 4589 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4590 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 4591 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 4592 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4593 | mWindow->assertNoEvents(); |
| 4594 | } |
| 4595 | |
| 4596 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4597 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 4598 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 4599 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 4600 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4601 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4602 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4603 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4604 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 4605 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4606 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4607 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4608 | mWindow->assertNoEvents(); |
| 4609 | } |
| 4610 | |
| 4611 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4612 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 4613 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 4614 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 4615 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4616 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4617 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4618 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4619 | mWindow->assertNoEvents(); |
| 4620 | } |
| 4621 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4622 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 4623 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4624 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 4625 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4626 | mDispatcher->notifyDeviceReset(&args); |
| 4627 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 4628 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 4629 | mWindow->assertNoEvents(); |
| 4630 | } |
| 4631 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4632 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4633 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4634 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4635 | InputEvent* repeatEvent = mWindow->consume(); |
| 4636 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4637 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 4638 | IdGenerator::getSource(repeatEvent->getId())); |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4643 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4644 | |
| 4645 | std::unordered_set<int32_t> idSet; |
| 4646 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4647 | InputEvent* repeatEvent = mWindow->consume(); |
| 4648 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4649 | int32_t id = repeatEvent->getId(); |
| 4650 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 4651 | idSet.insert(id); |
| 4652 | } |
| 4653 | } |
| 4654 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4655 | /* Test InputDispatcher for MultiDisplay */ |
| 4656 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 4657 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4658 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4659 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4660 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4661 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4662 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4663 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4664 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4665 | // Set focus window for primary display, but focused display would be second one. |
| 4666 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4667 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4668 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4669 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4670 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4671 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4672 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4673 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4674 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4675 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4676 | // Set focus display to second one. |
| 4677 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 4678 | // Set focus window for second display. |
| 4679 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4680 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4681 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4682 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4683 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4684 | } |
| 4685 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4686 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4687 | InputDispatcherTest::TearDown(); |
| 4688 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4689 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4690 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4691 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4692 | windowInSecondary.clear(); |
| 4693 | } |
| 4694 | |
| 4695 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4696 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4697 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4698 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4699 | sp<FakeWindowHandle> windowInSecondary; |
| 4700 | }; |
| 4701 | |
| 4702 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 4703 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4705 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4706 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4707 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4708 | windowInSecondary->assertNoEvents(); |
| 4709 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4710 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4711 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4712 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4713 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4714 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4715 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4716 | } |
| 4717 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4718 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4719 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4720 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4721 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4722 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4723 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4724 | windowInSecondary->assertNoEvents(); |
| 4725 | |
| 4726 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4728 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4729 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4730 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4731 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4732 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4733 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4734 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4735 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4736 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 4737 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4738 | |
| 4739 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4740 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4741 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4742 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4743 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4744 | windowInSecondary->assertNoEvents(); |
| 4745 | } |
| 4746 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4747 | // Test per-display input monitors for motion event. |
| 4748 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4749 | FakeMonitorReceiver monitorInPrimary = |
| 4750 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4751 | FakeMonitorReceiver monitorInSecondary = |
| 4752 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4753 | |
| 4754 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4755 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4756 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4757 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4758 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4759 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4760 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4761 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4762 | |
| 4763 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4764 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4765 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4767 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4768 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4769 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4770 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4771 | |
| 4772 | // Test inject a non-pointer motion event. |
| 4773 | // If specific a display, it will dispatch to the focused window of particular display, |
| 4774 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4775 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4776 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 4777 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4778 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4779 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4780 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4781 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | // Test per-display input monitors for key event. |
| 4785 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4786 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4787 | FakeMonitorReceiver monitorInPrimary = |
| 4788 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4789 | FakeMonitorReceiver monitorInSecondary = |
| 4790 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4791 | |
| 4792 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4794 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4795 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4796 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4797 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4798 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4799 | } |
| 4800 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4801 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 4802 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4803 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4804 | secondWindowInPrimary->setFocusable(true); |
| 4805 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 4806 | setFocusedWindow(secondWindowInPrimary); |
| 4807 | windowInPrimary->consumeFocusEvent(false); |
| 4808 | secondWindowInPrimary->consumeFocusEvent(true); |
| 4809 | |
| 4810 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4811 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 4812 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4813 | windowInPrimary->assertNoEvents(); |
| 4814 | windowInSecondary->assertNoEvents(); |
| 4815 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4816 | } |
| 4817 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 4818 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 4819 | FakeMonitorReceiver monitorInPrimary = |
| 4820 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4821 | FakeMonitorReceiver monitorInSecondary = |
| 4822 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 4823 | |
| 4824 | // Test touch down on primary display. |
| 4825 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4826 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4827 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4828 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4829 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4830 | |
| 4831 | // Test touch down on second display. |
| 4832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4833 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4834 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4835 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4836 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 4837 | |
| 4838 | // Trigger cancel touch. |
| 4839 | mDispatcher->cancelCurrentTouch(); |
| 4840 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4841 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4842 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4843 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4844 | |
| 4845 | // Test inject a move motion event, no window/monitor should receive the event. |
| 4846 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4847 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4848 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4849 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4850 | windowInPrimary->assertNoEvents(); |
| 4851 | monitorInPrimary.assertNoEvents(); |
| 4852 | |
| 4853 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4854 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4855 | SECOND_DISPLAY_ID, {110, 200})) |
| 4856 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4857 | windowInSecondary->assertNoEvents(); |
| 4858 | monitorInSecondary.assertNoEvents(); |
| 4859 | } |
| 4860 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4861 | class InputFilterTest : public InputDispatcherTest { |
| 4862 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4863 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 4864 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4865 | NotifyMotionArgs motionArgs; |
| 4866 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4867 | motionArgs = |
| 4868 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4869 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4870 | motionArgs = |
| 4871 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4872 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4873 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4874 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4875 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 4876 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4877 | } else { |
| 4878 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | void testNotifyKey(bool expectToBeFiltered) { |
| 4883 | NotifyKeyArgs keyArgs; |
| 4884 | |
| 4885 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4886 | mDispatcher->notifyKey(&keyArgs); |
| 4887 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 4888 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4889 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4890 | |
| 4891 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 4892 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4893 | } else { |
| 4894 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4895 | } |
| 4896 | } |
| 4897 | }; |
| 4898 | |
| 4899 | // Test InputFilter for MotionEvent |
| 4900 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 4901 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 4902 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4903 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4904 | |
| 4905 | // Enable InputFilter |
| 4906 | mDispatcher->setInputFilterEnabled(true); |
| 4907 | // Test touch on both primary and second display, and check if both events are filtered. |
| 4908 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 4909 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 4910 | |
| 4911 | // Disable InputFilter |
| 4912 | mDispatcher->setInputFilterEnabled(false); |
| 4913 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 4914 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4915 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4916 | } |
| 4917 | |
| 4918 | // Test InputFilter for KeyEvent |
| 4919 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 4920 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 4921 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4922 | |
| 4923 | // Enable InputFilter |
| 4924 | mDispatcher->setInputFilterEnabled(true); |
| 4925 | // Send a key event, and check if it is filtered. |
| 4926 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 4927 | |
| 4928 | // Disable InputFilter |
| 4929 | mDispatcher->setInputFilterEnabled(false); |
| 4930 | // Send a key event, and check if it isn't filtered. |
| 4931 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4932 | } |
| 4933 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4934 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 4935 | // logical display coordinate space. |
| 4936 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 4937 | ui::Transform firstDisplayTransform; |
| 4938 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4939 | ui::Transform secondDisplayTransform; |
| 4940 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 4941 | |
| 4942 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 4943 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 4944 | displayInfos[0].transform = firstDisplayTransform; |
| 4945 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 4946 | displayInfos[1].transform = secondDisplayTransform; |
| 4947 | |
| 4948 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 4949 | |
| 4950 | // Enable InputFilter |
| 4951 | mDispatcher->setInputFilterEnabled(true); |
| 4952 | |
| 4953 | // Ensure the correct transforms are used for the displays. |
| 4954 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 4955 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 4956 | } |
| 4957 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4958 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 4959 | protected: |
| 4960 | virtual void SetUp() override { |
| 4961 | InputDispatcherTest::SetUp(); |
| 4962 | |
| 4963 | /** |
| 4964 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 4965 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 4966 | * on. |
| 4967 | */ |
| 4968 | mDispatcher->setInputFilterEnabled(true); |
| 4969 | |
| 4970 | std::shared_ptr<InputApplicationHandle> application = |
| 4971 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4972 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 4973 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4974 | |
| 4975 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4976 | mWindow->setFocusable(true); |
| 4977 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4978 | setFocusedWindow(mWindow); |
| 4979 | mWindow->consumeFocusEvent(true); |
| 4980 | } |
| 4981 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4982 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4983 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4984 | KeyEvent event; |
| 4985 | |
| 4986 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4987 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 4988 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4989 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4990 | const int32_t additionalPolicyFlags = |
| 4991 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 4992 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4993 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4994 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4995 | policyFlags | additionalPolicyFlags)); |
| 4996 | |
| 4997 | InputEvent* received = mWindow->consume(); |
| 4998 | ASSERT_NE(nullptr, received); |
| 4999 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5000 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 5001 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 5002 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 5003 | } |
| 5004 | |
| 5005 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5006 | int32_t flags) { |
| 5007 | MotionEvent event; |
| 5008 | PointerProperties pointerProperties[1]; |
| 5009 | PointerCoords pointerCoords[1]; |
| 5010 | pointerProperties[0].clear(); |
| 5011 | pointerProperties[0].id = 0; |
| 5012 | pointerCoords[0].clear(); |
| 5013 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 5014 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 5015 | |
| 5016 | ui::Transform identityTransform; |
| 5017 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5018 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 5019 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 5020 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 5021 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 5022 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 5023 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5024 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 5025 | |
| 5026 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 5027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5028 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5029 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5030 | policyFlags | additionalPolicyFlags)); |
| 5031 | |
| 5032 | InputEvent* received = mWindow->consume(); |
| 5033 | ASSERT_NE(nullptr, received); |
| 5034 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 5035 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 5036 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 5037 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | private: |
| 5041 | sp<FakeWindowHandle> mWindow; |
| 5042 | }; |
| 5043 | |
| 5044 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5045 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 5046 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 5047 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5048 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 5049 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5052 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5053 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5054 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5055 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 5056 | } |
| 5057 | |
| 5058 | TEST_F(InputFilterInjectionPolicyTest, |
| 5059 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 5060 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5061 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5062 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5066 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 5067 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5070 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5071 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5072 | InputDispatcherTest::SetUp(); |
| 5073 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5074 | std::shared_ptr<FakeApplicationHandle> application = |
| 5075 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5076 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5077 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5078 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5079 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5080 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5081 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5082 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5083 | |
| 5084 | // Set focused application. |
| 5085 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5086 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5087 | |
| 5088 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5090 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5091 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5092 | } |
| 5093 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5094 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5095 | InputDispatcherTest::TearDown(); |
| 5096 | |
| 5097 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5098 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | protected: |
| 5102 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5103 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5104 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5105 | }; |
| 5106 | |
| 5107 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5108 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 5109 | // the onPointerDownOutsideFocus callback. |
| 5110 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5111 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5112 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5113 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5114 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5115 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5116 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5117 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5118 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 5119 | } |
| 5120 | |
| 5121 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 5122 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 5123 | // onPointerDownOutsideFocus callback. |
| 5124 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5125 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5126 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5127 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5128 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5129 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5130 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5131 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 5135 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 5136 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5137 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5138 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5139 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5140 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5141 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5142 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5143 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5147 | // DOWN on the window that already has focus. Ensure no window received the |
| 5148 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5149 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5150 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5151 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5152 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5153 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5154 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5155 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5156 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5157 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5158 | } |
| 5159 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 5160 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 5161 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 5162 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 5163 | const MotionEvent event = |
| 5164 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5165 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5166 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 5167 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 5168 | .build(); |
| 5169 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 5170 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5171 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 5172 | |
| 5173 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5174 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 5175 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 5176 | mUnfocusedWindow->assertNoEvents(); |
| 5177 | } |
| 5178 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5179 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 5180 | // windows that point to the same client token. |
| 5181 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 5182 | virtual void SetUp() override { |
| 5183 | InputDispatcherTest::SetUp(); |
| 5184 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5185 | std::shared_ptr<FakeApplicationHandle> application = |
| 5186 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5187 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 5188 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5189 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 5190 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5191 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 5192 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5193 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 5194 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5195 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5196 | } |
| 5197 | |
| 5198 | protected: |
| 5199 | sp<FakeWindowHandle> mWindow1; |
| 5200 | sp<FakeWindowHandle> mWindow2; |
| 5201 | |
| 5202 | // 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] | 5203 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5204 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 5205 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 5209 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5210 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5211 | InputEvent* event = window->consume(); |
| 5212 | |
| 5213 | ASSERT_NE(nullptr, event) << name.c_str() |
| 5214 | << ": consumer should have returned non-NULL event."; |
| 5215 | |
| 5216 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 5217 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 5218 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 5219 | |
| 5220 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5221 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 5222 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5223 | |
| 5224 | for (size_t i = 0; i < points.size(); i++) { |
| 5225 | float expectedX = points[i].x; |
| 5226 | float expectedY = points[i].y; |
| 5227 | |
| 5228 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 5229 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 5230 | << ", got " << motionEvent.getX(i); |
| 5231 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 5232 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 5233 | << ", got " << motionEvent.getY(i); |
| 5234 | } |
| 5235 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5236 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5237 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5238 | std::vector<PointF> expectedPoints) { |
| 5239 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 5240 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 5241 | mDispatcher->notifyMotion(&motionArgs); |
| 5242 | |
| 5243 | // Always consume from window1 since it's the window that has the InputReceiver |
| 5244 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 5245 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5246 | }; |
| 5247 | |
| 5248 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 5249 | // Touch Window 1 |
| 5250 | PointF touchedPoint = {10, 10}; |
| 5251 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5252 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5253 | |
| 5254 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5255 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5256 | |
| 5257 | // Touch Window 2 |
| 5258 | touchedPoint = {150, 150}; |
| 5259 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5260 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5261 | } |
| 5262 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5263 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 5264 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5265 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5266 | |
| 5267 | // Touch Window 1 |
| 5268 | PointF touchedPoint = {10, 10}; |
| 5269 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5270 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5271 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5272 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5273 | |
| 5274 | // Touch Window 2 |
| 5275 | touchedPoint = {150, 150}; |
| 5276 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5277 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 5278 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5279 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5280 | // Update the transform so rotation is set |
| 5281 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5282 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 5283 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5284 | } |
| 5285 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5286 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5287 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5288 | |
| 5289 | // Touch Window 1 |
| 5290 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5291 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5292 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5293 | |
| 5294 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5295 | touchedPoints.push_back(PointF{150, 150}); |
| 5296 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5297 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5298 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5299 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5300 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5301 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5302 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5303 | // Update the transform so rotation is set for Window 2 |
| 5304 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5305 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5306 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5309 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5310 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5311 | |
| 5312 | // Touch Window 1 |
| 5313 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5314 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5315 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5316 | |
| 5317 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5318 | touchedPoints.push_back(PointF{150, 150}); |
| 5319 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5320 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5321 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5322 | |
| 5323 | // Move both windows |
| 5324 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5325 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5326 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5327 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5328 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5329 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5330 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5331 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5332 | expectedPoints.pop_back(); |
| 5333 | |
| 5334 | // Touch Window 2 |
| 5335 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5336 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5337 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5338 | |
| 5339 | // Move both windows |
| 5340 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5341 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5342 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5343 | |
| 5344 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
| 5347 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 5348 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 5349 | |
| 5350 | // Touch Window 1 |
| 5351 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5352 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5353 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5354 | |
| 5355 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5356 | touchedPoints.push_back(PointF{150, 150}); |
| 5357 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5358 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5359 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5360 | |
| 5361 | // Move both windows |
| 5362 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5363 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5364 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5365 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5366 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5369 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 5370 | virtual void SetUp() override { |
| 5371 | InputDispatcherTest::SetUp(); |
| 5372 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5373 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5374 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5375 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 5376 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5377 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5378 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5379 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5380 | |
| 5381 | // Set focused application. |
| 5382 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 5383 | |
| 5384 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5385 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5386 | mWindow->consumeFocusEvent(true); |
| 5387 | } |
| 5388 | |
| 5389 | virtual void TearDown() override { |
| 5390 | InputDispatcherTest::TearDown(); |
| 5391 | mWindow.clear(); |
| 5392 | } |
| 5393 | |
| 5394 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5395 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5396 | sp<FakeWindowHandle> mWindow; |
| 5397 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 5398 | |
| 5399 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5400 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5401 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5402 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5403 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5404 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5405 | WINDOW_LOCATION)); |
| 5406 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5407 | |
| 5408 | sp<FakeWindowHandle> addSpyWindow() { |
| 5409 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5410 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5411 | spy->setTrustedOverlay(true); |
| 5412 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5413 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5414 | spy->setDispatchingTimeout(30ms); |
| 5415 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 5416 | return spy; |
| 5417 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5418 | }; |
| 5419 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5420 | // Send a tap and respond, which should not cause an ANR. |
| 5421 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 5422 | tapOnWindow(); |
| 5423 | mWindow->consumeMotionDown(); |
| 5424 | mWindow->consumeMotionUp(); |
| 5425 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5426 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5427 | } |
| 5428 | |
| 5429 | // Send a regular key and respond, which should not cause an ANR. |
| 5430 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5431 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5432 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5433 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5434 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5435 | } |
| 5436 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5437 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 5438 | mWindow->setFocusable(false); |
| 5439 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5440 | mWindow->consumeFocusEvent(false); |
| 5441 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5442 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5443 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5444 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 5445 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5446 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5447 | // Key will not go to window because we have no focused window. |
| 5448 | // The 'no focused window' ANR timer should start instead. |
| 5449 | |
| 5450 | // Now, the focused application goes away. |
| 5451 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 5452 | // The key should get dropped and there should be no ANR. |
| 5453 | |
| 5454 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5455 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5456 | } |
| 5457 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5458 | // 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] | 5459 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 5460 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5461 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5462 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5463 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5464 | WINDOW_LOCATION)); |
| 5465 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5466 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 5467 | ASSERT_TRUE(sequenceNum); |
| 5468 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5469 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5470 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5471 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5472 | mWindow->consumeMotionEvent( |
| 5473 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5474 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5475 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | // Send a key to the app and have the app not respond right away. |
| 5479 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 5480 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5481 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5482 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 5483 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5484 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5485 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5486 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5487 | } |
| 5488 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5489 | // We have a focused application, but no focused window |
| 5490 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5491 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5492 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5493 | mWindow->consumeFocusEvent(false); |
| 5494 | |
| 5495 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5496 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5497 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5498 | WINDOW_LOCATION)); |
| 5499 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 5500 | mDispatcher->waitForIdle(); |
| 5501 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5502 | |
| 5503 | // Once a focused event arrives, we get an ANR for this application |
| 5504 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5505 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5506 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5507 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5508 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5509 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5510 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5511 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5512 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5513 | } |
| 5514 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5515 | /** |
| 5516 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 5517 | * there will not be an ANR. |
| 5518 | */ |
| 5519 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 5520 | mWindow->setFocusable(false); |
| 5521 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5522 | mWindow->consumeFocusEvent(false); |
| 5523 | |
| 5524 | KeyEvent event; |
| 5525 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 5526 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 5527 | |
| 5528 | // Define a valid key down event that is stale (too old). |
| 5529 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 5530 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5531 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5532 | |
| 5533 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 5534 | |
| 5535 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5536 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5537 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 5538 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 5539 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 5540 | << "Injection should fail because the event is stale"; |
| 5541 | |
| 5542 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5543 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5544 | mWindow->assertNoEvents(); |
| 5545 | } |
| 5546 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5547 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5548 | // Make sure that we don't notify policy twice about the same ANR. |
| 5549 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5550 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5551 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5552 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5553 | |
| 5554 | // Once a focused event arrives, we get an ANR for this application |
| 5555 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5556 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5557 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5558 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5559 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5560 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5561 | const std::chrono::duration appTimeout = |
| 5562 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5563 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5564 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5565 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5566 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 5567 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5568 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5569 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 5570 | // '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] | 5571 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5572 | } |
| 5573 | |
| 5574 | // We have a focused application, but no focused window |
| 5575 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5576 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5577 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5578 | mWindow->consumeFocusEvent(false); |
| 5579 | |
| 5580 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5581 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5582 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5583 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5584 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5585 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5586 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5587 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5588 | |
| 5589 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5590 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5591 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5592 | mWindow->assertNoEvents(); |
| 5593 | } |
| 5594 | |
| 5595 | /** |
| 5596 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 5597 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 5598 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 5599 | * the ANR mechanism should still work. |
| 5600 | * |
| 5601 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 5602 | * DOWN event, while not responding on the second one. |
| 5603 | */ |
| 5604 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 5605 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5606 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5607 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5608 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5609 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5610 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5611 | |
| 5612 | // Now send ACTION_UP, with identical timestamp |
| 5613 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5614 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5615 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5616 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5617 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5618 | |
| 5619 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 5620 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5621 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5622 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5623 | } |
| 5624 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5625 | // A spy window can receive an ANR |
| 5626 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 5627 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 5628 | |
| 5629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5630 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5631 | WINDOW_LOCATION)); |
| 5632 | mWindow->consumeMotionDown(); |
| 5633 | |
| 5634 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 5635 | ASSERT_TRUE(sequenceNum); |
| 5636 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5637 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5638 | |
| 5639 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5640 | spy->consumeMotionEvent( |
| 5641 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5642 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5643 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5644 | } |
| 5645 | |
| 5646 | // 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] | 5647 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5648 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 5649 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5650 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5652 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5653 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5654 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5655 | |
| 5656 | // Stuck on the ACTION_UP |
| 5657 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5658 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5659 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5660 | // 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] | 5661 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5662 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5663 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5664 | |
| 5665 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5666 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5667 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5668 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5669 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5670 | } |
| 5671 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5672 | // 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] | 5673 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5674 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 5675 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5676 | |
| 5677 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5678 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5679 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5680 | |
| 5681 | mWindow->consumeMotionDown(); |
| 5682 | // Stuck on the ACTION_UP |
| 5683 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5684 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5685 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5686 | // 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] | 5687 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5688 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5689 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5690 | |
| 5691 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5692 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5693 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5694 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5695 | spy->assertNoEvents(); |
| 5696 | } |
| 5697 | |
| 5698 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 5699 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 5700 | |
| 5701 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5702 | |
| 5703 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5704 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5705 | WINDOW_LOCATION)); |
| 5706 | |
| 5707 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5708 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 5709 | ASSERT_TRUE(consumeSeq); |
| 5710 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5711 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5712 | |
| 5713 | monitor.finishEvent(*consumeSeq); |
| 5714 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5715 | |
| 5716 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5717 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 5721 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 5722 | // get an ANR again. |
| 5723 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 5724 | // 2. consume all pending events (= queue becomes healthy again) |
| 5725 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 5726 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 5727 | tapOnWindow(); |
| 5728 | |
| 5729 | mWindow->consumeMotionDown(); |
| 5730 | // Block on ACTION_UP |
| 5731 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5732 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5733 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 5734 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5735 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5736 | mWindow->assertNoEvents(); |
| 5737 | |
| 5738 | tapOnWindow(); |
| 5739 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5740 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5741 | mWindow->consumeMotionUp(); |
| 5742 | |
| 5743 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5744 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5745 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5746 | mWindow->assertNoEvents(); |
| 5747 | } |
| 5748 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5749 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 5750 | // it. |
| 5751 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5752 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5753 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5754 | WINDOW_LOCATION)); |
| 5755 | |
| 5756 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5757 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5758 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5759 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 5760 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5761 | // 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] | 5762 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5763 | mWindow->consumeMotionEvent( |
| 5764 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5765 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5766 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5767 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5768 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | /** |
| 5772 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5773 | * not to to the focused window until the motion is processed. |
| 5774 | * |
| 5775 | * Warning!!! |
| 5776 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5777 | * and the injection timeout that we specify when injecting the key. |
| 5778 | * We must have the injection timeout (10ms) be smaller than |
| 5779 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5780 | * |
| 5781 | * If that value changes, this test should also change. |
| 5782 | */ |
| 5783 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 5784 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5785 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5786 | |
| 5787 | tapOnWindow(); |
| 5788 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5789 | ASSERT_TRUE(downSequenceNum); |
| 5790 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5791 | ASSERT_TRUE(upSequenceNum); |
| 5792 | // Don't finish the events yet, and send a key |
| 5793 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 5794 | // window even if motions are still being processed. But because the injection timeout is short, |
| 5795 | // we will receive INJECTION_TIMED_OUT as the result. |
| 5796 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5797 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5798 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5799 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5800 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5801 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5802 | // and the key remains pending, waiting for the touch events to be processed |
| 5803 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5804 | ASSERT_FALSE(keySequenceNum); |
| 5805 | |
| 5806 | std::this_thread::sleep_for(500ms); |
| 5807 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 5808 | // to the focused window, even though we have not yet finished the motion event |
| 5809 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5810 | mWindow->finishEvent(*downSequenceNum); |
| 5811 | mWindow->finishEvent(*upSequenceNum); |
| 5812 | } |
| 5813 | |
| 5814 | /** |
| 5815 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5816 | * not go to the focused window until the motion is processed. |
| 5817 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 5818 | * focused window right away. |
| 5819 | */ |
| 5820 | TEST_F(InputDispatcherSingleWindowAnr, |
| 5821 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 5822 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5823 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5824 | |
| 5825 | tapOnWindow(); |
| 5826 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5827 | ASSERT_TRUE(downSequenceNum); |
| 5828 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5829 | ASSERT_TRUE(upSequenceNum); |
| 5830 | // Don't finish the events yet, and send a key |
| 5831 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5833 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5834 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5835 | // At this point, key is still pending, and should not be sent to the application yet. |
| 5836 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5837 | ASSERT_FALSE(keySequenceNum); |
| 5838 | |
| 5839 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 5840 | tapOnWindow(); |
| 5841 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 5842 | // the other events yet. We can finish events in any order. |
| 5843 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 5844 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 5845 | mWindow->consumeMotionDown(); |
| 5846 | mWindow->consumeMotionUp(); |
| 5847 | mWindow->assertNoEvents(); |
| 5848 | } |
| 5849 | |
| 5850 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 5851 | virtual void SetUp() override { |
| 5852 | InputDispatcherTest::SetUp(); |
| 5853 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5854 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5855 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5856 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 5857 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5858 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5859 | // 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] | 5860 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5861 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5862 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 5863 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5864 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5865 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5866 | |
| 5867 | // Set focused application. |
| 5868 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5869 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5870 | |
| 5871 | // Expect one focus window exist in display. |
| 5872 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5873 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5874 | mFocusedWindow->consumeFocusEvent(true); |
| 5875 | } |
| 5876 | |
| 5877 | virtual void TearDown() override { |
| 5878 | InputDispatcherTest::TearDown(); |
| 5879 | |
| 5880 | mUnfocusedWindow.clear(); |
| 5881 | mFocusedWindow.clear(); |
| 5882 | } |
| 5883 | |
| 5884 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5885 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5886 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 5887 | sp<FakeWindowHandle> mFocusedWindow; |
| 5888 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 5889 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 5890 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 5891 | |
| 5892 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 5893 | |
| 5894 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 5895 | |
| 5896 | private: |
| 5897 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5898 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5899 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5900 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5902 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5903 | location)); |
| 5904 | } |
| 5905 | }; |
| 5906 | |
| 5907 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 5908 | // should be ANR'd first. |
| 5909 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5911 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5912 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5913 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5914 | mFocusedWindow->consumeMotionDown(); |
| 5915 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5916 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5917 | // We consumed all events, so no ANR |
| 5918 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5919 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5920 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5921 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5922 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5923 | FOCUSED_WINDOW_LOCATION)); |
| 5924 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5925 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5926 | |
| 5927 | const std::chrono::duration timeout = |
| 5928 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5929 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5930 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 5931 | // sequence to make it consistent |
| 5932 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5933 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5934 | mFocusedWindow->consumeMotionDown(); |
| 5935 | // This cancel is generated because the connection was unresponsive |
| 5936 | mFocusedWindow->consumeMotionCancel(); |
| 5937 | mFocusedWindow->assertNoEvents(); |
| 5938 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5939 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5940 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5941 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5942 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5943 | } |
| 5944 | |
| 5945 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 5946 | // it doesn't matter which order they should have ANR. |
| 5947 | // But we should receive ANR for both. |
| 5948 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 5949 | // Set the timeout for unfocused window to match the focused window |
| 5950 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 5951 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5952 | |
| 5953 | tapOnFocusedWindow(); |
| 5954 | // 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] | 5955 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 5956 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 5957 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5958 | |
| 5959 | // 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] | 5960 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 5961 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 5962 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 5963 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5964 | |
| 5965 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5966 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5967 | |
| 5968 | mFocusedWindow->consumeMotionDown(); |
| 5969 | mFocusedWindow->consumeMotionUp(); |
| 5970 | mUnfocusedWindow->consumeMotionOutside(); |
| 5971 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5972 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 5973 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 5974 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5975 | |
| 5976 | // Both applications should be marked as responsive, in any order |
| 5977 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 5978 | mFocusedWindow->getToken() == responsiveToken2); |
| 5979 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 5980 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 5981 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5982 | } |
| 5983 | |
| 5984 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 5985 | // We should also log an error to account for the dropped event (not tested here). |
| 5986 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 5987 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 5988 | tapOnFocusedWindow(); |
| 5989 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5990 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5991 | // Receive the events, but don't respond |
| 5992 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 5993 | ASSERT_TRUE(downEventSequenceNum); |
| 5994 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 5995 | ASSERT_TRUE(upEventSequenceNum); |
| 5996 | const std::chrono::duration timeout = |
| 5997 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5998 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5999 | |
| 6000 | // Tap once again |
| 6001 | // 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] | 6002 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6003 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6004 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6005 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6006 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6007 | FOCUSED_WINDOW_LOCATION)); |
| 6008 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 6009 | // valid touch target |
| 6010 | mUnfocusedWindow->assertNoEvents(); |
| 6011 | |
| 6012 | // Consume the first tap |
| 6013 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 6014 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 6015 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6016 | // The second tap did not go to the focused window |
| 6017 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6018 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6019 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6020 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6021 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6022 | } |
| 6023 | |
| 6024 | // If you tap outside of all windows, there will not be ANR |
| 6025 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6026 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6027 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6028 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 6029 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6030 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6031 | } |
| 6032 | |
| 6033 | // Since the focused window is paused, tapping on it should not produce any events |
| 6034 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 6035 | mFocusedWindow->setPaused(true); |
| 6036 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6037 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6038 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6039 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6040 | FOCUSED_WINDOW_LOCATION)); |
| 6041 | |
| 6042 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 6043 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6044 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 6045 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6046 | |
| 6047 | mFocusedWindow->assertNoEvents(); |
| 6048 | mUnfocusedWindow->assertNoEvents(); |
| 6049 | } |
| 6050 | |
| 6051 | /** |
| 6052 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6053 | * not to to the focused window until the motion is processed. |
| 6054 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 6055 | * |
| 6056 | * Warning!!! |
| 6057 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6058 | * and the injection timeout that we specify when injecting the key. |
| 6059 | * We must have the injection timeout (10ms) be smaller than |
| 6060 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6061 | * |
| 6062 | * If that value changes, this test should also change. |
| 6063 | */ |
| 6064 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 6065 | // Set a long ANR timeout to prevent it from triggering |
| 6066 | mFocusedWindow->setDispatchingTimeout(2s); |
| 6067 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6068 | |
| 6069 | tapOnUnfocusedWindow(); |
| 6070 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6071 | ASSERT_TRUE(downSequenceNum); |
| 6072 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6073 | ASSERT_TRUE(upSequenceNum); |
| 6074 | // Don't finish the events yet, and send a key |
| 6075 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 6076 | // window even if motions are still being processed. |
| 6077 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6078 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6079 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6080 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6081 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6082 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6083 | // and the key remains pending, waiting for the touch events to be processed |
| 6084 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 6085 | ASSERT_FALSE(keySequenceNum); |
| 6086 | |
| 6087 | // 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] | 6088 | mFocusedWindow->setFocusable(false); |
| 6089 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6090 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6091 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6092 | |
| 6093 | // Focus events should precede the key events |
| 6094 | mUnfocusedWindow->consumeFocusEvent(true); |
| 6095 | mFocusedWindow->consumeFocusEvent(false); |
| 6096 | |
| 6097 | // Finish the tap events, which should unblock dispatcher |
| 6098 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 6099 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 6100 | |
| 6101 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 6102 | // can finally go to the newly focused "mUnfocusedWindow". |
| 6103 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6104 | mFocusedWindow->assertNoEvents(); |
| 6105 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6106 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6107 | } |
| 6108 | |
| 6109 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 6110 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 6111 | // The other window should not be affected by that. |
| 6112 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 6113 | // Touch Window 1 |
| 6114 | NotifyMotionArgs motionArgs = |
| 6115 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6116 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 6117 | mDispatcher->notifyMotion(&motionArgs); |
| 6118 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6119 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6120 | |
| 6121 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6122 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6123 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6124 | mDispatcher->notifyMotion(&motionArgs); |
| 6125 | |
| 6126 | const std::chrono::duration timeout = |
| 6127 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6128 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6129 | |
| 6130 | mUnfocusedWindow->consumeMotionDown(); |
| 6131 | mFocusedWindow->consumeMotionDown(); |
| 6132 | // Focused window may or may not receive ACTION_MOVE |
| 6133 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 6134 | InputEvent* event; |
| 6135 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 6136 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 6137 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 6138 | ASSERT_NE(nullptr, event); |
| 6139 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 6140 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 6141 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 6142 | mFocusedWindow->consumeMotionCancel(); |
| 6143 | } else { |
| 6144 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 6145 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6146 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6147 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6148 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6149 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6150 | mUnfocusedWindow->assertNoEvents(); |
| 6151 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6152 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6153 | } |
| 6154 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6155 | /** |
| 6156 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 6157 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 6158 | * |
| 6159 | * If the user touches another application during this time, the key should be dropped. |
| 6160 | * Next, if a new focused window comes in, without toggling the focused application, |
| 6161 | * then no ANR should occur. |
| 6162 | * |
| 6163 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 6164 | * but in some cases the policy may not update the focused application. |
| 6165 | */ |
| 6166 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 6167 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 6168 | std::make_shared<FakeApplicationHandle>(); |
| 6169 | focusedApplication->setDispatchingTimeout(60ms); |
| 6170 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 6171 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 6172 | mFocusedWindow->setFocusable(false); |
| 6173 | |
| 6174 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6175 | mFocusedWindow->consumeFocusEvent(false); |
| 6176 | |
| 6177 | // Send a key. The ANR timer should start because there is no focused window. |
| 6178 | // 'focusedApplication' will get blamed if this timer completes. |
| 6179 | // 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] | 6180 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6181 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6182 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6183 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6184 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6185 | |
| 6186 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 6187 | // then the injected touches won't cause the focused event to get dropped. |
| 6188 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 6189 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 6190 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 6191 | // For this test, it means that the key would get delivered to the window once it becomes |
| 6192 | // focused. |
| 6193 | std::this_thread::sleep_for(10ms); |
| 6194 | |
| 6195 | // Touch unfocused window. This should force the pending key to get dropped. |
| 6196 | NotifyMotionArgs motionArgs = |
| 6197 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6198 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 6199 | mDispatcher->notifyMotion(&motionArgs); |
| 6200 | |
| 6201 | // We do not consume the motion right away, because that would require dispatcher to first |
| 6202 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 6203 | // Set the focused window first. |
| 6204 | mFocusedWindow->setFocusable(true); |
| 6205 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6206 | setFocusedWindow(mFocusedWindow); |
| 6207 | mFocusedWindow->consumeFocusEvent(true); |
| 6208 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 6209 | // to another application. This could be a bug / behaviour in the policy. |
| 6210 | |
| 6211 | mUnfocusedWindow->consumeMotionDown(); |
| 6212 | |
| 6213 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6214 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 6215 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 6216 | } |
| 6217 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6218 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 6219 | // that has feature NO_INPUT_CHANNEL. |
| 6220 | // Layout: |
| 6221 | // Top (closest to user) |
| 6222 | // mNoInputWindow (above all windows) |
| 6223 | // mBottomWindow |
| 6224 | // Bottom (furthest from user) |
| 6225 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 6226 | virtual void SetUp() override { |
| 6227 | InputDispatcherTest::SetUp(); |
| 6228 | |
| 6229 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6230 | mNoInputWindow = |
| 6231 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 6232 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6233 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6234 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6235 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6236 | // It's perfectly valid for this window to not have an associated input channel |
| 6237 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6238 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 6239 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6240 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6241 | |
| 6242 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 6243 | } |
| 6244 | |
| 6245 | protected: |
| 6246 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 6247 | sp<FakeWindowHandle> mNoInputWindow; |
| 6248 | sp<FakeWindowHandle> mBottomWindow; |
| 6249 | }; |
| 6250 | |
| 6251 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 6252 | PointF touchedPoint = {10, 10}; |
| 6253 | |
| 6254 | NotifyMotionArgs motionArgs = |
| 6255 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6256 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 6257 | mDispatcher->notifyMotion(&motionArgs); |
| 6258 | |
| 6259 | mNoInputWindow->assertNoEvents(); |
| 6260 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 6261 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 6262 | // and therefore should prevent mBottomWindow from receiving touches |
| 6263 | mBottomWindow->assertNoEvents(); |
| 6264 | } |
| 6265 | |
| 6266 | /** |
| 6267 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 6268 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 6269 | */ |
| 6270 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 6271 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6272 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 6273 | "Window with input channel and NO_INPUT_CHANNEL", |
| 6274 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6275 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6276 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6277 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6278 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 6279 | |
| 6280 | PointF touchedPoint = {10, 10}; |
| 6281 | |
| 6282 | NotifyMotionArgs motionArgs = |
| 6283 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6284 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 6285 | mDispatcher->notifyMotion(&motionArgs); |
| 6286 | |
| 6287 | mNoInputWindow->assertNoEvents(); |
| 6288 | mBottomWindow->assertNoEvents(); |
| 6289 | } |
| 6290 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6291 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 6292 | protected: |
| 6293 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6294 | sp<FakeWindowHandle> mWindow; |
| 6295 | sp<FakeWindowHandle> mMirror; |
| 6296 | |
| 6297 | virtual void SetUp() override { |
| 6298 | InputDispatcherTest::SetUp(); |
| 6299 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6300 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 6301 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 6302 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6303 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6304 | mWindow->setFocusable(true); |
| 6305 | mMirror->setFocusable(true); |
| 6306 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6307 | } |
| 6308 | }; |
| 6309 | |
| 6310 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 6311 | // Request focus on a mirrored window |
| 6312 | setFocusedWindow(mMirror); |
| 6313 | |
| 6314 | // window gets focused |
| 6315 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6316 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6317 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6318 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6319 | } |
| 6320 | |
| 6321 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 6322 | // focusable. |
| 6323 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 6324 | setFocusedWindow(mMirror); |
| 6325 | |
| 6326 | // window gets focused |
| 6327 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6329 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6330 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6332 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6333 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6334 | |
| 6335 | mMirror->setFocusable(false); |
| 6336 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6337 | |
| 6338 | // window loses focus since one of the windows associated with the token in not focusable |
| 6339 | mWindow->consumeFocusEvent(false); |
| 6340 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6341 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6342 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6343 | mWindow->assertNoEvents(); |
| 6344 | } |
| 6345 | |
| 6346 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 6347 | // invisible. |
| 6348 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 6349 | setFocusedWindow(mMirror); |
| 6350 | |
| 6351 | // window gets focused |
| 6352 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6354 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6355 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6356 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6357 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6358 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6359 | |
| 6360 | mMirror->setVisible(false); |
| 6361 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6362 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6363 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6364 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6365 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6367 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6368 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6369 | |
| 6370 | mWindow->setVisible(false); |
| 6371 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6372 | |
| 6373 | // window loses focus only after all windows associated with the token become invisible. |
| 6374 | mWindow->consumeFocusEvent(false); |
| 6375 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6376 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6377 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6378 | mWindow->assertNoEvents(); |
| 6379 | } |
| 6380 | |
| 6381 | // A focused & mirrored window remains focused until both windows are removed. |
| 6382 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 6383 | setFocusedWindow(mMirror); |
| 6384 | |
| 6385 | // window gets focused |
| 6386 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6388 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6389 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6390 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6391 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6392 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6393 | |
| 6394 | // single window is removed but the window token remains focused |
| 6395 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 6396 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6397 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6398 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6399 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6400 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6401 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6402 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6403 | |
| 6404 | // Both windows are removed |
| 6405 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 6406 | mWindow->consumeFocusEvent(false); |
| 6407 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6408 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6409 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6410 | mWindow->assertNoEvents(); |
| 6411 | } |
| 6412 | |
| 6413 | // Focus request can be pending until one window becomes visible. |
| 6414 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 6415 | // Request focus on an invisible mirror. |
| 6416 | mWindow->setVisible(false); |
| 6417 | mMirror->setVisible(false); |
| 6418 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6419 | setFocusedWindow(mMirror); |
| 6420 | |
| 6421 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6422 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6423 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6424 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6425 | |
| 6426 | mMirror->setVisible(true); |
| 6427 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6428 | |
| 6429 | // window gets focused |
| 6430 | mWindow->consumeFocusEvent(true); |
| 6431 | // window gets the pending key event |
| 6432 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6433 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6434 | |
| 6435 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 6436 | protected: |
| 6437 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6438 | sp<FakeWindowHandle> mWindow; |
| 6439 | sp<FakeWindowHandle> mSecondWindow; |
| 6440 | |
| 6441 | void SetUp() override { |
| 6442 | InputDispatcherTest::SetUp(); |
| 6443 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6444 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6445 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6446 | mSecondWindow = |
| 6447 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6448 | mSecondWindow->setFocusable(true); |
| 6449 | |
| 6450 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 6452 | |
| 6453 | setFocusedWindow(mWindow); |
| 6454 | mWindow->consumeFocusEvent(true); |
| 6455 | } |
| 6456 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6457 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 6458 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6459 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 6460 | } |
| 6461 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6462 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 6463 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6464 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6465 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 6466 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6467 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6468 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6469 | } |
| 6470 | }; |
| 6471 | |
| 6472 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 6473 | // Ensure that capture cannot be obtained for unfocused windows. |
| 6474 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 6475 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6476 | mSecondWindow->assertNoEvents(); |
| 6477 | |
| 6478 | // Ensure that capture can be enabled from the focus window. |
| 6479 | requestAndVerifyPointerCapture(mWindow, true); |
| 6480 | |
| 6481 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 6482 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 6483 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6484 | |
| 6485 | // Ensure that capture can be disabled from the window with capture. |
| 6486 | requestAndVerifyPointerCapture(mWindow, false); |
| 6487 | } |
| 6488 | |
| 6489 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6490 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6491 | |
| 6492 | setFocusedWindow(mSecondWindow); |
| 6493 | |
| 6494 | // Ensure that the capture disabled event was sent first. |
| 6495 | mWindow->consumeCaptureEvent(false); |
| 6496 | mWindow->consumeFocusEvent(false); |
| 6497 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6498 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6499 | |
| 6500 | // 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] | 6501 | notifyPointerCaptureChanged({}); |
| 6502 | notifyPointerCaptureChanged(request); |
| 6503 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6504 | mWindow->assertNoEvents(); |
| 6505 | mSecondWindow->assertNoEvents(); |
| 6506 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6507 | } |
| 6508 | |
| 6509 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6510 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6511 | |
| 6512 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6513 | notifyPointerCaptureChanged({}); |
| 6514 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6515 | |
| 6516 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6517 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6518 | mWindow->consumeCaptureEvent(false); |
| 6519 | mWindow->assertNoEvents(); |
| 6520 | } |
| 6521 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6522 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 6523 | requestAndVerifyPointerCapture(mWindow, true); |
| 6524 | |
| 6525 | // The first window loses focus. |
| 6526 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6527 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6528 | mWindow->consumeCaptureEvent(false); |
| 6529 | |
| 6530 | // Request Pointer Capture from the second window before the notification from InputReader |
| 6531 | // arrives. |
| 6532 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6533 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6534 | |
| 6535 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6536 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6537 | |
| 6538 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6539 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6540 | |
| 6541 | mSecondWindow->consumeFocusEvent(true); |
| 6542 | mSecondWindow->consumeCaptureEvent(true); |
| 6543 | } |
| 6544 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6545 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 6546 | // App repeatedly enables and disables capture. |
| 6547 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6548 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6549 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6550 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6551 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6552 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6553 | |
| 6554 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 6555 | // first request is now stale, this should do nothing. |
| 6556 | notifyPointerCaptureChanged(firstRequest); |
| 6557 | mWindow->assertNoEvents(); |
| 6558 | |
| 6559 | // InputReader notifies that the second request was enabled. |
| 6560 | notifyPointerCaptureChanged(secondRequest); |
| 6561 | mWindow->consumeCaptureEvent(true); |
| 6562 | } |
| 6563 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 6564 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 6565 | requestAndVerifyPointerCapture(mWindow, true); |
| 6566 | |
| 6567 | // App toggles pointer capture off and on. |
| 6568 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6569 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6570 | |
| 6571 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6572 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6573 | |
| 6574 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 6575 | // preceding "disable" request. |
| 6576 | notifyPointerCaptureChanged(enableRequest); |
| 6577 | |
| 6578 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 6579 | // any notifications. |
| 6580 | mWindow->assertNoEvents(); |
| 6581 | } |
| 6582 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6583 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 6584 | protected: |
| 6585 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6586 | |
| 6587 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 6588 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 6589 | |
| 6590 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 6591 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6592 | |
| 6593 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 6594 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 6595 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6596 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 6597 | MAXIMUM_OBSCURING_OPACITY); |
| 6598 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6599 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6600 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6601 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6602 | |
| 6603 | sp<FakeWindowHandle> mTouchWindow; |
| 6604 | |
| 6605 | virtual void SetUp() override { |
| 6606 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6607 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6608 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 6609 | } |
| 6610 | |
| 6611 | virtual void TearDown() override { |
| 6612 | InputDispatcherTest::TearDown(); |
| 6613 | mTouchWindow.clear(); |
| 6614 | } |
| 6615 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6616 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 6617 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6618 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6619 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6620 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6621 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6622 | return window; |
| 6623 | } |
| 6624 | |
| 6625 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 6626 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 6627 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6628 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6629 | // Generate an arbitrary PID based on the UID |
| 6630 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 6631 | return window; |
| 6632 | } |
| 6633 | |
| 6634 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 6635 | NotifyMotionArgs args = |
| 6636 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6637 | ADISPLAY_ID_DEFAULT, points); |
| 6638 | mDispatcher->notifyMotion(&args); |
| 6639 | } |
| 6640 | }; |
| 6641 | |
| 6642 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6643 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6644 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6645 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6646 | |
| 6647 | touch(); |
| 6648 | |
| 6649 | mTouchWindow->assertNoEvents(); |
| 6650 | } |
| 6651 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6652 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6653 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 6654 | const sp<FakeWindowHandle>& w = |
| 6655 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 6656 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6657 | |
| 6658 | touch(); |
| 6659 | |
| 6660 | mTouchWindow->assertNoEvents(); |
| 6661 | } |
| 6662 | |
| 6663 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6664 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 6665 | const sp<FakeWindowHandle>& w = |
| 6666 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6667 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6668 | |
| 6669 | touch(); |
| 6670 | |
| 6671 | w->assertNoEvents(); |
| 6672 | } |
| 6673 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6674 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6675 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 6676 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6677 | |
| 6678 | touch(); |
| 6679 | |
| 6680 | mTouchWindow->consumeAnyMotionDown(); |
| 6681 | } |
| 6682 | |
| 6683 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6684 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6685 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6686 | w->setFrame(Rect(0, 0, 50, 50)); |
| 6687 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6688 | |
| 6689 | touch({PointF{100, 100}}); |
| 6690 | |
| 6691 | mTouchWindow->consumeAnyMotionDown(); |
| 6692 | } |
| 6693 | |
| 6694 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6695 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6696 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6697 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6698 | |
| 6699 | touch(); |
| 6700 | |
| 6701 | mTouchWindow->consumeAnyMotionDown(); |
| 6702 | } |
| 6703 | |
| 6704 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 6705 | const sp<FakeWindowHandle>& w = |
| 6706 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6708 | |
| 6709 | touch(); |
| 6710 | |
| 6711 | mTouchWindow->consumeAnyMotionDown(); |
| 6712 | } |
| 6713 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6714 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 6715 | const sp<FakeWindowHandle>& w = |
| 6716 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6717 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6718 | |
| 6719 | touch(); |
| 6720 | |
| 6721 | w->assertNoEvents(); |
| 6722 | } |
| 6723 | |
| 6724 | /** |
| 6725 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 6726 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 6727 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 6728 | */ |
| 6729 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6730 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 6731 | const sp<FakeWindowHandle>& w = |
| 6732 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6733 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6734 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6735 | |
| 6736 | touch(); |
| 6737 | |
| 6738 | w->consumeMotionOutside(); |
| 6739 | } |
| 6740 | |
| 6741 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 6742 | const sp<FakeWindowHandle>& w = |
| 6743 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6744 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6745 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6746 | |
| 6747 | touch(); |
| 6748 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6749 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6752 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6753 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6754 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6755 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6756 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6757 | |
| 6758 | touch(); |
| 6759 | |
| 6760 | mTouchWindow->consumeAnyMotionDown(); |
| 6761 | } |
| 6762 | |
| 6763 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 6764 | const sp<FakeWindowHandle>& w = |
| 6765 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6766 | MAXIMUM_OBSCURING_OPACITY); |
| 6767 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6768 | |
| 6769 | touch(); |
| 6770 | |
| 6771 | mTouchWindow->consumeAnyMotionDown(); |
| 6772 | } |
| 6773 | |
| 6774 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6775 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6776 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6777 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6778 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6779 | |
| 6780 | touch(); |
| 6781 | |
| 6782 | mTouchWindow->assertNoEvents(); |
| 6783 | } |
| 6784 | |
| 6785 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 6786 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 6787 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6788 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6789 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6790 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6791 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6792 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6793 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6794 | |
| 6795 | touch(); |
| 6796 | |
| 6797 | mTouchWindow->assertNoEvents(); |
| 6798 | } |
| 6799 | |
| 6800 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 6801 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 6802 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6803 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6804 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6805 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6806 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6807 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6808 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6809 | |
| 6810 | touch(); |
| 6811 | |
| 6812 | mTouchWindow->consumeAnyMotionDown(); |
| 6813 | } |
| 6814 | |
| 6815 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6816 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 6817 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6818 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6819 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6820 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6821 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6822 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6823 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6824 | |
| 6825 | touch(); |
| 6826 | |
| 6827 | mTouchWindow->consumeAnyMotionDown(); |
| 6828 | } |
| 6829 | |
| 6830 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 6831 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6832 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6833 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6834 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6835 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6836 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6837 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6838 | |
| 6839 | touch(); |
| 6840 | |
| 6841 | mTouchWindow->assertNoEvents(); |
| 6842 | } |
| 6843 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6844 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6845 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 6846 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6847 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6848 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6849 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6850 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6851 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6852 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6853 | |
| 6854 | touch(); |
| 6855 | |
| 6856 | mTouchWindow->assertNoEvents(); |
| 6857 | } |
| 6858 | |
| 6859 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6860 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 6861 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6862 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6863 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6864 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6865 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6866 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6867 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6868 | |
| 6869 | touch(); |
| 6870 | |
| 6871 | mTouchWindow->consumeAnyMotionDown(); |
| 6872 | } |
| 6873 | |
| 6874 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 6875 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6876 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6877 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6878 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6879 | |
| 6880 | touch(); |
| 6881 | |
| 6882 | mTouchWindow->consumeAnyMotionDown(); |
| 6883 | } |
| 6884 | |
| 6885 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 6886 | const sp<FakeWindowHandle>& w = |
| 6887 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6889 | |
| 6890 | touch(); |
| 6891 | |
| 6892 | mTouchWindow->consumeAnyMotionDown(); |
| 6893 | } |
| 6894 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6895 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6896 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 6897 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6898 | const sp<FakeWindowHandle>& w = |
| 6899 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 6900 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6901 | |
| 6902 | touch(); |
| 6903 | |
| 6904 | mTouchWindow->assertNoEvents(); |
| 6905 | } |
| 6906 | |
| 6907 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 6908 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6909 | const sp<FakeWindowHandle>& w = |
| 6910 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 6911 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6912 | |
| 6913 | touch(); |
| 6914 | |
| 6915 | mTouchWindow->consumeAnyMotionDown(); |
| 6916 | } |
| 6917 | |
| 6918 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6919 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 6920 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 6921 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6922 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6923 | OPACITY_ABOVE_THRESHOLD); |
| 6924 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6925 | |
| 6926 | touch(); |
| 6927 | |
| 6928 | mTouchWindow->consumeAnyMotionDown(); |
| 6929 | } |
| 6930 | |
| 6931 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6932 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 6933 | const sp<FakeWindowHandle>& w1 = |
| 6934 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6935 | OPACITY_BELOW_THRESHOLD); |
| 6936 | const sp<FakeWindowHandle>& w2 = |
| 6937 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6938 | OPACITY_BELOW_THRESHOLD); |
| 6939 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6940 | |
| 6941 | touch(); |
| 6942 | |
| 6943 | mTouchWindow->assertNoEvents(); |
| 6944 | } |
| 6945 | |
| 6946 | /** |
| 6947 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 6948 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 6949 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 6950 | */ |
| 6951 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6952 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 6953 | const sp<FakeWindowHandle>& wB = |
| 6954 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6955 | OPACITY_BELOW_THRESHOLD); |
| 6956 | const sp<FakeWindowHandle>& wC = |
| 6957 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6958 | OPACITY_BELOW_THRESHOLD); |
| 6959 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6960 | |
| 6961 | touch(); |
| 6962 | |
| 6963 | mTouchWindow->assertNoEvents(); |
| 6964 | } |
| 6965 | |
| 6966 | /** |
| 6967 | * This test is testing that a window from a different UID but with same application token doesn't |
| 6968 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 6969 | */ |
| 6970 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6971 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 6972 | const sp<FakeWindowHandle>& w = |
| 6973 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6974 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6975 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6976 | |
| 6977 | touch(); |
| 6978 | |
| 6979 | mTouchWindow->consumeAnyMotionDown(); |
| 6980 | } |
| 6981 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6982 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 6983 | protected: |
| 6984 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6985 | sp<FakeWindowHandle> mWindow; |
| 6986 | sp<FakeWindowHandle> mSecondWindow; |
| 6987 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6988 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6989 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 6990 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6991 | |
| 6992 | void SetUp() override { |
| 6993 | InputDispatcherTest::SetUp(); |
| 6994 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6995 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6996 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6997 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6998 | mSecondWindow = |
| 6999 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7000 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7001 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7002 | mSpyWindow = |
| 7003 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7004 | mSpyWindow->setSpy(true); |
| 7005 | mSpyWindow->setTrustedOverlay(true); |
| 7006 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 7007 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7008 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7009 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7010 | } |
| 7011 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7012 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 7013 | switch (fromSource) { |
| 7014 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 7015 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7016 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7017 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7018 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7019 | break; |
| 7020 | case AINPUT_SOURCE_STYLUS: |
| 7021 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7022 | injectMotionEvent( |
| 7023 | mDispatcher, |
| 7024 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7025 | AINPUT_SOURCE_STYLUS) |
| 7026 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 7027 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7028 | .x(50) |
| 7029 | .y(50)) |
| 7030 | .build())); |
| 7031 | break; |
| 7032 | case AINPUT_SOURCE_MOUSE: |
| 7033 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7034 | injectMotionEvent( |
| 7035 | mDispatcher, |
| 7036 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7037 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7038 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7039 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7040 | .x(50) |
| 7041 | .y(50)) |
| 7042 | .build())); |
| 7043 | break; |
| 7044 | default: |
| 7045 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 7046 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7047 | |
| 7048 | // Window should receive motion event. |
| 7049 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7050 | // Spy window should also receive motion event |
| 7051 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 7055 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 7056 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7057 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7058 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7059 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7060 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7061 | |
| 7062 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7063 | mDragWindow = |
| 7064 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7065 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7066 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7067 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7068 | |
| 7069 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7070 | bool transferred = |
| 7071 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7072 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7073 | if (transferred) { |
| 7074 | mWindow->consumeMotionCancel(); |
| 7075 | mDragWindow->consumeMotionDown(); |
| 7076 | } |
| 7077 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7078 | } |
| 7079 | }; |
| 7080 | |
| 7081 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7082 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7083 | |
| 7084 | // Move on window. |
| 7085 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7086 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7087 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7088 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7089 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7090 | mWindow->consumeDragEvent(false, 50, 50); |
| 7091 | mSecondWindow->assertNoEvents(); |
| 7092 | |
| 7093 | // Move to another window. |
| 7094 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7095 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7096 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7097 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7098 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7099 | mWindow->consumeDragEvent(true, 150, 50); |
| 7100 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7101 | |
| 7102 | // Move back to original window. |
| 7103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7104 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7105 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7106 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7107 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7108 | mWindow->consumeDragEvent(false, 50, 50); |
| 7109 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 7110 | |
| 7111 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7112 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7113 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7114 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7115 | mWindow->assertNoEvents(); |
| 7116 | mSecondWindow->assertNoEvents(); |
| 7117 | } |
| 7118 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7119 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7120 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7121 | |
| 7122 | // No cancel event after drag start |
| 7123 | mSpyWindow->assertNoEvents(); |
| 7124 | |
| 7125 | const MotionEvent secondFingerDownEvent = |
| 7126 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7127 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7128 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7129 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7130 | .build(); |
| 7131 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7132 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7133 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7134 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7135 | |
| 7136 | // Receives cancel for first pointer after next pointer down |
| 7137 | mSpyWindow->consumeMotionCancel(); |
| 7138 | mSpyWindow->consumeMotionDown(); |
| 7139 | |
| 7140 | mSpyWindow->assertNoEvents(); |
| 7141 | } |
| 7142 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7143 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7144 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7145 | |
| 7146 | // Move on window. |
| 7147 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7148 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7149 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7150 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7151 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7152 | mWindow->consumeDragEvent(false, 50, 50); |
| 7153 | mSecondWindow->assertNoEvents(); |
| 7154 | |
| 7155 | // Move to another window. |
| 7156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7157 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7158 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7159 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7160 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7161 | mWindow->consumeDragEvent(true, 150, 50); |
| 7162 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7163 | |
| 7164 | // drop to another window. |
| 7165 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7166 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7167 | {150, 50})) |
| 7168 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7169 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7170 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7171 | mWindow->assertNoEvents(); |
| 7172 | mSecondWindow->assertNoEvents(); |
| 7173 | } |
| 7174 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7175 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7176 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7177 | |
| 7178 | // Move on window and keep button pressed. |
| 7179 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7180 | injectMotionEvent(mDispatcher, |
| 7181 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7182 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 7183 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7184 | .x(50) |
| 7185 | .y(50)) |
| 7186 | .build())) |
| 7187 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7188 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7189 | mWindow->consumeDragEvent(false, 50, 50); |
| 7190 | mSecondWindow->assertNoEvents(); |
| 7191 | |
| 7192 | // Move to another window and release button, expect to drop item. |
| 7193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7194 | injectMotionEvent(mDispatcher, |
| 7195 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7196 | .buttonState(0) |
| 7197 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7198 | .x(150) |
| 7199 | .y(50)) |
| 7200 | .build())) |
| 7201 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7202 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7203 | mWindow->assertNoEvents(); |
| 7204 | mSecondWindow->assertNoEvents(); |
| 7205 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7206 | |
| 7207 | // nothing to the window. |
| 7208 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7209 | injectMotionEvent(mDispatcher, |
| 7210 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 7211 | .buttonState(0) |
| 7212 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7213 | .x(150) |
| 7214 | .y(50)) |
| 7215 | .build())) |
| 7216 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7217 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7218 | mWindow->assertNoEvents(); |
| 7219 | mSecondWindow->assertNoEvents(); |
| 7220 | } |
| 7221 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7222 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7223 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 7224 | |
| 7225 | // Set second window invisible. |
| 7226 | mSecondWindow->setVisible(false); |
| 7227 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 7228 | |
| 7229 | // Move on window. |
| 7230 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7231 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7232 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7233 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7234 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7235 | mWindow->consumeDragEvent(false, 50, 50); |
| 7236 | mSecondWindow->assertNoEvents(); |
| 7237 | |
| 7238 | // Move to another window. |
| 7239 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7240 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7241 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7242 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7243 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7244 | mWindow->consumeDragEvent(true, 150, 50); |
| 7245 | mSecondWindow->assertNoEvents(); |
| 7246 | |
| 7247 | // drop to another window. |
| 7248 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7249 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7250 | {150, 50})) |
| 7251 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7252 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7253 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 7254 | mWindow->assertNoEvents(); |
| 7255 | mSecondWindow->assertNoEvents(); |
| 7256 | } |
| 7257 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7258 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7259 | // Ensure window could track pointerIds if it didn't support split touch. |
| 7260 | mWindow->setPreventSplitting(true); |
| 7261 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7263 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7264 | {50, 50})) |
| 7265 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7266 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7267 | |
| 7268 | const MotionEvent secondFingerDownEvent = |
| 7269 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7270 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7271 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7272 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7273 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7274 | .build(); |
| 7275 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7276 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7277 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7278 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7279 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7280 | |
| 7281 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7282 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7283 | } |
| 7284 | |
| 7285 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 7286 | // First down on second window. |
| 7287 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7288 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7289 | {150, 50})) |
| 7290 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7291 | |
| 7292 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7293 | |
| 7294 | // Second down on first window. |
| 7295 | const MotionEvent secondFingerDownEvent = |
| 7296 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7297 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7298 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7299 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7300 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7301 | .build(); |
| 7302 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7303 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7304 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7305 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7306 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7307 | |
| 7308 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7309 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7310 | |
| 7311 | // Move on window. |
| 7312 | const MotionEvent secondFingerMoveEvent = |
| 7313 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7314 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7315 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7316 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7317 | .build(); |
| 7318 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7319 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 7320 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 7321 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7322 | mWindow->consumeDragEvent(false, 50, 50); |
| 7323 | mSecondWindow->consumeMotionMove(); |
| 7324 | |
| 7325 | // Release the drag pointer should perform drop. |
| 7326 | const MotionEvent secondFingerUpEvent = |
| 7327 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7328 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7329 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7330 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7331 | .build(); |
| 7332 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7333 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 7334 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 7335 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7336 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 7337 | mWindow->assertNoEvents(); |
| 7338 | mSecondWindow->consumeMotionMove(); |
| 7339 | } |
| 7340 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7341 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7342 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7343 | |
| 7344 | // Update window of second display. |
| 7345 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7346 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7347 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 7348 | |
| 7349 | // Let second display has a touch state. |
| 7350 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7351 | injectMotionEvent(mDispatcher, |
| 7352 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7353 | AINPUT_SOURCE_TOUCHSCREEN) |
| 7354 | .displayId(SECOND_DISPLAY_ID) |
| 7355 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7356 | .x(100) |
| 7357 | .y(100)) |
| 7358 | .build())); |
| 7359 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7360 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7361 | // Update window again. |
| 7362 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 7363 | |
| 7364 | // Move on window. |
| 7365 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7366 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7367 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7368 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7369 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7370 | mWindow->consumeDragEvent(false, 50, 50); |
| 7371 | mSecondWindow->assertNoEvents(); |
| 7372 | |
| 7373 | // Move to another window. |
| 7374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7375 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7376 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7377 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7378 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7379 | mWindow->consumeDragEvent(true, 150, 50); |
| 7380 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7381 | |
| 7382 | // drop to another window. |
| 7383 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7384 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7385 | {150, 50})) |
| 7386 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7387 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7388 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7389 | mWindow->assertNoEvents(); |
| 7390 | mSecondWindow->assertNoEvents(); |
| 7391 | } |
| 7392 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7393 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 7394 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 7395 | // Move on window. |
| 7396 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7397 | injectMotionEvent(mDispatcher, |
| 7398 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 7399 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7400 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7401 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7402 | .x(50) |
| 7403 | .y(50)) |
| 7404 | .build())) |
| 7405 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7406 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7407 | mWindow->consumeDragEvent(false, 50, 50); |
| 7408 | mSecondWindow->assertNoEvents(); |
| 7409 | |
| 7410 | // Move to another window. |
| 7411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7412 | injectMotionEvent(mDispatcher, |
| 7413 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 7414 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7415 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7416 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7417 | .x(150) |
| 7418 | .y(50)) |
| 7419 | .build())) |
| 7420 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7421 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7422 | mWindow->consumeDragEvent(true, 150, 50); |
| 7423 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7424 | |
| 7425 | // drop to another window. |
| 7426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7427 | injectMotionEvent(mDispatcher, |
| 7428 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 7429 | .buttonState(0) |
| 7430 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7431 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7432 | .x(150) |
| 7433 | .y(50)) |
| 7434 | .build())) |
| 7435 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7436 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7437 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7438 | mWindow->assertNoEvents(); |
| 7439 | mSecondWindow->assertNoEvents(); |
| 7440 | } |
| 7441 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7442 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 7443 | |
| 7444 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 7445 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7446 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7447 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7448 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7449 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7450 | window->setFocusable(true); |
| 7451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7452 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7453 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7454 | |
| 7455 | // With the flag set, window should not get any input |
| 7456 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7457 | mDispatcher->notifyKey(&keyArgs); |
| 7458 | window->assertNoEvents(); |
| 7459 | |
| 7460 | NotifyMotionArgs motionArgs = |
| 7461 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7462 | ADISPLAY_ID_DEFAULT); |
| 7463 | mDispatcher->notifyMotion(&motionArgs); |
| 7464 | window->assertNoEvents(); |
| 7465 | |
| 7466 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7467 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7468 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7469 | |
| 7470 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7471 | mDispatcher->notifyKey(&keyArgs); |
| 7472 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7473 | |
| 7474 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7475 | ADISPLAY_ID_DEFAULT); |
| 7476 | mDispatcher->notifyMotion(&motionArgs); |
| 7477 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7478 | window->assertNoEvents(); |
| 7479 | } |
| 7480 | |
| 7481 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 7482 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7483 | std::make_shared<FakeApplicationHandle>(); |
| 7484 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7485 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7486 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7487 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7488 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7489 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7490 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7491 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7492 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7493 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7494 | window->setOwnerInfo(222, 222); |
| 7495 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7496 | window->setFocusable(true); |
| 7497 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7498 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7499 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7500 | |
| 7501 | // With the flag set, window should not get any input |
| 7502 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7503 | mDispatcher->notifyKey(&keyArgs); |
| 7504 | window->assertNoEvents(); |
| 7505 | |
| 7506 | NotifyMotionArgs motionArgs = |
| 7507 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7508 | ADISPLAY_ID_DEFAULT); |
| 7509 | mDispatcher->notifyMotion(&motionArgs); |
| 7510 | window->assertNoEvents(); |
| 7511 | |
| 7512 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7513 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7514 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7515 | |
| 7516 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7517 | mDispatcher->notifyKey(&keyArgs); |
| 7518 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7519 | |
| 7520 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7521 | ADISPLAY_ID_DEFAULT); |
| 7522 | mDispatcher->notifyMotion(&motionArgs); |
| 7523 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7524 | window->assertNoEvents(); |
| 7525 | } |
| 7526 | |
| 7527 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 7528 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7529 | std::make_shared<FakeApplicationHandle>(); |
| 7530 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7531 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7532 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7533 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7534 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7535 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7536 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7537 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7538 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7539 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7540 | window->setOwnerInfo(222, 222); |
| 7541 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7542 | window->setFocusable(true); |
| 7543 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7544 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7545 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7546 | |
| 7547 | // With the flag set, window should not get any input |
| 7548 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7549 | mDispatcher->notifyKey(&keyArgs); |
| 7550 | window->assertNoEvents(); |
| 7551 | |
| 7552 | NotifyMotionArgs motionArgs = |
| 7553 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7554 | ADISPLAY_ID_DEFAULT); |
| 7555 | mDispatcher->notifyMotion(&motionArgs); |
| 7556 | window->assertNoEvents(); |
| 7557 | |
| 7558 | // When the window is no longer obscured because it went on top, it should get input |
| 7559 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 7560 | |
| 7561 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7562 | mDispatcher->notifyKey(&keyArgs); |
| 7563 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7564 | |
| 7565 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7566 | ADISPLAY_ID_DEFAULT); |
| 7567 | mDispatcher->notifyMotion(&motionArgs); |
| 7568 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7569 | window->assertNoEvents(); |
| 7570 | } |
| 7571 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7572 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 7573 | protected: |
| 7574 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7575 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7576 | sp<FakeWindowHandle> mWindow; |
| 7577 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7578 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7579 | |
| 7580 | void SetUp() override { |
| 7581 | InputDispatcherTest::SetUp(); |
| 7582 | |
| 7583 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7584 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7585 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7586 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7587 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7588 | mSecondWindow = |
| 7589 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7590 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7591 | mThirdWindow = |
| 7592 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 7593 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 7594 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7595 | |
| 7596 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7597 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 7598 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 7599 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7600 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7601 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7602 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7603 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7604 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7605 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 7606 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7607 | mThirdWindow->assertNoEvents(); |
| 7608 | } |
| 7609 | |
| 7610 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 7611 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7612 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7613 | SECOND_DISPLAY_ID)) { |
| 7614 | mWindow->assertNoEvents(); |
| 7615 | mSecondWindow->assertNoEvents(); |
| 7616 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7617 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7618 | } |
| 7619 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7620 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 7621 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7622 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 7623 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7624 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 7625 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7626 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7627 | } |
| 7628 | }; |
| 7629 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7630 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7631 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7632 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 7633 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7634 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7635 | } |
| 7636 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7637 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 7638 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7639 | int32_t ownerPid = windowInfo.ownerPid; |
| 7640 | int32_t ownerUid = windowInfo.ownerUid; |
| 7641 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 7642 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7643 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7644 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7645 | mWindow->assertNoEvents(); |
| 7646 | mSecondWindow->assertNoEvents(); |
| 7647 | } |
| 7648 | |
| 7649 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 7650 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7651 | int32_t ownerPid = windowInfo.ownerPid; |
| 7652 | int32_t ownerUid = windowInfo.ownerUid; |
| 7653 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7654 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7655 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7656 | } |
| 7657 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7658 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7659 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7660 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 7661 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7662 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7663 | mWindow->assertNoEvents(); |
| 7664 | mSecondWindow->assertNoEvents(); |
| 7665 | } |
| 7666 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7667 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 7668 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 7669 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7670 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7671 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7672 | mWindow->assertNoEvents(); |
| 7673 | mSecondWindow->assertNoEvents(); |
| 7674 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 7675 | } |
| 7676 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7677 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 7678 | // Interact with the window first. |
| 7679 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 7680 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7681 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7682 | |
| 7683 | // Then remove focus. |
| 7684 | mWindow->setFocusable(false); |
| 7685 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7686 | |
| 7687 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 7688 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7689 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7690 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7691 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7692 | } |
| 7693 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7694 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 7695 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7696 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7697 | std::shared_ptr<FakeApplicationHandle> application = |
| 7698 | std::make_shared<FakeApplicationHandle>(); |
| 7699 | std::string name = "Fake Spy "; |
| 7700 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7701 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7702 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7703 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7704 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7705 | return spy; |
| 7706 | } |
| 7707 | |
| 7708 | sp<FakeWindowHandle> createForeground() { |
| 7709 | std::shared_ptr<FakeApplicationHandle> application = |
| 7710 | std::make_shared<FakeApplicationHandle>(); |
| 7711 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7712 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7713 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7714 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7715 | return window; |
| 7716 | } |
| 7717 | |
| 7718 | private: |
| 7719 | int mSpyCount{0}; |
| 7720 | }; |
| 7721 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7722 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7723 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7724 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 7725 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7726 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 7727 | ScopedSilentDeath _silentDeath; |
| 7728 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7729 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7730 | spy->setTrustedOverlay(false); |
| 7731 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 7732 | ".* not a trusted overlay"); |
| 7733 | } |
| 7734 | |
| 7735 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7736 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 7737 | */ |
| 7738 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7739 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7740 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 7741 | |
| 7742 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7743 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7744 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7745 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7746 | } |
| 7747 | |
| 7748 | /** |
| 7749 | * Verify the order in which different input windows receive events. The touched foreground window |
| 7750 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 7751 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 7752 | * receive events before ones belows it. |
| 7753 | * |
| 7754 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 7755 | * spy1, spy2, window, spy3. |
| 7756 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 7757 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 7758 | * window. |
| 7759 | */ |
| 7760 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 7761 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7762 | auto spy1 = createSpy(); |
| 7763 | auto spy2 = createSpy(); |
| 7764 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7765 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 7766 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 7767 | const size_t numChannels = channels.size(); |
| 7768 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 7769 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7770 | if (!epollFd.ok()) { |
| 7771 | FAIL() << "Failed to create epoll fd"; |
| 7772 | } |
| 7773 | |
| 7774 | for (size_t i = 0; i < numChannels; i++) { |
| 7775 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 7776 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 7777 | FAIL() << "Failed to add fd to epoll"; |
| 7778 | } |
| 7779 | } |
| 7780 | |
| 7781 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7782 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7783 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7784 | |
| 7785 | std::vector<size_t> eventOrder; |
| 7786 | std::vector<struct epoll_event> events(numChannels); |
| 7787 | for (;;) { |
| 7788 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 7789 | (100ms).count()); |
| 7790 | if (nFds < 0) { |
| 7791 | FAIL() << "Failed to call epoll_wait"; |
| 7792 | } |
| 7793 | if (nFds == 0) { |
| 7794 | break; // epoll_wait timed out |
| 7795 | } |
| 7796 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 7797 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 7798 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7799 | channels[i]->consumeMotionDown(); |
| 7800 | } |
| 7801 | } |
| 7802 | |
| 7803 | // Verify the order in which the events were received. |
| 7804 | EXPECT_EQ(3u, eventOrder.size()); |
| 7805 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 7806 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 7807 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 7808 | } |
| 7809 | |
| 7810 | /** |
| 7811 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 7812 | */ |
| 7813 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 7814 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7815 | auto spy = createSpy(); |
| 7816 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7817 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7818 | |
| 7819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7820 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7821 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7822 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7823 | spy->assertNoEvents(); |
| 7824 | } |
| 7825 | |
| 7826 | /** |
| 7827 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 7828 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 7829 | * to the window. |
| 7830 | */ |
| 7831 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 7832 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7833 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7834 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 7835 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7836 | |
| 7837 | // Inject an event outside the spy window's touchable region. |
| 7838 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7839 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7840 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7841 | window->consumeMotionDown(); |
| 7842 | spy->assertNoEvents(); |
| 7843 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7844 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7845 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7846 | window->consumeMotionUp(); |
| 7847 | spy->assertNoEvents(); |
| 7848 | |
| 7849 | // Inject an event inside the spy window's touchable region. |
| 7850 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7851 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7852 | {5, 10})) |
| 7853 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7854 | window->consumeMotionDown(); |
| 7855 | spy->consumeMotionDown(); |
| 7856 | } |
| 7857 | |
| 7858 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7859 | * 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] | 7860 | * 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] | 7861 | */ |
| 7862 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 7863 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7864 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7865 | auto spy = createSpy(); |
| 7866 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7867 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7868 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 7869 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7870 | |
| 7871 | // Inject an event outside the spy window's frame and touchable region. |
| 7872 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7873 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7874 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7875 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7876 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7877 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7878 | } |
| 7879 | |
| 7880 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7881 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 7882 | * pointers that are down within its bounds. |
| 7883 | */ |
| 7884 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 7885 | auto windowLeft = createForeground(); |
| 7886 | windowLeft->setFrame({0, 0, 100, 200}); |
| 7887 | auto windowRight = createForeground(); |
| 7888 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7889 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7890 | spy->setFrame({0, 0, 200, 200}); |
| 7891 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 7892 | |
| 7893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7894 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7895 | {50, 50})) |
| 7896 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7897 | windowLeft->consumeMotionDown(); |
| 7898 | spy->consumeMotionDown(); |
| 7899 | |
| 7900 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7901 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7902 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7903 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7904 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7905 | .build(); |
| 7906 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7907 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7908 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7909 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7910 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7911 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7912 | } |
| 7913 | |
| 7914 | /** |
| 7915 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 7916 | * the spy should receive the second pointer with ACTION_DOWN. |
| 7917 | */ |
| 7918 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 7919 | auto window = createForeground(); |
| 7920 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7921 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7922 | spyRight->setFrame({100, 0, 200, 200}); |
| 7923 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 7924 | |
| 7925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7926 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7927 | {50, 50})) |
| 7928 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7929 | window->consumeMotionDown(); |
| 7930 | spyRight->assertNoEvents(); |
| 7931 | |
| 7932 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7933 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7934 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7935 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7936 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7937 | .build(); |
| 7938 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7939 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7940 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7941 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7942 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7943 | spyRight->consumeMotionDown(); |
| 7944 | } |
| 7945 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7946 | /** |
| 7947 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 7948 | * windows should be allowed to control split touch. |
| 7949 | */ |
| 7950 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7951 | // 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] | 7952 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7953 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7954 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7955 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7956 | auto window = createForeground(); |
| 7957 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7958 | |
| 7959 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7960 | |
| 7961 | // First finger down, no window touched. |
| 7962 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7963 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7964 | {100, 200})) |
| 7965 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7966 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7967 | window->assertNoEvents(); |
| 7968 | |
| 7969 | // Second finger down on window, the window should receive touch down. |
| 7970 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7971 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7972 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7973 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7974 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 7975 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7976 | .build(); |
| 7977 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7978 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7979 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7980 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7981 | |
| 7982 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7983 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | /** |
| 7987 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 7988 | * do not receive key events. |
| 7989 | */ |
| 7990 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7991 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7992 | spy->setFocusable(false); |
| 7993 | |
| 7994 | auto window = createForeground(); |
| 7995 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7996 | setFocusedWindow(window); |
| 7997 | window->consumeFocusEvent(true); |
| 7998 | |
| 7999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 8000 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8001 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8002 | |
| 8003 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 8004 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8005 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8006 | |
| 8007 | spy->assertNoEvents(); |
| 8008 | } |
| 8009 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8010 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 8011 | |
| 8012 | /** |
| 8013 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 8014 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 8015 | */ |
| 8016 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 8017 | auto window = createForeground(); |
| 8018 | auto spy1 = createSpy(); |
| 8019 | auto spy2 = createSpy(); |
| 8020 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 8021 | |
| 8022 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8023 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8024 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8025 | window->consumeMotionDown(); |
| 8026 | spy1->consumeMotionDown(); |
| 8027 | spy2->consumeMotionDown(); |
| 8028 | |
| 8029 | // Pilfer pointers from the second spy window. |
| 8030 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 8031 | spy2->assertNoEvents(); |
| 8032 | spy1->consumeMotionCancel(); |
| 8033 | window->consumeMotionCancel(); |
| 8034 | |
| 8035 | // The rest of the gesture should only be sent to the second spy window. |
| 8036 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8037 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8038 | ADISPLAY_ID_DEFAULT)) |
| 8039 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8040 | spy2->consumeMotionMove(); |
| 8041 | spy1->assertNoEvents(); |
| 8042 | window->assertNoEvents(); |
| 8043 | } |
| 8044 | |
| 8045 | /** |
| 8046 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 8047 | * in the middle of the gesture. |
| 8048 | */ |
| 8049 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 8050 | auto window = createForeground(); |
| 8051 | auto spy = createSpy(); |
| 8052 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8053 | |
| 8054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8055 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8056 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8057 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8058 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8059 | |
| 8060 | window->releaseChannel(); |
| 8061 | |
| 8062 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8063 | |
| 8064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8065 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8066 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8067 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8068 | } |
| 8069 | |
| 8070 | /** |
| 8071 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 8072 | * the spy, but not to any other windows. |
| 8073 | */ |
| 8074 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 8075 | auto spy = createSpy(); |
| 8076 | auto window = createForeground(); |
| 8077 | |
| 8078 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8079 | |
| 8080 | // First finger down on the window and the spy. |
| 8081 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8082 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8083 | {100, 200})) |
| 8084 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8085 | spy->consumeMotionDown(); |
| 8086 | window->consumeMotionDown(); |
| 8087 | |
| 8088 | // Spy window pilfers the pointers. |
| 8089 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8090 | window->consumeMotionCancel(); |
| 8091 | |
| 8092 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 8093 | const MotionEvent secondFingerDownEvent = |
| 8094 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8095 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8096 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8097 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 8098 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8099 | .build(); |
| 8100 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8101 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8102 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8103 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8104 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8105 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8106 | |
| 8107 | // Third finger goes down outside all windows, so injection should fail. |
| 8108 | const MotionEvent thirdFingerDownEvent = |
| 8109 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8110 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8111 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8112 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 8113 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8114 | .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8115 | .build(); |
| 8116 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 8117 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8118 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8119 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8120 | |
| 8121 | spy->assertNoEvents(); |
| 8122 | window->assertNoEvents(); |
| 8123 | } |
| 8124 | |
| 8125 | /** |
| 8126 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 8127 | */ |
| 8128 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 8129 | auto spy = createSpy(); |
| 8130 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8131 | auto window = createForeground(); |
| 8132 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8133 | |
| 8134 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8135 | |
| 8136 | // First finger down on the window only |
| 8137 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8138 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8139 | {150, 150})) |
| 8140 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8141 | window->consumeMotionDown(); |
| 8142 | |
| 8143 | // Second finger down on the spy and window |
| 8144 | const MotionEvent secondFingerDownEvent = |
| 8145 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8146 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8147 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8148 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
| 8149 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8150 | .build(); |
| 8151 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8152 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8153 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8154 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8155 | spy->consumeMotionDown(); |
| 8156 | window->consumeMotionPointerDown(1); |
| 8157 | |
| 8158 | // Third finger down on the spy and window |
| 8159 | const MotionEvent thirdFingerDownEvent = |
| 8160 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8161 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8162 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8163 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
| 8164 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8165 | .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8166 | .build(); |
| 8167 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8168 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8169 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8170 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8171 | spy->consumeMotionPointerDown(1); |
| 8172 | window->consumeMotionPointerDown(2); |
| 8173 | |
| 8174 | // Spy window pilfers the pointers. |
| 8175 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8176 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8177 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8178 | |
| 8179 | spy->assertNoEvents(); |
| 8180 | window->assertNoEvents(); |
| 8181 | } |
| 8182 | |
| 8183 | /** |
| 8184 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 8185 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 8186 | * window, then that window should receive ACTION_CANCEL. |
| 8187 | */ |
| 8188 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 8189 | auto spy = createSpy(); |
| 8190 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8191 | auto window = createForeground(); |
| 8192 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8193 | |
| 8194 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8195 | |
| 8196 | // First finger down on both spy and window |
| 8197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8198 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8199 | {10, 10})) |
| 8200 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8201 | window->consumeMotionDown(); |
| 8202 | spy->consumeMotionDown(); |
| 8203 | |
| 8204 | // Second finger down on the spy and window |
| 8205 | const MotionEvent secondFingerDownEvent = |
| 8206 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8207 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8208 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8209 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8210 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8211 | .build(); |
| 8212 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8213 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8214 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8215 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8216 | spy->consumeMotionPointerDown(1); |
| 8217 | window->consumeMotionPointerDown(1); |
| 8218 | |
| 8219 | // Spy window pilfers the pointers. |
| 8220 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8221 | window->consumeMotionCancel(); |
| 8222 | |
| 8223 | spy->assertNoEvents(); |
| 8224 | window->assertNoEvents(); |
| 8225 | } |
| 8226 | |
| 8227 | /** |
| 8228 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 8229 | * be sent to other windows |
| 8230 | */ |
| 8231 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 8232 | auto spy = createSpy(); |
| 8233 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8234 | auto window = createForeground(); |
| 8235 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8236 | |
| 8237 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8238 | |
| 8239 | // First finger down on both window and spy |
| 8240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8241 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8242 | {10, 10})) |
| 8243 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8244 | window->consumeMotionDown(); |
| 8245 | spy->consumeMotionDown(); |
| 8246 | |
| 8247 | // Spy window pilfers the pointers. |
| 8248 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8249 | window->consumeMotionCancel(); |
| 8250 | |
| 8251 | // Second finger down on the window only |
| 8252 | const MotionEvent secondFingerDownEvent = |
| 8253 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8254 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8255 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8256 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8257 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8258 | .build(); |
| 8259 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8260 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8261 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8262 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8263 | window->consumeMotionDown(); |
| 8264 | window->assertNoEvents(); |
| 8265 | |
| 8266 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 8267 | spy->consumeMotionMove(); |
| 8268 | spy->assertNoEvents(); |
| 8269 | } |
| 8270 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8271 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 8272 | public: |
| 8273 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 8274 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8275 | std::make_shared<FakeApplicationHandle>(); |
| 8276 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8277 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 8278 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8279 | overlay->setFocusable(false); |
| 8280 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8281 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8282 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8283 | overlay->setTrustedOverlay(true); |
| 8284 | |
| 8285 | std::shared_ptr<FakeApplicationHandle> application = |
| 8286 | std::make_shared<FakeApplicationHandle>(); |
| 8287 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8288 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 8289 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8290 | window->setFocusable(true); |
| 8291 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8292 | |
| 8293 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8294 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8295 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8296 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8297 | return {std::move(overlay), std::move(window)}; |
| 8298 | } |
| 8299 | |
| 8300 | void sendFingerEvent(int32_t action) { |
| 8301 | NotifyMotionArgs motionArgs = |
| 8302 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 8303 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 8304 | mDispatcher->notifyMotion(&motionArgs); |
| 8305 | } |
| 8306 | |
| 8307 | void sendStylusEvent(int32_t action) { |
| 8308 | NotifyMotionArgs motionArgs = |
| 8309 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 8310 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 8311 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 8312 | mDispatcher->notifyMotion(&motionArgs); |
| 8313 | } |
| 8314 | }; |
| 8315 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8316 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 8317 | |
| 8318 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 8319 | ScopedSilentDeath _silentDeath; |
| 8320 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8321 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8322 | overlay->setTrustedOverlay(false); |
| 8323 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 8324 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 8325 | ".* not a trusted overlay"); |
| 8326 | } |
| 8327 | |
| 8328 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 8329 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8330 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8331 | |
| 8332 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8333 | overlay->consumeMotionDown(); |
| 8334 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8335 | overlay->consumeMotionUp(); |
| 8336 | |
| 8337 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8338 | window->consumeMotionDown(); |
| 8339 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 8340 | window->consumeMotionUp(); |
| 8341 | |
| 8342 | overlay->assertNoEvents(); |
| 8343 | window->assertNoEvents(); |
| 8344 | } |
| 8345 | |
| 8346 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 8347 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8348 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8349 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8350 | |
| 8351 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8352 | overlay->consumeMotionDown(); |
| 8353 | window->consumeMotionDown(); |
| 8354 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8355 | overlay->consumeMotionUp(); |
| 8356 | window->consumeMotionUp(); |
| 8357 | |
| 8358 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8359 | window->consumeMotionDown(); |
| 8360 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 8361 | window->consumeMotionUp(); |
| 8362 | |
| 8363 | overlay->assertNoEvents(); |
| 8364 | window->assertNoEvents(); |
| 8365 | } |
| 8366 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 8367 | /** |
| 8368 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 8369 | * The scenario is as follows: |
| 8370 | * - The stylus interceptor overlay is configured as a spy window. |
| 8371 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 8372 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 8373 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 8374 | */ |
| 8375 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 8376 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8377 | overlay->setSpy(true); |
| 8378 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8379 | |
| 8380 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8381 | overlay->consumeMotionDown(); |
| 8382 | window->consumeMotionDown(); |
| 8383 | |
| 8384 | // The interceptor pilfers the pointers. |
| 8385 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 8386 | window->consumeMotionCancel(); |
| 8387 | |
| 8388 | // The interceptor configures itself so that it is no longer a spy. |
| 8389 | overlay->setSpy(false); |
| 8390 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8391 | |
| 8392 | // It continues to receive the rest of the stylus gesture. |
| 8393 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 8394 | overlay->consumeMotionMove(); |
| 8395 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8396 | overlay->consumeMotionUp(); |
| 8397 | |
| 8398 | window->assertNoEvents(); |
| 8399 | } |
| 8400 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8401 | struct User { |
| 8402 | int32_t mPid; |
| 8403 | int32_t mUid; |
| 8404 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 8405 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 8406 | |
| 8407 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 8408 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 8409 | |
| 8410 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 8411 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 8412 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 8413 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8414 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 8415 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8416 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 8417 | } |
| 8418 | |
| 8419 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8420 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8421 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8422 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8423 | mPolicyFlags); |
| 8424 | } |
| 8425 | |
| 8426 | sp<FakeWindowHandle> createWindow() const { |
| 8427 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8428 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8429 | sp<FakeWindowHandle> window = |
| 8430 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 8431 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8432 | window->setOwnerInfo(mPid, mUid); |
| 8433 | return window; |
| 8434 | } |
| 8435 | }; |
| 8436 | |
| 8437 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 8438 | |
| 8439 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 8440 | auto owner = User(mDispatcher, 10, 11); |
| 8441 | auto window = owner.createWindow(); |
| 8442 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8443 | |
| 8444 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8445 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8446 | window->consumeMotionDown(); |
| 8447 | |
| 8448 | setFocusedWindow(window); |
| 8449 | window->consumeFocusEvent(true); |
| 8450 | |
| 8451 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8452 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8453 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8454 | } |
| 8455 | |
| 8456 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 8457 | auto owner = User(mDispatcher, 10, 11); |
| 8458 | auto window = owner.createWindow(); |
| 8459 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8460 | |
| 8461 | auto rando = User(mDispatcher, 20, 21); |
| 8462 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8463 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8464 | |
| 8465 | setFocusedWindow(window); |
| 8466 | window->consumeFocusEvent(true); |
| 8467 | |
| 8468 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8469 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8470 | window->assertNoEvents(); |
| 8471 | } |
| 8472 | |
| 8473 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 8474 | auto owner = User(mDispatcher, 10, 11); |
| 8475 | auto window = owner.createWindow(); |
| 8476 | auto spy = owner.createWindow(); |
| 8477 | spy->setSpy(true); |
| 8478 | spy->setTrustedOverlay(true); |
| 8479 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8480 | |
| 8481 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8482 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8483 | spy->consumeMotionDown(); |
| 8484 | window->consumeMotionDown(); |
| 8485 | } |
| 8486 | |
| 8487 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 8488 | auto owner = User(mDispatcher, 10, 11); |
| 8489 | auto window = owner.createWindow(); |
| 8490 | |
| 8491 | auto rando = User(mDispatcher, 20, 21); |
| 8492 | auto randosSpy = rando.createWindow(); |
| 8493 | randosSpy->setSpy(true); |
| 8494 | randosSpy->setTrustedOverlay(true); |
| 8495 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8496 | |
| 8497 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 8498 | // not receive the event. |
| 8499 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8500 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8501 | randosSpy->assertNoEvents(); |
| 8502 | window->consumeMotionDown(); |
| 8503 | } |
| 8504 | |
| 8505 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 8506 | auto owner = User(mDispatcher, 10, 11); |
| 8507 | auto window = owner.createWindow(); |
| 8508 | |
| 8509 | auto rando = User(mDispatcher, 20, 21); |
| 8510 | auto randosSpy = rando.createWindow(); |
| 8511 | randosSpy->setSpy(true); |
| 8512 | randosSpy->setTrustedOverlay(true); |
| 8513 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8514 | |
| 8515 | // A user that has injection permission can inject into any window. |
| 8516 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8517 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8518 | ADISPLAY_ID_DEFAULT)); |
| 8519 | randosSpy->consumeMotionDown(); |
| 8520 | window->consumeMotionDown(); |
| 8521 | |
| 8522 | setFocusedWindow(randosSpy); |
| 8523 | randosSpy->consumeFocusEvent(true); |
| 8524 | |
| 8525 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 8526 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8527 | window->assertNoEvents(); |
| 8528 | } |
| 8529 | |
| 8530 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 8531 | auto owner = User(mDispatcher, 10, 11); |
| 8532 | auto window = owner.createWindow(); |
| 8533 | |
| 8534 | auto rando = User(mDispatcher, 20, 21); |
| 8535 | auto randosWindow = rando.createWindow(); |
| 8536 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 8537 | randosWindow->setWatchOutsideTouch(true); |
| 8538 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 8539 | |
| 8540 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 8541 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8542 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8543 | window->consumeMotionDown(); |
| 8544 | randosWindow->consumeMotionOutside(); |
| 8545 | } |
| 8546 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 8547 | } // namespace android::inputdispatcher |