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 | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame^] | 2271 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2272 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2273 | * touch is gone, stylus hovering should start working again. |
| 2274 | */ |
| 2275 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 2276 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2277 | sp<FakeWindowHandle> window = |
| 2278 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2279 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2280 | |
| 2281 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2282 | |
| 2283 | const int32_t stylusDeviceId = 5; |
| 2284 | const int32_t touchDeviceId = 4; |
| 2285 | // Start hovering with stylus |
| 2286 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2287 | injectMotionEvent(mDispatcher, |
| 2288 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2289 | AINPUT_SOURCE_STYLUS) |
| 2290 | .deviceId(stylusDeviceId) |
| 2291 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 2292 | .x(50) |
| 2293 | .y(50)) |
| 2294 | .build())); |
| 2295 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2296 | |
| 2297 | // Finger down on the window |
| 2298 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2299 | injectMotionEvent(mDispatcher, |
| 2300 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2301 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2302 | .deviceId(touchDeviceId) |
| 2303 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2304 | .x(100) |
| 2305 | .y(100)) |
| 2306 | .build())); |
| 2307 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2308 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2309 | |
| 2310 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2311 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2312 | injectMotionEvent(mDispatcher, |
| 2313 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2314 | AINPUT_SOURCE_STYLUS) |
| 2315 | .deviceId(stylusDeviceId) |
| 2316 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 2317 | .x(50) |
| 2318 | .y(50)) |
| 2319 | .build())); |
| 2320 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2321 | // is already down. |
| 2322 | |
| 2323 | // Lift up the finger |
| 2324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2325 | injectMotionEvent(mDispatcher, |
| 2326 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2327 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2328 | .deviceId(touchDeviceId) |
| 2329 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2330 | .x(100) |
| 2331 | .y(100)) |
| 2332 | .build())); |
| 2333 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2334 | |
| 2335 | // Now that the touch is gone, stylus hovering should start working again |
| 2336 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2337 | injectMotionEvent(mDispatcher, |
| 2338 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2339 | AINPUT_SOURCE_STYLUS) |
| 2340 | .deviceId(stylusDeviceId) |
| 2341 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 2342 | .x(50) |
| 2343 | .y(50)) |
| 2344 | .build())); |
| 2345 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2346 | // No more events |
| 2347 | window->assertNoEvents(); |
| 2348 | } |
| 2349 | |
| 2350 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2351 | * On the display, have a single window, and also an area where there's no window. |
| 2352 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2353 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2354 | */ |
| 2355 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2356 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2357 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2358 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2359 | |
| 2360 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 2361 | NotifyMotionArgs args; |
| 2362 | |
| 2363 | // Touch down on the empty space |
| 2364 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 2365 | |
| 2366 | mDispatcher->waitForIdle(); |
| 2367 | window->assertNoEvents(); |
| 2368 | |
| 2369 | // Now touch down on the window with another pointer |
| 2370 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 2371 | mDispatcher->waitForIdle(); |
| 2372 | window->consumeMotionDown(); |
| 2373 | } |
| 2374 | |
| 2375 | /** |
| 2376 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2377 | * non-touchable window. |
| 2378 | */ |
| 2379 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2380 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2381 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2382 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2383 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2384 | window1->setTouchable(false); |
| 2385 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2386 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2387 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2388 | |
| 2389 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2390 | |
| 2391 | NotifyMotionArgs args; |
| 2392 | // Touch down on the non-touchable window |
| 2393 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2394 | |
| 2395 | mDispatcher->waitForIdle(); |
| 2396 | window1->assertNoEvents(); |
| 2397 | window2->assertNoEvents(); |
| 2398 | |
| 2399 | // Now touch down on the window with another pointer |
| 2400 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2401 | mDispatcher->waitForIdle(); |
| 2402 | window2->consumeMotionDown(); |
| 2403 | } |
| 2404 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2405 | /** |
| 2406 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2407 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2408 | */ |
| 2409 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2410 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2411 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2412 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2413 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2414 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2415 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2416 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2417 | |
| 2418 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2419 | |
| 2420 | NotifyMotionArgs args; |
| 2421 | // Touch down on the first window |
| 2422 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2423 | |
| 2424 | mDispatcher->waitForIdle(); |
| 2425 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2426 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2427 | window2->assertNoEvents(); |
| 2428 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2429 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2430 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2431 | |
| 2432 | // Now touch down on the window with another pointer |
| 2433 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2434 | mDispatcher->waitForIdle(); |
| 2435 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2436 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2437 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2438 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2439 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2440 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2441 | |
| 2442 | // Now move the pointer on the second window |
| 2443 | mDispatcher->notifyMotion( |
| 2444 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); |
| 2445 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2446 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2447 | |
| 2448 | // Now add new touch down on the second window |
| 2449 | mDispatcher->notifyMotion( |
| 2450 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); |
| 2451 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2452 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2453 | |
| 2454 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2455 | window1->consumeMotionMove(); |
| 2456 | window1->assertNoEvents(); |
| 2457 | |
| 2458 | // Now move the pointer on the first window |
| 2459 | mDispatcher->notifyMotion( |
| 2460 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}}))); |
| 2461 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2462 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2463 | |
| 2464 | mDispatcher->notifyMotion(&( |
| 2465 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); |
| 2466 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2467 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2470 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2471 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2472 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2473 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2474 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2475 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2476 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2477 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2478 | |
| 2479 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2480 | |
| 2481 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 2482 | |
| 2483 | // 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] | 2484 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2485 | injectMotionEvent(mDispatcher, |
| 2486 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2487 | AINPUT_SOURCE_MOUSE) |
| 2488 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2489 | .x(900) |
| 2490 | .y(400)) |
| 2491 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2492 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2493 | |
| 2494 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2495 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2496 | injectMotionEvent(mDispatcher, |
| 2497 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2498 | AINPUT_SOURCE_MOUSE) |
| 2499 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2500 | .x(300) |
| 2501 | .y(400)) |
| 2502 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2503 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2504 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2505 | |
| 2506 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2507 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2508 | injectMotionEvent(mDispatcher, |
| 2509 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2510 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2511 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2512 | .x(300) |
| 2513 | .y(400)) |
| 2514 | .build())); |
| 2515 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2516 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2517 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2518 | injectMotionEvent(mDispatcher, |
| 2519 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2520 | AINPUT_SOURCE_MOUSE) |
| 2521 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2522 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2523 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2524 | .x(300) |
| 2525 | .y(400)) |
| 2526 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2527 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2528 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2529 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2530 | injectMotionEvent(mDispatcher, |
| 2531 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2532 | AINPUT_SOURCE_MOUSE) |
| 2533 | .buttonState(0) |
| 2534 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2535 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2536 | .x(300) |
| 2537 | .y(400)) |
| 2538 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2539 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2540 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2541 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2542 | injectMotionEvent(mDispatcher, |
| 2543 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2544 | .buttonState(0) |
| 2545 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2546 | .x(300) |
| 2547 | .y(400)) |
| 2548 | .build())); |
| 2549 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2550 | |
| 2551 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2552 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2553 | injectMotionEvent(mDispatcher, |
| 2554 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2555 | AINPUT_SOURCE_MOUSE) |
| 2556 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2557 | .x(900) |
| 2558 | .y(400)) |
| 2559 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2560 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2561 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2562 | |
| 2563 | // No more events |
| 2564 | windowLeft->assertNoEvents(); |
| 2565 | windowRight->assertNoEvents(); |
| 2566 | } |
| 2567 | |
| 2568 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 2569 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2570 | |
| 2571 | sp<FakeWindowHandle> spyWindow = |
| 2572 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2573 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2574 | spyWindow->setTrustedOverlay(true); |
| 2575 | spyWindow->setSpy(true); |
| 2576 | sp<FakeWindowHandle> window = |
| 2577 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2578 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2579 | |
| 2580 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2582 | |
| 2583 | // Send mouse cursor to the window |
| 2584 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2585 | injectMotionEvent(mDispatcher, |
| 2586 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2587 | AINPUT_SOURCE_MOUSE) |
| 2588 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2589 | .x(100) |
| 2590 | .y(100)) |
| 2591 | .build())); |
| 2592 | |
| 2593 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2594 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2595 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2596 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2597 | |
| 2598 | window->assertNoEvents(); |
| 2599 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2600 | } |
| 2601 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2602 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 2603 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2604 | |
| 2605 | sp<FakeWindowHandle> spyWindow = |
| 2606 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2607 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2608 | spyWindow->setTrustedOverlay(true); |
| 2609 | spyWindow->setSpy(true); |
| 2610 | sp<FakeWindowHandle> window = |
| 2611 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2612 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2613 | |
| 2614 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2615 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2616 | |
| 2617 | // Send mouse cursor to the window |
| 2618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2619 | injectMotionEvent(mDispatcher, |
| 2620 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2621 | AINPUT_SOURCE_MOUSE) |
| 2622 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2623 | .x(100) |
| 2624 | .y(100)) |
| 2625 | .build())); |
| 2626 | |
| 2627 | // Move mouse cursor |
| 2628 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2629 | injectMotionEvent(mDispatcher, |
| 2630 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2631 | AINPUT_SOURCE_MOUSE) |
| 2632 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2633 | .x(110) |
| 2634 | .y(110)) |
| 2635 | .build())); |
| 2636 | |
| 2637 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2638 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2639 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2640 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2641 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2642 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2643 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2644 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2645 | // Touch down on the window |
| 2646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2647 | injectMotionEvent(mDispatcher, |
| 2648 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2649 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2650 | .deviceId(SECOND_DEVICE_ID) |
| 2651 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2652 | .x(200) |
| 2653 | .y(200)) |
| 2654 | .build())); |
| 2655 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2656 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2657 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2658 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2659 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2660 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2661 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2662 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2663 | |
| 2664 | // pilfer the motion, retaining the gesture on the spy window. |
| 2665 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2666 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 2667 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2668 | |
| 2669 | // Touch UP on the window |
| 2670 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2671 | injectMotionEvent(mDispatcher, |
| 2672 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2673 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2674 | .deviceId(SECOND_DEVICE_ID) |
| 2675 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2676 | .x(200) |
| 2677 | .y(200)) |
| 2678 | .build())); |
| 2679 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2680 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2681 | |
| 2682 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 2683 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 2684 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 2685 | |
| 2686 | // One more tap - DOWN |
| 2687 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2688 | injectMotionEvent(mDispatcher, |
| 2689 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2690 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2691 | .deviceId(SECOND_DEVICE_ID) |
| 2692 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2693 | .x(250) |
| 2694 | .y(250)) |
| 2695 | .build())); |
| 2696 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2697 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2698 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2699 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2700 | |
| 2701 | // Touch UP on the window |
| 2702 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2703 | injectMotionEvent(mDispatcher, |
| 2704 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2705 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2706 | .deviceId(SECOND_DEVICE_ID) |
| 2707 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2708 | .x(250) |
| 2709 | .y(250)) |
| 2710 | .build())); |
| 2711 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2712 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2713 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2714 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2715 | |
| 2716 | window->assertNoEvents(); |
| 2717 | spyWindow->assertNoEvents(); |
| 2718 | } |
| 2719 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2720 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 2721 | // directly in this test. |
| 2722 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2723 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2724 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2725 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2726 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2727 | |
| 2728 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2729 | |
| 2730 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2731 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2732 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2733 | injectMotionEvent(mDispatcher, |
| 2734 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2735 | AINPUT_SOURCE_MOUSE) |
| 2736 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2737 | .x(300) |
| 2738 | .y(400)) |
| 2739 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2740 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2741 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2742 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2743 | injectMotionEvent(mDispatcher, |
| 2744 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2745 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2746 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2747 | .x(300) |
| 2748 | .y(400)) |
| 2749 | .build())); |
| 2750 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2751 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2752 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2753 | injectMotionEvent(mDispatcher, |
| 2754 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2755 | AINPUT_SOURCE_MOUSE) |
| 2756 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2757 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2758 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2759 | .x(300) |
| 2760 | .y(400)) |
| 2761 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2762 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2763 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2764 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2765 | injectMotionEvent(mDispatcher, |
| 2766 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2767 | AINPUT_SOURCE_MOUSE) |
| 2768 | .buttonState(0) |
| 2769 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2770 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2771 | .x(300) |
| 2772 | .y(400)) |
| 2773 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2774 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2775 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2776 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2777 | injectMotionEvent(mDispatcher, |
| 2778 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2779 | .buttonState(0) |
| 2780 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2781 | .x(300) |
| 2782 | .y(400)) |
| 2783 | .build())); |
| 2784 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2785 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2786 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2787 | injectMotionEvent(mDispatcher, |
| 2788 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2789 | AINPUT_SOURCE_MOUSE) |
| 2790 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2791 | .x(300) |
| 2792 | .y(400)) |
| 2793 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2794 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2797 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2798 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 2799 | * is generated. |
| 2800 | */ |
| 2801 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 2802 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2803 | sp<FakeWindowHandle> window = |
| 2804 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2805 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 2806 | |
| 2807 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2808 | |
| 2809 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2810 | |
| 2811 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2812 | injectMotionEvent(mDispatcher, |
| 2813 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2814 | AINPUT_SOURCE_MOUSE) |
| 2815 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2816 | .x(300) |
| 2817 | .y(400)) |
| 2818 | .build())); |
| 2819 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2820 | |
| 2821 | // Remove the window, but keep the channel. |
| 2822 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 2823 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2824 | } |
| 2825 | |
| 2826 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2827 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2828 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 2829 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2830 | */ |
| 2831 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 2832 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2833 | sp<FakeWindowHandle> window = |
| 2834 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2835 | window->setFrame(Rect(0, 0, 100, 100)); |
| 2836 | |
| 2837 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2838 | |
| 2839 | // Inject a hover_move from mouse. |
| 2840 | NotifyMotionArgs motionArgs = |
| 2841 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 2842 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2843 | motionArgs.xCursorPosition = 50; |
| 2844 | motionArgs.yCursorPosition = 50; |
| 2845 | mDispatcher->notifyMotion(&motionArgs); |
| 2846 | ASSERT_NO_FATAL_FAILURE( |
| 2847 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2848 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2849 | |
| 2850 | // Tap on the window |
| 2851 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2852 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2853 | mDispatcher->notifyMotion(&motionArgs); |
| 2854 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2855 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2856 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 2857 | |
| 2858 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2859 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2860 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2861 | |
| 2862 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2863 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2864 | mDispatcher->notifyMotion(&motionArgs); |
| 2865 | ASSERT_NO_FATAL_FAILURE( |
| 2866 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2867 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2868 | } |
| 2869 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2870 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 2871 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2872 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 2873 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 2874 | ADISPLAY_ID_DEFAULT); |
| 2875 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2876 | sp<FakeWindowHandle> windowSecondDisplay = |
| 2877 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 2878 | SECOND_DISPLAY_ID); |
| 2879 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2880 | |
| 2881 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2882 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2883 | |
| 2884 | // Set cursor position in window in default display and check that hover enter and move |
| 2885 | // events are generated. |
| 2886 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2887 | injectMotionEvent(mDispatcher, |
| 2888 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2889 | AINPUT_SOURCE_MOUSE) |
| 2890 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2891 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2892 | .x(300) |
| 2893 | .y(600)) |
| 2894 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2895 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2896 | |
| 2897 | // Remove all windows in secondary display and check that no event happens on window in |
| 2898 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2899 | mDispatcher->setInputWindows( |
| 2900 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2901 | windowDefaultDisplay->assertNoEvents(); |
| 2902 | |
| 2903 | // Move cursor position in window in default display and check that only hover move |
| 2904 | // event is generated and not hover enter event. |
| 2905 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2906 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2907 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2908 | injectMotionEvent(mDispatcher, |
| 2909 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2910 | AINPUT_SOURCE_MOUSE) |
| 2911 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2912 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2913 | .x(400) |
| 2914 | .y(700)) |
| 2915 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2916 | windowDefaultDisplay->consumeMotionEvent( |
| 2917 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2918 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2919 | windowDefaultDisplay->assertNoEvents(); |
| 2920 | } |
| 2921 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2922 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2923 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2924 | |
| 2925 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2926 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2927 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2928 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2929 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2930 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2931 | |
| 2932 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2933 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2934 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2935 | |
| 2936 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 2937 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2938 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2939 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2940 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2941 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2942 | windowRight->assertNoEvents(); |
| 2943 | } |
| 2944 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2945 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2946 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2947 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2948 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2949 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2950 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2951 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2952 | setFocusedWindow(window); |
| 2953 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2954 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2955 | |
| 2956 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2957 | mDispatcher->notifyKey(&keyArgs); |
| 2958 | |
| 2959 | // Window should receive key down event. |
| 2960 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2961 | |
| 2962 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 2963 | // on the app side. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2964 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2965 | mDispatcher->notifyDeviceReset(&args); |
| 2966 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2967 | AKEY_EVENT_FLAG_CANCELED); |
| 2968 | } |
| 2969 | |
| 2970 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2971 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2972 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2973 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2974 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2975 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2976 | |
| 2977 | NotifyMotionArgs motionArgs = |
| 2978 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2979 | ADISPLAY_ID_DEFAULT); |
| 2980 | mDispatcher->notifyMotion(&motionArgs); |
| 2981 | |
| 2982 | // Window should receive motion down event. |
| 2983 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2984 | |
| 2985 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 2986 | // on the app side. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2987 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2988 | mDispatcher->notifyDeviceReset(&args); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 2989 | window->consumeMotionEvent( |
| 2990 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2991 | } |
| 2992 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2993 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 2994 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2995 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2996 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2997 | window->setFocusable(true); |
| 2998 | |
| 2999 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3000 | setFocusedWindow(window); |
| 3001 | |
| 3002 | window->consumeFocusEvent(true); |
| 3003 | |
| 3004 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3005 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3006 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3007 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 3008 | mDispatcher->notifyKey(&keyArgs); |
| 3009 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3010 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3011 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3012 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3013 | } |
| 3014 | |
| 3015 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3016 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3017 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3018 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3019 | window->setFocusable(true); |
| 3020 | |
| 3021 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3022 | setFocusedWindow(window); |
| 3023 | |
| 3024 | window->consumeFocusEvent(true); |
| 3025 | |
| 3026 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3027 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3028 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 3029 | mDispatcher->notifyKey(&keyDown); |
| 3030 | mDispatcher->notifyKey(&keyUp); |
| 3031 | |
| 3032 | // Window should receive key event immediately when same key up. |
| 3033 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3034 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3035 | } |
| 3036 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3037 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3038 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3039 | * WATCH_OUTSIDE_TOUCH. |
| 3040 | * Both windows are owned by the same UID. |
| 3041 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3042 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3043 | */ |
| 3044 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3045 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3046 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3047 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3048 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3049 | |
| 3050 | sp<FakeWindowHandle> outsideWindow = |
| 3051 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3052 | ADISPLAY_ID_DEFAULT); |
| 3053 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3054 | outsideWindow->setWatchOutsideTouch(true); |
| 3055 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3056 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3057 | |
| 3058 | // Tap on first window. |
| 3059 | NotifyMotionArgs motionArgs = |
| 3060 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3061 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 3062 | mDispatcher->notifyMotion(&motionArgs); |
| 3063 | window->consumeMotionDown(); |
| 3064 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3065 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3066 | outsideWindow->consumeMotionEvent( |
| 3067 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3068 | } |
| 3069 | |
| 3070 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3071 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3072 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3073 | * ACTION_OUTSIDE event is sent per gesture. |
| 3074 | */ |
| 3075 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3076 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 3077 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3078 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3079 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3080 | window->setWatchOutsideTouch(true); |
| 3081 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3082 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3083 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3084 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3085 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3086 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3087 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3088 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3089 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3090 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3091 | |
| 3092 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 3093 | NotifyMotionArgs motionArgs = |
| 3094 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3095 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 3096 | mDispatcher->notifyMotion(&motionArgs); |
| 3097 | window->assertNoEvents(); |
| 3098 | secondWindow->assertNoEvents(); |
| 3099 | |
| 3100 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3101 | // Now, `window` should get ACTION_OUTSIDE. |
| 3102 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3103 | {PointF{-10, -10}, PointF{105, 105}}); |
| 3104 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3105 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3106 | window->consumeMotionEvent( |
| 3107 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3108 | secondWindow->consumeMotionDown(); |
| 3109 | thirdWindow->assertNoEvents(); |
| 3110 | |
| 3111 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3112 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 3113 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3114 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 3115 | mDispatcher->notifyMotion(&motionArgs); |
| 3116 | window->assertNoEvents(); |
| 3117 | secondWindow->consumeMotionMove(); |
| 3118 | thirdWindow->consumeMotionDown(); |
| 3119 | } |
| 3120 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3121 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3122 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3123 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3124 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3125 | window->setFocusable(true); |
| 3126 | |
| 3127 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 3128 | setFocusedWindow(window); |
| 3129 | |
| 3130 | window->consumeFocusEvent(true); |
| 3131 | |
| 3132 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3133 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3134 | mDispatcher->notifyKey(&keyDown); |
| 3135 | mDispatcher->notifyKey(&keyUp); |
| 3136 | |
| 3137 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3138 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3139 | |
| 3140 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
| 3141 | mDispatcher->onWindowInfosChanged({}, {}); |
| 3142 | |
| 3143 | window->consumeFocusEvent(false); |
| 3144 | |
| 3145 | mDispatcher->notifyKey(&keyDown); |
| 3146 | mDispatcher->notifyKey(&keyUp); |
| 3147 | window->assertNoEvents(); |
| 3148 | } |
| 3149 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3150 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3151 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3152 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3153 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3154 | // Ensure window is non-split and have some transform. |
| 3155 | window->setPreventSplitting(true); |
| 3156 | window->setWindowOffset(20, 40); |
| 3157 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 3158 | |
| 3159 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3160 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3161 | {50, 50})) |
| 3162 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3163 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3164 | |
| 3165 | const MotionEvent secondFingerDownEvent = |
| 3166 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3167 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3168 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3169 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 3170 | .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] | 3171 | .build(); |
| 3172 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3173 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3174 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3175 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3176 | |
| 3177 | const MotionEvent* event = window->consumeMotion(); |
| 3178 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3179 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3180 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3181 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3182 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3183 | } |
| 3184 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3185 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3186 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 3187 | * |
| 3188 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 3189 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 3190 | * space. |
| 3191 | */ |
| 3192 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 3193 | public: |
| 3194 | void SetUp() override { |
| 3195 | InputDispatcherTest::SetUp(); |
| 3196 | mDisplayInfos.clear(); |
| 3197 | mWindowInfos.clear(); |
| 3198 | } |
| 3199 | |
| 3200 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 3201 | gui::DisplayInfo info; |
| 3202 | info.displayId = displayId; |
| 3203 | info.transform = transform; |
| 3204 | mDisplayInfos.push_back(std::move(info)); |
| 3205 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3206 | } |
| 3207 | |
| 3208 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 3209 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 3210 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3211 | } |
| 3212 | |
| 3213 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 3214 | // on the display. |
| 3215 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 3216 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 3217 | // respectively. |
| 3218 | ui::Transform displayTransform; |
| 3219 | displayTransform.set(2, 0, 0, 4); |
| 3220 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 3221 | |
| 3222 | std::shared_ptr<FakeApplicationHandle> application = |
| 3223 | std::make_shared<FakeApplicationHandle>(); |
| 3224 | |
| 3225 | // Add two windows to the display. Their frames are represented in the display space. |
| 3226 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3227 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3228 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3229 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 3230 | addWindow(firstWindow); |
| 3231 | |
| 3232 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3233 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3234 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3235 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 3236 | addWindow(secondWindow); |
| 3237 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 3238 | } |
| 3239 | |
| 3240 | private: |
| 3241 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 3242 | std::vector<gui::WindowInfo> mWindowInfos; |
| 3243 | }; |
| 3244 | |
| 3245 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 3246 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3247 | // Send down to the first window. The point is represented in the display space. The point is |
| 3248 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 3249 | // end up in the incorrect window. |
| 3250 | NotifyMotionArgs downMotionArgs = |
| 3251 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3252 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 3253 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3254 | |
| 3255 | firstWindow->consumeMotionDown(); |
| 3256 | secondWindow->assertNoEvents(); |
| 3257 | } |
| 3258 | |
| 3259 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 3260 | // the event should be treated as being in the logical display space. |
| 3261 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 3262 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3263 | // Send down to the first window. The point is represented in the logical display space. The |
| 3264 | // point is selected so that if the hit test was done in logical display space, then it would |
| 3265 | // end up in the incorrect window. |
| 3266 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3267 | PointF{75 * 2, 55 * 4}); |
| 3268 | |
| 3269 | firstWindow->consumeMotionDown(); |
| 3270 | secondWindow->assertNoEvents(); |
| 3271 | } |
| 3272 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3273 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 3274 | // event should be treated as being in the logical display space. |
| 3275 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 3276 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3277 | |
| 3278 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 3279 | ui::Transform injectedEventTransform; |
| 3280 | injectedEventTransform.set(matrix); |
| 3281 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 3282 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 3283 | |
| 3284 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3285 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3286 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3287 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3288 | .x(untransformedPoint.x) |
| 3289 | .y(untransformedPoint.y)) |
| 3290 | .build(); |
| 3291 | event.transform(matrix); |
| 3292 | |
| 3293 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 3294 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 3295 | |
| 3296 | firstWindow->consumeMotionDown(); |
| 3297 | secondWindow->assertNoEvents(); |
| 3298 | } |
| 3299 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3300 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 3301 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3302 | |
| 3303 | // Send down to the second window. |
| 3304 | NotifyMotionArgs downMotionArgs = |
| 3305 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3306 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 3307 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3308 | |
| 3309 | firstWindow->assertNoEvents(); |
| 3310 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 3311 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 3312 | |
| 3313 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 3314 | EXPECT_EQ(300, event->getRawX(0)); |
| 3315 | EXPECT_EQ(880, event->getRawY(0)); |
| 3316 | |
| 3317 | // Ensure that the x and y values are in the window's coordinate space. |
| 3318 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 3319 | // the logical display space. This will be the origin of the window space. |
| 3320 | EXPECT_EQ(100, event->getX(0)); |
| 3321 | EXPECT_EQ(80, event->getY(0)); |
| 3322 | } |
| 3323 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3324 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3325 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3326 | |
| 3327 | class TransferTouchFixture : public InputDispatcherTest, |
| 3328 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 3329 | |
| 3330 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3331 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3332 | |
| 3333 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3334 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3335 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3336 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3337 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3338 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3339 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3340 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3341 | sp<FakeWindowHandle> wallpaper = |
| 3342 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 3343 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3344 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3345 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3346 | |
| 3347 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3348 | NotifyMotionArgs downMotionArgs = |
| 3349 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3350 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3351 | mDispatcher->notifyMotion(&downMotionArgs); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3352 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3353 | // Only the first window should get the down event |
| 3354 | firstWindow->consumeMotionDown(); |
| 3355 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3356 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3357 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3358 | // Transfer touch to the second window |
| 3359 | TransferFunction f = GetParam(); |
| 3360 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3361 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3362 | // The first window gets cancel and the second gets down |
| 3363 | firstWindow->consumeMotionCancel(); |
| 3364 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3365 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3366 | |
| 3367 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3368 | NotifyMotionArgs upMotionArgs = |
| 3369 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3370 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3371 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3372 | // The first window gets no events and the second gets up |
| 3373 | firstWindow->assertNoEvents(); |
| 3374 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3375 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3376 | } |
| 3377 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3378 | /** |
| 3379 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 3380 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 3381 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 3382 | * natural to the user. |
| 3383 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 3384 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 3385 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 3386 | * the first window to the second. |
| 3387 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 3388 | * the other API, as well. |
| 3389 | */ |
| 3390 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 3391 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3392 | |
| 3393 | // Create a couple of windows + a spy window |
| 3394 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3395 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3396 | spyWindow->setTrustedOverlay(true); |
| 3397 | spyWindow->setSpy(true); |
| 3398 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3399 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3400 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3401 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3402 | |
| 3403 | // Add the windows to the dispatcher |
| 3404 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 3405 | |
| 3406 | // Send down to the first window |
| 3407 | NotifyMotionArgs downMotionArgs = |
| 3408 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3409 | ADISPLAY_ID_DEFAULT); |
| 3410 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3411 | // Only the first window and spy should get the down event |
| 3412 | spyWindow->consumeMotionDown(); |
| 3413 | firstWindow->consumeMotionDown(); |
| 3414 | |
| 3415 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 3416 | // if f === 'transferTouch'. |
| 3417 | TransferFunction f = GetParam(); |
| 3418 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3419 | ASSERT_TRUE(success); |
| 3420 | // The first window gets cancel and the second gets down |
| 3421 | firstWindow->consumeMotionCancel(); |
| 3422 | secondWindow->consumeMotionDown(); |
| 3423 | |
| 3424 | // Send up event to the second window |
| 3425 | NotifyMotionArgs upMotionArgs = |
| 3426 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3427 | ADISPLAY_ID_DEFAULT); |
| 3428 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3429 | // The first window gets no events and the second+spy get up |
| 3430 | firstWindow->assertNoEvents(); |
| 3431 | spyWindow->consumeMotionUp(); |
| 3432 | secondWindow->consumeMotionUp(); |
| 3433 | } |
| 3434 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3435 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3436 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3437 | |
| 3438 | PointF touchPoint = {10, 10}; |
| 3439 | |
| 3440 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3441 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3442 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3443 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3444 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3445 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3446 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3447 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3448 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3449 | |
| 3450 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3452 | |
| 3453 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3454 | NotifyMotionArgs downMotionArgs = |
| 3455 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3456 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3457 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3458 | // Only the first window should get the down event |
| 3459 | firstWindow->consumeMotionDown(); |
| 3460 | secondWindow->assertNoEvents(); |
| 3461 | |
| 3462 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3463 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3464 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3465 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3466 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 3467 | // Only the first window should get the pointer down event |
| 3468 | firstWindow->consumeMotionPointerDown(1); |
| 3469 | secondWindow->assertNoEvents(); |
| 3470 | |
| 3471 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3472 | TransferFunction f = GetParam(); |
| 3473 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3474 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3475 | // The first window gets cancel and the second gets down and pointer down |
| 3476 | firstWindow->consumeMotionCancel(); |
| 3477 | secondWindow->consumeMotionDown(); |
| 3478 | secondWindow->consumeMotionPointerDown(1); |
| 3479 | |
| 3480 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3481 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3482 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3483 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3484 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3485 | // The first window gets nothing and the second gets pointer up |
| 3486 | firstWindow->assertNoEvents(); |
| 3487 | secondWindow->consumeMotionPointerUp(1); |
| 3488 | |
| 3489 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3490 | NotifyMotionArgs upMotionArgs = |
| 3491 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3492 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3493 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3494 | // The first window gets nothing and the second gets up |
| 3495 | firstWindow->assertNoEvents(); |
| 3496 | secondWindow->consumeMotionUp(); |
| 3497 | } |
| 3498 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3499 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 3500 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3501 | |
| 3502 | // Create a couple of windows |
| 3503 | sp<FakeWindowHandle> firstWindow = |
| 3504 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3505 | ADISPLAY_ID_DEFAULT); |
| 3506 | firstWindow->setDupTouchToWallpaper(true); |
| 3507 | sp<FakeWindowHandle> secondWindow = |
| 3508 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3509 | ADISPLAY_ID_DEFAULT); |
| 3510 | secondWindow->setDupTouchToWallpaper(true); |
| 3511 | |
| 3512 | sp<FakeWindowHandle> wallpaper1 = |
| 3513 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 3514 | wallpaper1->setIsWallpaper(true); |
| 3515 | |
| 3516 | sp<FakeWindowHandle> wallpaper2 = |
| 3517 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 3518 | wallpaper2->setIsWallpaper(true); |
| 3519 | // Add the windows to the dispatcher |
| 3520 | mDispatcher->setInputWindows( |
| 3521 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 3522 | |
| 3523 | // Send down to the first window |
| 3524 | NotifyMotionArgs downMotionArgs = |
| 3525 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3526 | ADISPLAY_ID_DEFAULT); |
| 3527 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3528 | |
| 3529 | // Only the first window should get the down event |
| 3530 | firstWindow->consumeMotionDown(); |
| 3531 | secondWindow->assertNoEvents(); |
| 3532 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3533 | wallpaper2->assertNoEvents(); |
| 3534 | |
| 3535 | // Transfer touch focus to the second window |
| 3536 | TransferFunction f = GetParam(); |
| 3537 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3538 | ASSERT_TRUE(success); |
| 3539 | |
| 3540 | // The first window gets cancel and the second gets down |
| 3541 | firstWindow->consumeMotionCancel(); |
| 3542 | secondWindow->consumeMotionDown(); |
| 3543 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3544 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3545 | |
| 3546 | // Send up event to the second window |
| 3547 | NotifyMotionArgs upMotionArgs = |
| 3548 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3549 | ADISPLAY_ID_DEFAULT); |
| 3550 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3551 | // The first window gets no events and the second gets up |
| 3552 | firstWindow->assertNoEvents(); |
| 3553 | secondWindow->consumeMotionUp(); |
| 3554 | wallpaper1->assertNoEvents(); |
| 3555 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3556 | } |
| 3557 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3558 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 3559 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 3560 | // for the case where there are multiple pointers split across several windows. |
| 3561 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 3562 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3563 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3564 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3565 | return dispatcher->transferTouch(destChannelToken, |
| 3566 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3567 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3568 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3569 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3570 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3571 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3572 | })); |
| 3573 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3574 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3575 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3576 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3577 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3578 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3579 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3580 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3581 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3582 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3583 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3584 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3585 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3586 | |
| 3587 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3588 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3589 | |
| 3590 | PointF pointInFirst = {300, 200}; |
| 3591 | PointF pointInSecond = {300, 600}; |
| 3592 | |
| 3593 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3594 | NotifyMotionArgs firstDownMotionArgs = |
| 3595 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3596 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3597 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3598 | // Only the first window should get the down event |
| 3599 | firstWindow->consumeMotionDown(); |
| 3600 | secondWindow->assertNoEvents(); |
| 3601 | |
| 3602 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3603 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3604 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3605 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3606 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3607 | // The first window gets a move and the second a down |
| 3608 | firstWindow->consumeMotionMove(); |
| 3609 | secondWindow->consumeMotionDown(); |
| 3610 | |
| 3611 | // Transfer touch focus to the second window |
| 3612 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 3613 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 3614 | firstWindow->consumeMotionCancel(); |
| 3615 | secondWindow->consumeMotionPointerDown(1); |
| 3616 | |
| 3617 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3618 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3619 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3620 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3621 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3622 | // The first window gets nothing and the second gets pointer up |
| 3623 | firstWindow->assertNoEvents(); |
| 3624 | secondWindow->consumeMotionPointerUp(1); |
| 3625 | |
| 3626 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3627 | NotifyMotionArgs upMotionArgs = |
| 3628 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3629 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3630 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3631 | // The first window gets nothing and the second gets up |
| 3632 | firstWindow->assertNoEvents(); |
| 3633 | secondWindow->consumeMotionUp(); |
| 3634 | } |
| 3635 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3636 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 3637 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 3638 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 3639 | // window should get nothing. |
| 3640 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 3641 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3642 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3643 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3644 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3645 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3646 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3647 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3648 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3649 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3650 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3651 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3652 | |
| 3653 | // Add the windows to the dispatcher |
| 3654 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3655 | |
| 3656 | PointF pointInFirst = {300, 200}; |
| 3657 | PointF pointInSecond = {300, 600}; |
| 3658 | |
| 3659 | // Send down to the first window |
| 3660 | NotifyMotionArgs firstDownMotionArgs = |
| 3661 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3662 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3663 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3664 | // Only the first window should get the down event |
| 3665 | firstWindow->consumeMotionDown(); |
| 3666 | secondWindow->assertNoEvents(); |
| 3667 | |
| 3668 | // Send down to the second window |
| 3669 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3670 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3671 | {pointInFirst, pointInSecond}); |
| 3672 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3673 | // The first window gets a move and the second a down |
| 3674 | firstWindow->consumeMotionMove(); |
| 3675 | secondWindow->consumeMotionDown(); |
| 3676 | |
| 3677 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3678 | const bool transferred = |
| 3679 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3680 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 3681 | ASSERT_FALSE(transferred); |
| 3682 | firstWindow->assertNoEvents(); |
| 3683 | secondWindow->assertNoEvents(); |
| 3684 | |
| 3685 | // The rest of the dispatch should proceed as normal |
| 3686 | // Send pointer up to the second window |
| 3687 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3688 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3689 | {pointInFirst, pointInSecond}); |
| 3690 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3691 | // The first window gets MOVE and the second gets pointer up |
| 3692 | firstWindow->consumeMotionMove(); |
| 3693 | secondWindow->consumeMotionUp(); |
| 3694 | |
| 3695 | // Send up event to the first window |
| 3696 | NotifyMotionArgs upMotionArgs = |
| 3697 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3698 | ADISPLAY_ID_DEFAULT); |
| 3699 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3700 | // The first window gets nothing and the second gets up |
| 3701 | firstWindow->consumeMotionUp(); |
| 3702 | secondWindow->assertNoEvents(); |
| 3703 | } |
| 3704 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3705 | // This case will create two windows and one mirrored window on the default display and mirror |
| 3706 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 3707 | // the windows info of second display before default display. |
| 3708 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 3709 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3710 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3711 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3712 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3713 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3714 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3715 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3716 | |
| 3717 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3718 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3719 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3720 | |
| 3721 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3722 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3723 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3724 | |
| 3725 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3726 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3727 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3728 | |
| 3729 | // Update window info, let it find window handle of second display first. |
| 3730 | mDispatcher->setInputWindows( |
| 3731 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3732 | {ADISPLAY_ID_DEFAULT, |
| 3733 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3734 | |
| 3735 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3736 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3737 | {50, 50})) |
| 3738 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3739 | |
| 3740 | // Window should receive motion event. |
| 3741 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3742 | |
| 3743 | // Transfer touch focus |
| 3744 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 3745 | secondWindowInPrimary->getToken())); |
| 3746 | // The first window gets cancel. |
| 3747 | firstWindowInPrimary->consumeMotionCancel(); |
| 3748 | secondWindowInPrimary->consumeMotionDown(); |
| 3749 | |
| 3750 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3751 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3752 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 3753 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3754 | firstWindowInPrimary->assertNoEvents(); |
| 3755 | secondWindowInPrimary->consumeMotionMove(); |
| 3756 | |
| 3757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3758 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3759 | {150, 50})) |
| 3760 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3761 | firstWindowInPrimary->assertNoEvents(); |
| 3762 | secondWindowInPrimary->consumeMotionUp(); |
| 3763 | } |
| 3764 | |
| 3765 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 3766 | // 'transferTouch' api. |
| 3767 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 3768 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3769 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3770 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3771 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3772 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3773 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3774 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3775 | |
| 3776 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3777 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3778 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3779 | |
| 3780 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3781 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3782 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3783 | |
| 3784 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3785 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3786 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3787 | |
| 3788 | // Update window info, let it find window handle of second display first. |
| 3789 | mDispatcher->setInputWindows( |
| 3790 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3791 | {ADISPLAY_ID_DEFAULT, |
| 3792 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3793 | |
| 3794 | // Touch on second display. |
| 3795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3796 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 3797 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3798 | |
| 3799 | // Window should receive motion event. |
| 3800 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3801 | |
| 3802 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3803 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3804 | |
| 3805 | // The first window gets cancel. |
| 3806 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3807 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3808 | |
| 3809 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3810 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3811 | SECOND_DISPLAY_ID, {150, 50})) |
| 3812 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3813 | firstWindowInPrimary->assertNoEvents(); |
| 3814 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 3815 | |
| 3816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3817 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 3818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3819 | firstWindowInPrimary->assertNoEvents(); |
| 3820 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 3821 | } |
| 3822 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3823 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3824 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3825 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3826 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3827 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3828 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3829 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3830 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3831 | |
| 3832 | window->consumeFocusEvent(true); |
| 3833 | |
| 3834 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3835 | mDispatcher->notifyKey(&keyArgs); |
| 3836 | |
| 3837 | // Window should receive key down event. |
| 3838 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3839 | } |
| 3840 | |
| 3841 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3842 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3843 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3844 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3845 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3846 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3847 | |
| 3848 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3849 | mDispatcher->notifyKey(&keyArgs); |
| 3850 | mDispatcher->waitForIdle(); |
| 3851 | |
| 3852 | window->assertNoEvents(); |
| 3853 | } |
| 3854 | |
| 3855 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 3856 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3857 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3858 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3859 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3860 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3861 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3862 | |
| 3863 | // Send key |
| 3864 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3865 | mDispatcher->notifyKey(&keyArgs); |
| 3866 | // Send motion |
| 3867 | NotifyMotionArgs motionArgs = |
| 3868 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3869 | ADISPLAY_ID_DEFAULT); |
| 3870 | mDispatcher->notifyMotion(&motionArgs); |
| 3871 | |
| 3872 | // Window should receive only the motion event |
| 3873 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3874 | window->assertNoEvents(); // Key event or focus event will not be received |
| 3875 | } |
| 3876 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3877 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 3878 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3879 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3880 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3881 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3882 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3883 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3884 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3885 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3886 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3887 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3888 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3889 | |
| 3890 | // Add the windows to the dispatcher |
| 3891 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3892 | |
| 3893 | PointF pointInFirst = {300, 200}; |
| 3894 | PointF pointInSecond = {300, 600}; |
| 3895 | |
| 3896 | // Send down to the first window |
| 3897 | NotifyMotionArgs firstDownMotionArgs = |
| 3898 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3899 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3900 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3901 | // Only the first window should get the down event |
| 3902 | firstWindow->consumeMotionDown(); |
| 3903 | secondWindow->assertNoEvents(); |
| 3904 | |
| 3905 | // Send down to the second window |
| 3906 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3907 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3908 | {pointInFirst, pointInSecond}); |
| 3909 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3910 | // The first window gets a move and the second a down |
| 3911 | firstWindow->consumeMotionMove(); |
| 3912 | secondWindow->consumeMotionDown(); |
| 3913 | |
| 3914 | // Send pointer cancel to the second window |
| 3915 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3916 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3917 | {pointInFirst, pointInSecond}); |
| 3918 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3919 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3920 | // The first window gets move and the second gets cancel. |
| 3921 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3922 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3923 | |
| 3924 | // Send up event. |
| 3925 | NotifyMotionArgs upMotionArgs = |
| 3926 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3927 | ADISPLAY_ID_DEFAULT); |
| 3928 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3929 | // The first window gets up and the second gets nothing. |
| 3930 | firstWindow->consumeMotionUp(); |
| 3931 | secondWindow->assertNoEvents(); |
| 3932 | } |
| 3933 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3934 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 3935 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3936 | |
| 3937 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3938 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3939 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3940 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 3941 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 3942 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 3943 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3944 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3945 | window->assertNoEvents(); |
| 3946 | mDispatcher->waitForIdle(); |
| 3947 | } |
| 3948 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3949 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3950 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3951 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3952 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3953 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3954 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3955 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3958 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 3959 | |
| 3960 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3961 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 3962 | expectedDisplayId, expectedFlags); |
| 3963 | } |
| 3964 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3965 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 3966 | |
| 3967 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 3968 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3969 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3970 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 3971 | expectedDisplayId, expectedFlags); |
| 3972 | } |
| 3973 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3974 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3975 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 3976 | expectedDisplayId, expectedFlags); |
| 3977 | } |
| 3978 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3979 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3980 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 3981 | expectedDisplayId, expectedFlags); |
| 3982 | } |
| 3983 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3984 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3985 | mInputReceiver->consumeMotionEvent( |
| 3986 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3987 | WithDisplayId(expectedDisplayId), |
| 3988 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3991 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 3992 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 3993 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3994 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3995 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3998 | MotionEvent* consumeMotion() { |
| 3999 | InputEvent* event = mInputReceiver->consume(); |
| 4000 | if (!event) { |
| 4001 | ADD_FAILURE() << "No event was produced"; |
| 4002 | return nullptr; |
| 4003 | } |
| 4004 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 4005 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 4006 | return nullptr; |
| 4007 | } |
| 4008 | return static_cast<MotionEvent*>(event); |
| 4009 | } |
| 4010 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4011 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 4012 | |
| 4013 | private: |
| 4014 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4015 | }; |
| 4016 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4017 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 4018 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4019 | /** |
| 4020 | * Two entities that receive touch: A window, and a global monitor. |
| 4021 | * The touch goes to the window, and then the window disappears. |
| 4022 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 4023 | * for the monitor, as well. |
| 4024 | * 1. foregroundWindow |
| 4025 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 4026 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4027 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4028 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4029 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4030 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4031 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4032 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4033 | |
| 4034 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4035 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4036 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4037 | {100, 200})) |
| 4038 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4039 | |
| 4040 | // Both the foreground window and the global monitor should receive the touch down |
| 4041 | window->consumeMotionDown(); |
| 4042 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4043 | |
| 4044 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4045 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4046 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4047 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4048 | |
| 4049 | window->consumeMotionMove(); |
| 4050 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4051 | |
| 4052 | // Now the foreground window goes away |
| 4053 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4054 | window->consumeMotionCancel(); |
| 4055 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 4056 | |
| 4057 | // If more events come in, there will be no more foreground window to send them to. This will |
| 4058 | // cause a cancel for the monitor, as well. |
| 4059 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4060 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4061 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 4062 | << "Injection should fail because the window was removed"; |
| 4063 | window->assertNoEvents(); |
| 4064 | // Global monitor now gets the cancel |
| 4065 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4066 | } |
| 4067 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4068 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4069 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4070 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4071 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4072 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4073 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4074 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4075 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4076 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4077 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4078 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4079 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4080 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4083 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 4084 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4085 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4086 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4087 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4088 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4090 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4091 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4092 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4093 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4094 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4095 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4096 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4097 | // Pilfer pointers from the monitor. |
| 4098 | // This should not do anything and the window should continue to receive events. |
| 4099 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4100 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4102 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4103 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4104 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4105 | |
| 4106 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4107 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4110 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4111 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4112 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4113 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4114 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4115 | window->setWindowOffset(20, 40); |
| 4116 | window->setWindowTransform(0, 1, -1, 0); |
| 4117 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4118 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4119 | |
| 4120 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4121 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4122 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4123 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4124 | MotionEvent* event = monitor.consumeMotion(); |
| 4125 | // Even though window has transform, gesture monitor must not. |
| 4126 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 4127 | } |
| 4128 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4129 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4130 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4131 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4132 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4133 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4134 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4135 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 4136 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4139 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4140 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4141 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4142 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4143 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4144 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4145 | |
| 4146 | NotifyMotionArgs motionArgs = |
| 4147 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4148 | ADISPLAY_ID_DEFAULT); |
| 4149 | |
| 4150 | mDispatcher->notifyMotion(&motionArgs); |
| 4151 | // Window should receive motion down event. |
| 4152 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4153 | |
| 4154 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4155 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4156 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4157 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4158 | motionArgs.pointerCoords[0].getX() - 10); |
| 4159 | |
| 4160 | mDispatcher->notifyMotion(&motionArgs); |
| 4161 | 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] | 4162 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4163 | } |
| 4164 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4165 | /** |
| 4166 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 4167 | * the device default right away. In the test scenario, we check both the default value, |
| 4168 | * and the action of enabling / disabling. |
| 4169 | */ |
| 4170 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4171 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4172 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4173 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4174 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4175 | |
| 4176 | // Set focused application. |
| 4177 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4178 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4179 | |
| 4180 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4181 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4182 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4183 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4184 | |
| 4185 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4186 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4187 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4188 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4189 | |
| 4190 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4191 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4192 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4193 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4194 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4195 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4196 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4197 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4198 | |
| 4199 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4200 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4201 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4202 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4203 | |
| 4204 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4205 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4206 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4207 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4208 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4209 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4210 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4211 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4212 | |
| 4213 | window->assertNoEvents(); |
| 4214 | } |
| 4215 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4216 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4217 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4218 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4219 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4220 | |
| 4221 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4222 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4223 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4224 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4225 | setFocusedWindow(window); |
| 4226 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4227 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4228 | |
| 4229 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4230 | mDispatcher->notifyKey(&keyArgs); |
| 4231 | |
| 4232 | InputEvent* event = window->consume(); |
| 4233 | ASSERT_NE(event, nullptr); |
| 4234 | |
| 4235 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4236 | ASSERT_NE(verified, nullptr); |
| 4237 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 4238 | |
| 4239 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 4240 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 4241 | ASSERT_EQ(keyArgs.source, verified->source); |
| 4242 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 4243 | |
| 4244 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 4245 | |
| 4246 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4247 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4248 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4249 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 4250 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 4251 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 4252 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 4253 | } |
| 4254 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4255 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4256 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4257 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4258 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4259 | |
| 4260 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4261 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4262 | ui::Transform transform; |
| 4263 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4264 | |
| 4265 | gui::DisplayInfo displayInfo; |
| 4266 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 4267 | displayInfo.transform = transform; |
| 4268 | |
| 4269 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4270 | |
| 4271 | NotifyMotionArgs motionArgs = |
| 4272 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4273 | ADISPLAY_ID_DEFAULT); |
| 4274 | mDispatcher->notifyMotion(&motionArgs); |
| 4275 | |
| 4276 | InputEvent* event = window->consume(); |
| 4277 | ASSERT_NE(event, nullptr); |
| 4278 | |
| 4279 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4280 | ASSERT_NE(verified, nullptr); |
| 4281 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 4282 | |
| 4283 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 4284 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 4285 | EXPECT_EQ(motionArgs.source, verified->source); |
| 4286 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 4287 | |
| 4288 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 4289 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4290 | const vec2 rawXY = |
| 4291 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 4292 | motionArgs.pointerCoords[0].getXYValue()); |
| 4293 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 4294 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4295 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4296 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4297 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4298 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 4299 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 4300 | } |
| 4301 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4302 | /** |
| 4303 | * Ensure that separate calls to sign the same data are generating the same key. |
| 4304 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 4305 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 4306 | * tests. |
| 4307 | */ |
| 4308 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 4309 | KeyEvent event = getTestKeyEvent(); |
| 4310 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4311 | |
| 4312 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 4313 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 4314 | ASSERT_EQ(hmac1, hmac2); |
| 4315 | } |
| 4316 | |
| 4317 | /** |
| 4318 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 4319 | */ |
| 4320 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 4321 | KeyEvent event = getTestKeyEvent(); |
| 4322 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4323 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 4324 | |
| 4325 | verifiedEvent.deviceId += 1; |
| 4326 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4327 | |
| 4328 | verifiedEvent.source += 1; |
| 4329 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4330 | |
| 4331 | verifiedEvent.eventTimeNanos += 1; |
| 4332 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4333 | |
| 4334 | verifiedEvent.displayId += 1; |
| 4335 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4336 | |
| 4337 | verifiedEvent.action += 1; |
| 4338 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4339 | |
| 4340 | verifiedEvent.downTimeNanos += 1; |
| 4341 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4342 | |
| 4343 | verifiedEvent.flags += 1; |
| 4344 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4345 | |
| 4346 | verifiedEvent.keyCode += 1; |
| 4347 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4348 | |
| 4349 | verifiedEvent.scanCode += 1; |
| 4350 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4351 | |
| 4352 | verifiedEvent.metaState += 1; |
| 4353 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4354 | |
| 4355 | verifiedEvent.repeatCount += 1; |
| 4356 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4357 | } |
| 4358 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4359 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 4360 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4361 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4362 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4363 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4364 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4365 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4366 | |
| 4367 | // Top window is also focusable but is not granted focus. |
| 4368 | windowTop->setFocusable(true); |
| 4369 | windowSecond->setFocusable(true); |
| 4370 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4371 | setFocusedWindow(windowSecond); |
| 4372 | |
| 4373 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4375 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4376 | |
| 4377 | // Focused window should receive event. |
| 4378 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4379 | windowTop->assertNoEvents(); |
| 4380 | } |
| 4381 | |
| 4382 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 4383 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4384 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4385 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4386 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4387 | |
| 4388 | window->setFocusable(true); |
| 4389 | // Release channel for window is no longer valid. |
| 4390 | window->releaseChannel(); |
| 4391 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4392 | setFocusedWindow(window); |
| 4393 | |
| 4394 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4395 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4396 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4397 | |
| 4398 | // window channel is invalid, so it should not receive any input event. |
| 4399 | window->assertNoEvents(); |
| 4400 | } |
| 4401 | |
| 4402 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 4403 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4404 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4405 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4406 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4407 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4408 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4409 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4410 | setFocusedWindow(window); |
| 4411 | |
| 4412 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4413 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4414 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4415 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4416 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4417 | window->assertNoEvents(); |
| 4418 | } |
| 4419 | |
| 4420 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 4421 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4422 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4423 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4424 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4425 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4426 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4427 | |
| 4428 | windowTop->setFocusable(true); |
| 4429 | windowSecond->setFocusable(true); |
| 4430 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4431 | setFocusedWindow(windowTop); |
| 4432 | windowTop->consumeFocusEvent(true); |
| 4433 | |
| 4434 | setFocusedWindow(windowSecond, windowTop); |
| 4435 | windowSecond->consumeFocusEvent(true); |
| 4436 | windowTop->consumeFocusEvent(false); |
| 4437 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4438 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4439 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4440 | |
| 4441 | // Focused window should receive event. |
| 4442 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4443 | } |
| 4444 | |
| 4445 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 4446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4447 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4448 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4449 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4450 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4451 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4452 | |
| 4453 | windowTop->setFocusable(true); |
| 4454 | windowSecond->setFocusable(true); |
| 4455 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4456 | setFocusedWindow(windowSecond, windowTop); |
| 4457 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4458 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4459 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4460 | |
| 4461 | // Event should be dropped. |
| 4462 | windowTop->assertNoEvents(); |
| 4463 | windowSecond->assertNoEvents(); |
| 4464 | } |
| 4465 | |
| 4466 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 4467 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4468 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4469 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4470 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4471 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 4472 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4473 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4474 | |
| 4475 | window->setFocusable(true); |
| 4476 | previousFocusedWindow->setFocusable(true); |
| 4477 | window->setVisible(false); |
| 4478 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 4479 | setFocusedWindow(previousFocusedWindow); |
| 4480 | previousFocusedWindow->consumeFocusEvent(true); |
| 4481 | |
| 4482 | // Requesting focus on invisible window takes focus from currently focused window. |
| 4483 | setFocusedWindow(window); |
| 4484 | previousFocusedWindow->consumeFocusEvent(false); |
| 4485 | |
| 4486 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4488 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 4489 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4490 | |
| 4491 | // Window does not get focus event or key down. |
| 4492 | window->assertNoEvents(); |
| 4493 | |
| 4494 | // Window becomes visible. |
| 4495 | window->setVisible(true); |
| 4496 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4497 | |
| 4498 | // Window receives focus event. |
| 4499 | window->consumeFocusEvent(true); |
| 4500 | // Focused window receives key down. |
| 4501 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4502 | } |
| 4503 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4504 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 4505 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4506 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4507 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4508 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4509 | |
| 4510 | // window is granted focus. |
| 4511 | window->setFocusable(true); |
| 4512 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4513 | setFocusedWindow(window); |
| 4514 | window->consumeFocusEvent(true); |
| 4515 | |
| 4516 | // When a display is removed window loses focus. |
| 4517 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 4518 | window->consumeFocusEvent(false); |
| 4519 | } |
| 4520 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4521 | /** |
| 4522 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 4523 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 4524 | * of the 'slipperyEnterWindow'. |
| 4525 | * |
| 4526 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 4527 | * a way so that the touched location is no longer covered by the top window. |
| 4528 | * |
| 4529 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 4530 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 4531 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 4532 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 4533 | * with ACTION_DOWN). |
| 4534 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 4535 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 4536 | * |
| 4537 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 4538 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 4539 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 4540 | * |
| 4541 | * In this test, we ensure that the event received by the bottom window has |
| 4542 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 4543 | */ |
| 4544 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4545 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 4546 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4547 | |
| 4548 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4549 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4550 | |
| 4551 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4552 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4553 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4554 | // Make sure this one overlaps the bottom window |
| 4555 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 4556 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 4557 | // one. Windows with the same owner are not considered to be occluding each other. |
| 4558 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 4559 | |
| 4560 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4561 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4562 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4563 | |
| 4564 | mDispatcher->setInputWindows( |
| 4565 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4566 | |
| 4567 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 4568 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4569 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 4570 | mDispatcher->notifyMotion(&args); |
| 4571 | slipperyExitWindow->consumeMotionDown(); |
| 4572 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 4573 | mDispatcher->setInputWindows( |
| 4574 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4575 | |
| 4576 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4577 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 4578 | mDispatcher->notifyMotion(&args); |
| 4579 | |
| 4580 | slipperyExitWindow->consumeMotionCancel(); |
| 4581 | |
| 4582 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 4583 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 4584 | } |
| 4585 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4586 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 4587 | protected: |
| 4588 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 4589 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 4590 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4591 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4592 | sp<FakeWindowHandle> mWindow; |
| 4593 | |
| 4594 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4595 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4596 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4597 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4598 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 4599 | ASSERT_EQ(OK, mDispatcher->start()); |
| 4600 | |
| 4601 | setUpWindow(); |
| 4602 | } |
| 4603 | |
| 4604 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4605 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4606 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4607 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4608 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4609 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4610 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4611 | mWindow->consumeFocusEvent(true); |
| 4612 | } |
| 4613 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4614 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4615 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4616 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4617 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 4618 | mDispatcher->notifyKey(&keyArgs); |
| 4619 | |
| 4620 | // Window should receive key down event. |
| 4621 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4622 | } |
| 4623 | |
| 4624 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 4625 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 4626 | InputEvent* repeatEvent = mWindow->consume(); |
| 4627 | ASSERT_NE(nullptr, repeatEvent); |
| 4628 | |
| 4629 | uint32_t eventType = repeatEvent->getType(); |
| 4630 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 4631 | |
| 4632 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 4633 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 4634 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 4635 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 4636 | } |
| 4637 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4638 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4639 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4640 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4641 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 4642 | mDispatcher->notifyKey(&keyArgs); |
| 4643 | |
| 4644 | // Window should receive key down event. |
| 4645 | 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] | 4646 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4647 | } |
| 4648 | }; |
| 4649 | |
| 4650 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4651 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4652 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4653 | expectKeyRepeatOnce(repeatCount); |
| 4654 | } |
| 4655 | } |
| 4656 | |
| 4657 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4658 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4659 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4660 | expectKeyRepeatOnce(repeatCount); |
| 4661 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4662 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4663 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4664 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4665 | expectKeyRepeatOnce(repeatCount); |
| 4666 | } |
| 4667 | } |
| 4668 | |
| 4669 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4670 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 4671 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 4672 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4673 | mWindow->assertNoEvents(); |
| 4674 | } |
| 4675 | |
| 4676 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4677 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 4678 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 4679 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 4680 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4681 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4682 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4683 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4684 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 4685 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4686 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4687 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4688 | mWindow->assertNoEvents(); |
| 4689 | } |
| 4690 | |
| 4691 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4692 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 4693 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 4694 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 4695 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4696 | // 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] | 4697 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4698 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4699 | mWindow->assertNoEvents(); |
| 4700 | } |
| 4701 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4702 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 4703 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4704 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 4705 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4706 | mDispatcher->notifyDeviceReset(&args); |
| 4707 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 4708 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 4709 | mWindow->assertNoEvents(); |
| 4710 | } |
| 4711 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4712 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4713 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4714 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4715 | InputEvent* repeatEvent = mWindow->consume(); |
| 4716 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4717 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 4718 | IdGenerator::getSource(repeatEvent->getId())); |
| 4719 | } |
| 4720 | } |
| 4721 | |
| 4722 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4723 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4724 | |
| 4725 | std::unordered_set<int32_t> idSet; |
| 4726 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4727 | InputEvent* repeatEvent = mWindow->consume(); |
| 4728 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4729 | int32_t id = repeatEvent->getId(); |
| 4730 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 4731 | idSet.insert(id); |
| 4732 | } |
| 4733 | } |
| 4734 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4735 | /* Test InputDispatcher for MultiDisplay */ |
| 4736 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 4737 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4738 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4739 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4740 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4741 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4742 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4743 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4744 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4745 | // Set focus window for primary display, but focused display would be second one. |
| 4746 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4747 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4748 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4749 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4750 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4751 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4752 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4753 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4754 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4755 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4756 | // Set focus display to second one. |
| 4757 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 4758 | // Set focus window for second display. |
| 4759 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4760 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4761 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4762 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4763 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4764 | } |
| 4765 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4766 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4767 | InputDispatcherTest::TearDown(); |
| 4768 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4769 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4770 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4771 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4772 | windowInSecondary.clear(); |
| 4773 | } |
| 4774 | |
| 4775 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4776 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4777 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4778 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4779 | sp<FakeWindowHandle> windowInSecondary; |
| 4780 | }; |
| 4781 | |
| 4782 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 4783 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4784 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4785 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4786 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4787 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4788 | windowInSecondary->assertNoEvents(); |
| 4789 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4790 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4791 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4792 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4793 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4794 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4795 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4796 | } |
| 4797 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4798 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4799 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4800 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4801 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4802 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4803 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4804 | windowInSecondary->assertNoEvents(); |
| 4805 | |
| 4806 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4807 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4808 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4809 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4810 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4811 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4812 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4813 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4814 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4815 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4816 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 4817 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4818 | |
| 4819 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4820 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4821 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4822 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4823 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4824 | windowInSecondary->assertNoEvents(); |
| 4825 | } |
| 4826 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4827 | // Test per-display input monitors for motion event. |
| 4828 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4829 | FakeMonitorReceiver monitorInPrimary = |
| 4830 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4831 | FakeMonitorReceiver monitorInSecondary = |
| 4832 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4833 | |
| 4834 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4835 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4836 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4837 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4838 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4839 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4840 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4841 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4842 | |
| 4843 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4845 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4846 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4847 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4848 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4849 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4850 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4851 | |
| 4852 | // Test inject a non-pointer motion event. |
| 4853 | // If specific a display, it will dispatch to the focused window of particular display, |
| 4854 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4855 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4856 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 4857 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4858 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4859 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4860 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4861 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | // Test per-display input monitors for key event. |
| 4865 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4866 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4867 | FakeMonitorReceiver monitorInPrimary = |
| 4868 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4869 | FakeMonitorReceiver monitorInSecondary = |
| 4870 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4871 | |
| 4872 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4874 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4875 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4876 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4877 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4878 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4879 | } |
| 4880 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4881 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 4882 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4883 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4884 | secondWindowInPrimary->setFocusable(true); |
| 4885 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 4886 | setFocusedWindow(secondWindowInPrimary); |
| 4887 | windowInPrimary->consumeFocusEvent(false); |
| 4888 | secondWindowInPrimary->consumeFocusEvent(true); |
| 4889 | |
| 4890 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 4892 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4893 | windowInPrimary->assertNoEvents(); |
| 4894 | windowInSecondary->assertNoEvents(); |
| 4895 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4896 | } |
| 4897 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 4898 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 4899 | FakeMonitorReceiver monitorInPrimary = |
| 4900 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4901 | FakeMonitorReceiver monitorInSecondary = |
| 4902 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 4903 | |
| 4904 | // Test touch down on primary display. |
| 4905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4906 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4907 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4908 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4909 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4910 | |
| 4911 | // Test touch down on second display. |
| 4912 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4913 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4914 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4915 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4916 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 4917 | |
| 4918 | // Trigger cancel touch. |
| 4919 | mDispatcher->cancelCurrentTouch(); |
| 4920 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4921 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4922 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4923 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4924 | |
| 4925 | // Test inject a move motion event, no window/monitor should receive the event. |
| 4926 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4927 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4928 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4929 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4930 | windowInPrimary->assertNoEvents(); |
| 4931 | monitorInPrimary.assertNoEvents(); |
| 4932 | |
| 4933 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4934 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4935 | SECOND_DISPLAY_ID, {110, 200})) |
| 4936 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4937 | windowInSecondary->assertNoEvents(); |
| 4938 | monitorInSecondary.assertNoEvents(); |
| 4939 | } |
| 4940 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4941 | class InputFilterTest : public InputDispatcherTest { |
| 4942 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4943 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 4944 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4945 | NotifyMotionArgs motionArgs; |
| 4946 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4947 | motionArgs = |
| 4948 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4949 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4950 | motionArgs = |
| 4951 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4952 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4953 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4954 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4955 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 4956 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4957 | } else { |
| 4958 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | void testNotifyKey(bool expectToBeFiltered) { |
| 4963 | NotifyKeyArgs keyArgs; |
| 4964 | |
| 4965 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4966 | mDispatcher->notifyKey(&keyArgs); |
| 4967 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 4968 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4969 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4970 | |
| 4971 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 4972 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4973 | } else { |
| 4974 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4975 | } |
| 4976 | } |
| 4977 | }; |
| 4978 | |
| 4979 | // Test InputFilter for MotionEvent |
| 4980 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 4981 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 4982 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4983 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4984 | |
| 4985 | // Enable InputFilter |
| 4986 | mDispatcher->setInputFilterEnabled(true); |
| 4987 | // Test touch on both primary and second display, and check if both events are filtered. |
| 4988 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 4989 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 4990 | |
| 4991 | // Disable InputFilter |
| 4992 | mDispatcher->setInputFilterEnabled(false); |
| 4993 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 4994 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4995 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4996 | } |
| 4997 | |
| 4998 | // Test InputFilter for KeyEvent |
| 4999 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 5000 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 5001 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5002 | |
| 5003 | // Enable InputFilter |
| 5004 | mDispatcher->setInputFilterEnabled(true); |
| 5005 | // Send a key event, and check if it is filtered. |
| 5006 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 5007 | |
| 5008 | // Disable InputFilter |
| 5009 | mDispatcher->setInputFilterEnabled(false); |
| 5010 | // Send a key event, and check if it isn't filtered. |
| 5011 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5012 | } |
| 5013 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5014 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 5015 | // logical display coordinate space. |
| 5016 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 5017 | ui::Transform firstDisplayTransform; |
| 5018 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5019 | ui::Transform secondDisplayTransform; |
| 5020 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 5021 | |
| 5022 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 5023 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 5024 | displayInfos[0].transform = firstDisplayTransform; |
| 5025 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 5026 | displayInfos[1].transform = secondDisplayTransform; |
| 5027 | |
| 5028 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 5029 | |
| 5030 | // Enable InputFilter |
| 5031 | mDispatcher->setInputFilterEnabled(true); |
| 5032 | |
| 5033 | // Ensure the correct transforms are used for the displays. |
| 5034 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 5035 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 5036 | } |
| 5037 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5038 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 5039 | protected: |
| 5040 | virtual void SetUp() override { |
| 5041 | InputDispatcherTest::SetUp(); |
| 5042 | |
| 5043 | /** |
| 5044 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 5045 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 5046 | * on. |
| 5047 | */ |
| 5048 | mDispatcher->setInputFilterEnabled(true); |
| 5049 | |
| 5050 | std::shared_ptr<InputApplicationHandle> application = |
| 5051 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5052 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 5053 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5054 | |
| 5055 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5056 | mWindow->setFocusable(true); |
| 5057 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5058 | setFocusedWindow(mWindow); |
| 5059 | mWindow->consumeFocusEvent(true); |
| 5060 | } |
| 5061 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5062 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5063 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5064 | KeyEvent event; |
| 5065 | |
| 5066 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5067 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 5068 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5069 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5070 | const int32_t additionalPolicyFlags = |
| 5071 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 5072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5073 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5074 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5075 | policyFlags | additionalPolicyFlags)); |
| 5076 | |
| 5077 | InputEvent* received = mWindow->consume(); |
| 5078 | ASSERT_NE(nullptr, received); |
| 5079 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5080 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 5081 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 5082 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 5083 | } |
| 5084 | |
| 5085 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5086 | int32_t flags) { |
| 5087 | MotionEvent event; |
| 5088 | PointerProperties pointerProperties[1]; |
| 5089 | PointerCoords pointerCoords[1]; |
| 5090 | pointerProperties[0].clear(); |
| 5091 | pointerProperties[0].id = 0; |
| 5092 | pointerCoords[0].clear(); |
| 5093 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 5094 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 5095 | |
| 5096 | ui::Transform identityTransform; |
| 5097 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5098 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 5099 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 5100 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 5101 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 5102 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 5103 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5104 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 5105 | |
| 5106 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 5107 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5108 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5109 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5110 | policyFlags | additionalPolicyFlags)); |
| 5111 | |
| 5112 | InputEvent* received = mWindow->consume(); |
| 5113 | ASSERT_NE(nullptr, received); |
| 5114 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 5115 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 5116 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 5117 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | private: |
| 5121 | sp<FakeWindowHandle> mWindow; |
| 5122 | }; |
| 5123 | |
| 5124 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5125 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 5126 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 5127 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5128 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 5129 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5132 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5133 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5134 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5135 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 5136 | } |
| 5137 | |
| 5138 | TEST_F(InputFilterInjectionPolicyTest, |
| 5139 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 5140 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5141 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5142 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5146 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 5147 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5150 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5151 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5152 | InputDispatcherTest::SetUp(); |
| 5153 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5154 | std::shared_ptr<FakeApplicationHandle> application = |
| 5155 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5156 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5157 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5158 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5159 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5160 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5161 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5162 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5163 | |
| 5164 | // Set focused application. |
| 5165 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5166 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5167 | |
| 5168 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5169 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5170 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5171 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5172 | } |
| 5173 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5174 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5175 | InputDispatcherTest::TearDown(); |
| 5176 | |
| 5177 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5178 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | protected: |
| 5182 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5183 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5184 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5185 | }; |
| 5186 | |
| 5187 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5188 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 5189 | // the onPointerDownOutsideFocus callback. |
| 5190 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5192 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5193 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5194 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5195 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5196 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5197 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5198 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 5199 | } |
| 5200 | |
| 5201 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 5202 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 5203 | // onPointerDownOutsideFocus callback. |
| 5204 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5206 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5207 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5208 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5209 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5210 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5211 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 5215 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 5216 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5217 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5218 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5219 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5220 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5221 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5222 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5223 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5227 | // DOWN on the window that already has focus. Ensure no window received the |
| 5228 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5229 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5230 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5231 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5232 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5233 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5234 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5235 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5236 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5237 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5238 | } |
| 5239 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 5240 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 5241 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 5242 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 5243 | const MotionEvent event = |
| 5244 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5245 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5246 | .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] | 5247 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 5248 | .build(); |
| 5249 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 5250 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5251 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 5252 | |
| 5253 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5254 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 5255 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 5256 | mUnfocusedWindow->assertNoEvents(); |
| 5257 | } |
| 5258 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5259 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 5260 | // windows that point to the same client token. |
| 5261 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 5262 | virtual void SetUp() override { |
| 5263 | InputDispatcherTest::SetUp(); |
| 5264 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5265 | std::shared_ptr<FakeApplicationHandle> application = |
| 5266 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5267 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 5268 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5269 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 5270 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5271 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 5272 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5273 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 5274 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5275 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | protected: |
| 5279 | sp<FakeWindowHandle> mWindow1; |
| 5280 | sp<FakeWindowHandle> mWindow2; |
| 5281 | |
| 5282 | // 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] | 5283 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5284 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 5285 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 5289 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5290 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5291 | InputEvent* event = window->consume(); |
| 5292 | |
| 5293 | ASSERT_NE(nullptr, event) << name.c_str() |
| 5294 | << ": consumer should have returned non-NULL event."; |
| 5295 | |
| 5296 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 5297 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 5298 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 5299 | |
| 5300 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5301 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 5302 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5303 | |
| 5304 | for (size_t i = 0; i < points.size(); i++) { |
| 5305 | float expectedX = points[i].x; |
| 5306 | float expectedY = points[i].y; |
| 5307 | |
| 5308 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 5309 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 5310 | << ", got " << motionEvent.getX(i); |
| 5311 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 5312 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 5313 | << ", got " << motionEvent.getY(i); |
| 5314 | } |
| 5315 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5316 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5317 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5318 | std::vector<PointF> expectedPoints) { |
| 5319 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 5320 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 5321 | mDispatcher->notifyMotion(&motionArgs); |
| 5322 | |
| 5323 | // Always consume from window1 since it's the window that has the InputReceiver |
| 5324 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 5325 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5326 | }; |
| 5327 | |
| 5328 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 5329 | // Touch Window 1 |
| 5330 | PointF touchedPoint = {10, 10}; |
| 5331 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5332 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5333 | |
| 5334 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5335 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5336 | |
| 5337 | // Touch Window 2 |
| 5338 | touchedPoint = {150, 150}; |
| 5339 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5340 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5341 | } |
| 5342 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5343 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 5344 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5345 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5346 | |
| 5347 | // Touch Window 1 |
| 5348 | PointF touchedPoint = {10, 10}; |
| 5349 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5350 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5351 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5352 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5353 | |
| 5354 | // Touch Window 2 |
| 5355 | touchedPoint = {150, 150}; |
| 5356 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5357 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 5358 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5359 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5360 | // Update the transform so rotation is set |
| 5361 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5362 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 5363 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5364 | } |
| 5365 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5366 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5367 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5368 | |
| 5369 | // Touch Window 1 |
| 5370 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5371 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5372 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5373 | |
| 5374 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5375 | touchedPoints.push_back(PointF{150, 150}); |
| 5376 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5377 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5378 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5379 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5380 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5381 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5382 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5383 | // Update the transform so rotation is set for Window 2 |
| 5384 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5385 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5386 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5389 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5390 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5391 | |
| 5392 | // Touch Window 1 |
| 5393 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5394 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5395 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5396 | |
| 5397 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5398 | touchedPoints.push_back(PointF{150, 150}); |
| 5399 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5400 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5401 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5402 | |
| 5403 | // Move both windows |
| 5404 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5405 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5406 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5407 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5408 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5409 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5410 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5411 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5412 | expectedPoints.pop_back(); |
| 5413 | |
| 5414 | // Touch Window 2 |
| 5415 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5416 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5417 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5418 | |
| 5419 | // Move both windows |
| 5420 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5421 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5422 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5423 | |
| 5424 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 5428 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 5429 | |
| 5430 | // Touch Window 1 |
| 5431 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5432 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5433 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5434 | |
| 5435 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5436 | touchedPoints.push_back(PointF{150, 150}); |
| 5437 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5438 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5439 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5440 | |
| 5441 | // Move both windows |
| 5442 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5443 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5444 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5445 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5446 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5449 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 5450 | virtual void SetUp() override { |
| 5451 | InputDispatcherTest::SetUp(); |
| 5452 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5453 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5454 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5455 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 5456 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5457 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5458 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5459 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5460 | |
| 5461 | // Set focused application. |
| 5462 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 5463 | |
| 5464 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5465 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5466 | mWindow->consumeFocusEvent(true); |
| 5467 | } |
| 5468 | |
| 5469 | virtual void TearDown() override { |
| 5470 | InputDispatcherTest::TearDown(); |
| 5471 | mWindow.clear(); |
| 5472 | } |
| 5473 | |
| 5474 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5475 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5476 | sp<FakeWindowHandle> mWindow; |
| 5477 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 5478 | |
| 5479 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5480 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5481 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5482 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5483 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5484 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5485 | WINDOW_LOCATION)); |
| 5486 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5487 | |
| 5488 | sp<FakeWindowHandle> addSpyWindow() { |
| 5489 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5490 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5491 | spy->setTrustedOverlay(true); |
| 5492 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5493 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5494 | spy->setDispatchingTimeout(30ms); |
| 5495 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 5496 | return spy; |
| 5497 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5498 | }; |
| 5499 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5500 | // Send a tap and respond, which should not cause an ANR. |
| 5501 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 5502 | tapOnWindow(); |
| 5503 | mWindow->consumeMotionDown(); |
| 5504 | mWindow->consumeMotionUp(); |
| 5505 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5506 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5507 | } |
| 5508 | |
| 5509 | // Send a regular key and respond, which should not cause an ANR. |
| 5510 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5512 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5513 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5514 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5515 | } |
| 5516 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5517 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 5518 | mWindow->setFocusable(false); |
| 5519 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5520 | mWindow->consumeFocusEvent(false); |
| 5521 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5522 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5523 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5524 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 5525 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5526 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5527 | // Key will not go to window because we have no focused window. |
| 5528 | // The 'no focused window' ANR timer should start instead. |
| 5529 | |
| 5530 | // Now, the focused application goes away. |
| 5531 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 5532 | // The key should get dropped and there should be no ANR. |
| 5533 | |
| 5534 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5535 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5536 | } |
| 5537 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5538 | // 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] | 5539 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 5540 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5541 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5543 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5544 | WINDOW_LOCATION)); |
| 5545 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5546 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 5547 | ASSERT_TRUE(sequenceNum); |
| 5548 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5549 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5550 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5551 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5552 | mWindow->consumeMotionEvent( |
| 5553 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5554 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5555 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | // Send a key to the app and have the app not respond right away. |
| 5559 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 5560 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5561 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5562 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 5563 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5564 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5565 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5566 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5567 | } |
| 5568 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5569 | // We have a focused application, but no focused window |
| 5570 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5571 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5572 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5573 | mWindow->consumeFocusEvent(false); |
| 5574 | |
| 5575 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5577 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5578 | WINDOW_LOCATION)); |
| 5579 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 5580 | mDispatcher->waitForIdle(); |
| 5581 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5582 | |
| 5583 | // Once a focused event arrives, we get an ANR for this application |
| 5584 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5585 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5586 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5587 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5588 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5589 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5590 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5591 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5592 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5593 | } |
| 5594 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5595 | /** |
| 5596 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 5597 | * there will not be an ANR. |
| 5598 | */ |
| 5599 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 5600 | mWindow->setFocusable(false); |
| 5601 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5602 | mWindow->consumeFocusEvent(false); |
| 5603 | |
| 5604 | KeyEvent event; |
| 5605 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 5606 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 5607 | |
| 5608 | // Define a valid key down event that is stale (too old). |
| 5609 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 5610 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5611 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5612 | |
| 5613 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 5614 | |
| 5615 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5616 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5617 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 5618 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 5619 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 5620 | << "Injection should fail because the event is stale"; |
| 5621 | |
| 5622 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5623 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5624 | mWindow->assertNoEvents(); |
| 5625 | } |
| 5626 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5627 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5628 | // Make sure that we don't notify policy twice about the same ANR. |
| 5629 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5630 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5631 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5632 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5633 | |
| 5634 | // Once a focused event arrives, we get an ANR for this application |
| 5635 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5636 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5637 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5638 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5639 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5640 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5641 | const std::chrono::duration appTimeout = |
| 5642 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5643 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5644 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5645 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5646 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 5647 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5648 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5649 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 5650 | // '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] | 5651 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5652 | } |
| 5653 | |
| 5654 | // We have a focused application, but no focused window |
| 5655 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5656 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5657 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5658 | mWindow->consumeFocusEvent(false); |
| 5659 | |
| 5660 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5661 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5662 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5663 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5664 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5665 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5666 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5667 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5668 | |
| 5669 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5670 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5671 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5672 | mWindow->assertNoEvents(); |
| 5673 | } |
| 5674 | |
| 5675 | /** |
| 5676 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 5677 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 5678 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 5679 | * the ANR mechanism should still work. |
| 5680 | * |
| 5681 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 5682 | * DOWN event, while not responding on the second one. |
| 5683 | */ |
| 5684 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 5685 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5686 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5687 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5688 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5689 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5690 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5691 | |
| 5692 | // Now send ACTION_UP, with identical timestamp |
| 5693 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5694 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5695 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5696 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5697 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5698 | |
| 5699 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 5700 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5701 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5702 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5703 | } |
| 5704 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5705 | // A spy window can receive an ANR |
| 5706 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 5707 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 5708 | |
| 5709 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5710 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5711 | WINDOW_LOCATION)); |
| 5712 | mWindow->consumeMotionDown(); |
| 5713 | |
| 5714 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 5715 | ASSERT_TRUE(sequenceNum); |
| 5716 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5717 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5718 | |
| 5719 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5720 | spy->consumeMotionEvent( |
| 5721 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5722 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5723 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5724 | } |
| 5725 | |
| 5726 | // 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] | 5727 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5728 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 5729 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5730 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5731 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5732 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5733 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5734 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5735 | |
| 5736 | // Stuck on the ACTION_UP |
| 5737 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5738 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5739 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5740 | // 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] | 5741 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5742 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5743 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5744 | |
| 5745 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5746 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5747 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5748 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5749 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5750 | } |
| 5751 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5752 | // 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] | 5753 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5754 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 5755 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5756 | |
| 5757 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5758 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5759 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5760 | |
| 5761 | mWindow->consumeMotionDown(); |
| 5762 | // Stuck on the ACTION_UP |
| 5763 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5764 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5765 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5766 | // 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] | 5767 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5768 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5769 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5770 | |
| 5771 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5772 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5773 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5774 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5775 | spy->assertNoEvents(); |
| 5776 | } |
| 5777 | |
| 5778 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 5779 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 5780 | |
| 5781 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5782 | |
| 5783 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5784 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5785 | WINDOW_LOCATION)); |
| 5786 | |
| 5787 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5788 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 5789 | ASSERT_TRUE(consumeSeq); |
| 5790 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5791 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5792 | |
| 5793 | monitor.finishEvent(*consumeSeq); |
| 5794 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5795 | |
| 5796 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5797 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5798 | } |
| 5799 | |
| 5800 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 5801 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 5802 | // get an ANR again. |
| 5803 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 5804 | // 2. consume all pending events (= queue becomes healthy again) |
| 5805 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 5806 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 5807 | tapOnWindow(); |
| 5808 | |
| 5809 | mWindow->consumeMotionDown(); |
| 5810 | // Block on ACTION_UP |
| 5811 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5812 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5813 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 5814 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5815 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5816 | mWindow->assertNoEvents(); |
| 5817 | |
| 5818 | tapOnWindow(); |
| 5819 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5820 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5821 | mWindow->consumeMotionUp(); |
| 5822 | |
| 5823 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5824 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5825 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5826 | mWindow->assertNoEvents(); |
| 5827 | } |
| 5828 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5829 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 5830 | // it. |
| 5831 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5833 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5834 | WINDOW_LOCATION)); |
| 5835 | |
| 5836 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5837 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5838 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5839 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 5840 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5841 | // 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] | 5842 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5843 | mWindow->consumeMotionEvent( |
| 5844 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5845 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5846 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5847 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5848 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5849 | } |
| 5850 | |
| 5851 | /** |
| 5852 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5853 | * not to to the focused window until the motion is processed. |
| 5854 | * |
| 5855 | * Warning!!! |
| 5856 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5857 | * and the injection timeout that we specify when injecting the key. |
| 5858 | * We must have the injection timeout (10ms) be smaller than |
| 5859 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5860 | * |
| 5861 | * If that value changes, this test should also change. |
| 5862 | */ |
| 5863 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 5864 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5865 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5866 | |
| 5867 | tapOnWindow(); |
| 5868 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5869 | ASSERT_TRUE(downSequenceNum); |
| 5870 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5871 | ASSERT_TRUE(upSequenceNum); |
| 5872 | // Don't finish the events yet, and send a key |
| 5873 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 5874 | // window even if motions are still being processed. But because the injection timeout is short, |
| 5875 | // we will receive INJECTION_TIMED_OUT as the result. |
| 5876 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5877 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5878 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5879 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5880 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5881 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5882 | // and the key remains pending, waiting for the touch events to be processed |
| 5883 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5884 | ASSERT_FALSE(keySequenceNum); |
| 5885 | |
| 5886 | std::this_thread::sleep_for(500ms); |
| 5887 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 5888 | // to the focused window, even though we have not yet finished the motion event |
| 5889 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5890 | mWindow->finishEvent(*downSequenceNum); |
| 5891 | mWindow->finishEvent(*upSequenceNum); |
| 5892 | } |
| 5893 | |
| 5894 | /** |
| 5895 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5896 | * not go to the focused window until the motion is processed. |
| 5897 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 5898 | * focused window right away. |
| 5899 | */ |
| 5900 | TEST_F(InputDispatcherSingleWindowAnr, |
| 5901 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 5902 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5903 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5904 | |
| 5905 | tapOnWindow(); |
| 5906 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5907 | ASSERT_TRUE(downSequenceNum); |
| 5908 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5909 | ASSERT_TRUE(upSequenceNum); |
| 5910 | // Don't finish the events yet, and send a key |
| 5911 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5912 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5913 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5914 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5915 | // At this point, key is still pending, and should not be sent to the application yet. |
| 5916 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5917 | ASSERT_FALSE(keySequenceNum); |
| 5918 | |
| 5919 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 5920 | tapOnWindow(); |
| 5921 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 5922 | // the other events yet. We can finish events in any order. |
| 5923 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 5924 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 5925 | mWindow->consumeMotionDown(); |
| 5926 | mWindow->consumeMotionUp(); |
| 5927 | mWindow->assertNoEvents(); |
| 5928 | } |
| 5929 | |
| 5930 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 5931 | virtual void SetUp() override { |
| 5932 | InputDispatcherTest::SetUp(); |
| 5933 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5934 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5935 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5936 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 5937 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5938 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5939 | // 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] | 5940 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5941 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5942 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 5943 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5944 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5945 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5946 | |
| 5947 | // Set focused application. |
| 5948 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5949 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5950 | |
| 5951 | // Expect one focus window exist in display. |
| 5952 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5953 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5954 | mFocusedWindow->consumeFocusEvent(true); |
| 5955 | } |
| 5956 | |
| 5957 | virtual void TearDown() override { |
| 5958 | InputDispatcherTest::TearDown(); |
| 5959 | |
| 5960 | mUnfocusedWindow.clear(); |
| 5961 | mFocusedWindow.clear(); |
| 5962 | } |
| 5963 | |
| 5964 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5965 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5966 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 5967 | sp<FakeWindowHandle> mFocusedWindow; |
| 5968 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 5969 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 5970 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 5971 | |
| 5972 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 5973 | |
| 5974 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 5975 | |
| 5976 | private: |
| 5977 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5978 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5979 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5980 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5981 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5982 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5983 | location)); |
| 5984 | } |
| 5985 | }; |
| 5986 | |
| 5987 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 5988 | // should be ANR'd first. |
| 5989 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5990 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5991 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5992 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5993 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5994 | mFocusedWindow->consumeMotionDown(); |
| 5995 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5996 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5997 | // We consumed all events, so no ANR |
| 5998 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5999 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6000 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6001 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6002 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6003 | FOCUSED_WINDOW_LOCATION)); |
| 6004 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6005 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6006 | |
| 6007 | const std::chrono::duration timeout = |
| 6008 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6009 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6010 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 6011 | // sequence to make it consistent |
| 6012 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6013 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6014 | mFocusedWindow->consumeMotionDown(); |
| 6015 | // This cancel is generated because the connection was unresponsive |
| 6016 | mFocusedWindow->consumeMotionCancel(); |
| 6017 | mFocusedWindow->assertNoEvents(); |
| 6018 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6019 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6020 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6021 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6022 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 6026 | // it doesn't matter which order they should have ANR. |
| 6027 | // But we should receive ANR for both. |
| 6028 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 6029 | // Set the timeout for unfocused window to match the focused window |
| 6030 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 6031 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6032 | |
| 6033 | tapOnFocusedWindow(); |
| 6034 | // 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] | 6035 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 6036 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 6037 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6038 | |
| 6039 | // 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] | 6040 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 6041 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 6042 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 6043 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6044 | |
| 6045 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6046 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6047 | |
| 6048 | mFocusedWindow->consumeMotionDown(); |
| 6049 | mFocusedWindow->consumeMotionUp(); |
| 6050 | mUnfocusedWindow->consumeMotionOutside(); |
| 6051 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6052 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 6053 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 6054 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6055 | |
| 6056 | // Both applications should be marked as responsive, in any order |
| 6057 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 6058 | mFocusedWindow->getToken() == responsiveToken2); |
| 6059 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 6060 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 6061 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6062 | } |
| 6063 | |
| 6064 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 6065 | // We should also log an error to account for the dropped event (not tested here). |
| 6066 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 6067 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 6068 | tapOnFocusedWindow(); |
| 6069 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6070 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6071 | // Receive the events, but don't respond |
| 6072 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 6073 | ASSERT_TRUE(downEventSequenceNum); |
| 6074 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 6075 | ASSERT_TRUE(upEventSequenceNum); |
| 6076 | const std::chrono::duration timeout = |
| 6077 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6078 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6079 | |
| 6080 | // Tap once again |
| 6081 | // 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] | 6082 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6083 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6084 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6085 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6086 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6087 | FOCUSED_WINDOW_LOCATION)); |
| 6088 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 6089 | // valid touch target |
| 6090 | mUnfocusedWindow->assertNoEvents(); |
| 6091 | |
| 6092 | // Consume the first tap |
| 6093 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 6094 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 6095 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6096 | // The second tap did not go to the focused window |
| 6097 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6098 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6099 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6100 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6101 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6102 | } |
| 6103 | |
| 6104 | // If you tap outside of all windows, there will not be ANR |
| 6105 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6106 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6107 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6108 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 6109 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6110 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6111 | } |
| 6112 | |
| 6113 | // Since the focused window is paused, tapping on it should not produce any events |
| 6114 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 6115 | mFocusedWindow->setPaused(true); |
| 6116 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6117 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6118 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6119 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6120 | FOCUSED_WINDOW_LOCATION)); |
| 6121 | |
| 6122 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 6123 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6124 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 6125 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6126 | |
| 6127 | mFocusedWindow->assertNoEvents(); |
| 6128 | mUnfocusedWindow->assertNoEvents(); |
| 6129 | } |
| 6130 | |
| 6131 | /** |
| 6132 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6133 | * not to to the focused window until the motion is processed. |
| 6134 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 6135 | * |
| 6136 | * Warning!!! |
| 6137 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6138 | * and the injection timeout that we specify when injecting the key. |
| 6139 | * We must have the injection timeout (10ms) be smaller than |
| 6140 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6141 | * |
| 6142 | * If that value changes, this test should also change. |
| 6143 | */ |
| 6144 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 6145 | // Set a long ANR timeout to prevent it from triggering |
| 6146 | mFocusedWindow->setDispatchingTimeout(2s); |
| 6147 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6148 | |
| 6149 | tapOnUnfocusedWindow(); |
| 6150 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6151 | ASSERT_TRUE(downSequenceNum); |
| 6152 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6153 | ASSERT_TRUE(upSequenceNum); |
| 6154 | // Don't finish the events yet, and send a key |
| 6155 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 6156 | // window even if motions are still being processed. |
| 6157 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6158 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6159 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6160 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6161 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6162 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6163 | // and the key remains pending, waiting for the touch events to be processed |
| 6164 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 6165 | ASSERT_FALSE(keySequenceNum); |
| 6166 | |
| 6167 | // 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] | 6168 | mFocusedWindow->setFocusable(false); |
| 6169 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6170 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6171 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6172 | |
| 6173 | // Focus events should precede the key events |
| 6174 | mUnfocusedWindow->consumeFocusEvent(true); |
| 6175 | mFocusedWindow->consumeFocusEvent(false); |
| 6176 | |
| 6177 | // Finish the tap events, which should unblock dispatcher |
| 6178 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 6179 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 6180 | |
| 6181 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 6182 | // can finally go to the newly focused "mUnfocusedWindow". |
| 6183 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6184 | mFocusedWindow->assertNoEvents(); |
| 6185 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6186 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6187 | } |
| 6188 | |
| 6189 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 6190 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 6191 | // The other window should not be affected by that. |
| 6192 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 6193 | // Touch Window 1 |
| 6194 | NotifyMotionArgs motionArgs = |
| 6195 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6196 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 6197 | mDispatcher->notifyMotion(&motionArgs); |
| 6198 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6199 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6200 | |
| 6201 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6202 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6203 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6204 | mDispatcher->notifyMotion(&motionArgs); |
| 6205 | |
| 6206 | const std::chrono::duration timeout = |
| 6207 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6208 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6209 | |
| 6210 | mUnfocusedWindow->consumeMotionDown(); |
| 6211 | mFocusedWindow->consumeMotionDown(); |
| 6212 | // Focused window may or may not receive ACTION_MOVE |
| 6213 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 6214 | InputEvent* event; |
| 6215 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 6216 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 6217 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 6218 | ASSERT_NE(nullptr, event); |
| 6219 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 6220 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 6221 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 6222 | mFocusedWindow->consumeMotionCancel(); |
| 6223 | } else { |
| 6224 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 6225 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6226 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6227 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6228 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6229 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6230 | mUnfocusedWindow->assertNoEvents(); |
| 6231 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6232 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6233 | } |
| 6234 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6235 | /** |
| 6236 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 6237 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 6238 | * |
| 6239 | * If the user touches another application during this time, the key should be dropped. |
| 6240 | * Next, if a new focused window comes in, without toggling the focused application, |
| 6241 | * then no ANR should occur. |
| 6242 | * |
| 6243 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 6244 | * but in some cases the policy may not update the focused application. |
| 6245 | */ |
| 6246 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 6247 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 6248 | std::make_shared<FakeApplicationHandle>(); |
| 6249 | focusedApplication->setDispatchingTimeout(60ms); |
| 6250 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 6251 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 6252 | mFocusedWindow->setFocusable(false); |
| 6253 | |
| 6254 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6255 | mFocusedWindow->consumeFocusEvent(false); |
| 6256 | |
| 6257 | // Send a key. The ANR timer should start because there is no focused window. |
| 6258 | // 'focusedApplication' will get blamed if this timer completes. |
| 6259 | // 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] | 6260 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6261 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6262 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6263 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6264 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6265 | |
| 6266 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 6267 | // then the injected touches won't cause the focused event to get dropped. |
| 6268 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 6269 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 6270 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 6271 | // For this test, it means that the key would get delivered to the window once it becomes |
| 6272 | // focused. |
| 6273 | std::this_thread::sleep_for(10ms); |
| 6274 | |
| 6275 | // Touch unfocused window. This should force the pending key to get dropped. |
| 6276 | NotifyMotionArgs motionArgs = |
| 6277 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6278 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 6279 | mDispatcher->notifyMotion(&motionArgs); |
| 6280 | |
| 6281 | // We do not consume the motion right away, because that would require dispatcher to first |
| 6282 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 6283 | // Set the focused window first. |
| 6284 | mFocusedWindow->setFocusable(true); |
| 6285 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6286 | setFocusedWindow(mFocusedWindow); |
| 6287 | mFocusedWindow->consumeFocusEvent(true); |
| 6288 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 6289 | // to another application. This could be a bug / behaviour in the policy. |
| 6290 | |
| 6291 | mUnfocusedWindow->consumeMotionDown(); |
| 6292 | |
| 6293 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6294 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 6295 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 6296 | } |
| 6297 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6298 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 6299 | // that has feature NO_INPUT_CHANNEL. |
| 6300 | // Layout: |
| 6301 | // Top (closest to user) |
| 6302 | // mNoInputWindow (above all windows) |
| 6303 | // mBottomWindow |
| 6304 | // Bottom (furthest from user) |
| 6305 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 6306 | virtual void SetUp() override { |
| 6307 | InputDispatcherTest::SetUp(); |
| 6308 | |
| 6309 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6310 | mNoInputWindow = |
| 6311 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 6312 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6313 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6314 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6315 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6316 | // It's perfectly valid for this window to not have an associated input channel |
| 6317 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6318 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 6319 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6320 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6321 | |
| 6322 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 6323 | } |
| 6324 | |
| 6325 | protected: |
| 6326 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 6327 | sp<FakeWindowHandle> mNoInputWindow; |
| 6328 | sp<FakeWindowHandle> mBottomWindow; |
| 6329 | }; |
| 6330 | |
| 6331 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 6332 | PointF touchedPoint = {10, 10}; |
| 6333 | |
| 6334 | NotifyMotionArgs motionArgs = |
| 6335 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6336 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 6337 | mDispatcher->notifyMotion(&motionArgs); |
| 6338 | |
| 6339 | mNoInputWindow->assertNoEvents(); |
| 6340 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 6341 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 6342 | // and therefore should prevent mBottomWindow from receiving touches |
| 6343 | mBottomWindow->assertNoEvents(); |
| 6344 | } |
| 6345 | |
| 6346 | /** |
| 6347 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 6348 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 6349 | */ |
| 6350 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 6351 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6352 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 6353 | "Window with input channel and NO_INPUT_CHANNEL", |
| 6354 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6355 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6356 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6357 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6358 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 6359 | |
| 6360 | PointF touchedPoint = {10, 10}; |
| 6361 | |
| 6362 | NotifyMotionArgs motionArgs = |
| 6363 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6364 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 6365 | mDispatcher->notifyMotion(&motionArgs); |
| 6366 | |
| 6367 | mNoInputWindow->assertNoEvents(); |
| 6368 | mBottomWindow->assertNoEvents(); |
| 6369 | } |
| 6370 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6371 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 6372 | protected: |
| 6373 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6374 | sp<FakeWindowHandle> mWindow; |
| 6375 | sp<FakeWindowHandle> mMirror; |
| 6376 | |
| 6377 | virtual void SetUp() override { |
| 6378 | InputDispatcherTest::SetUp(); |
| 6379 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6380 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 6381 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 6382 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6383 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6384 | mWindow->setFocusable(true); |
| 6385 | mMirror->setFocusable(true); |
| 6386 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6387 | } |
| 6388 | }; |
| 6389 | |
| 6390 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 6391 | // Request focus on a mirrored window |
| 6392 | setFocusedWindow(mMirror); |
| 6393 | |
| 6394 | // window gets focused |
| 6395 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6396 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6397 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6398 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6399 | } |
| 6400 | |
| 6401 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 6402 | // focusable. |
| 6403 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 6404 | setFocusedWindow(mMirror); |
| 6405 | |
| 6406 | // window gets focused |
| 6407 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6408 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6409 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6410 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6412 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6413 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6414 | |
| 6415 | mMirror->setFocusable(false); |
| 6416 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6417 | |
| 6418 | // window loses focus since one of the windows associated with the token in not focusable |
| 6419 | mWindow->consumeFocusEvent(false); |
| 6420 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6421 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6422 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6423 | mWindow->assertNoEvents(); |
| 6424 | } |
| 6425 | |
| 6426 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 6427 | // invisible. |
| 6428 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 6429 | setFocusedWindow(mMirror); |
| 6430 | |
| 6431 | // window gets focused |
| 6432 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6433 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6434 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6435 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6436 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6437 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6438 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6439 | |
| 6440 | mMirror->setVisible(false); |
| 6441 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6442 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6443 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6444 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6445 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6446 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6447 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6448 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6449 | |
| 6450 | mWindow->setVisible(false); |
| 6451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6452 | |
| 6453 | // window loses focus only after all windows associated with the token become invisible. |
| 6454 | mWindow->consumeFocusEvent(false); |
| 6455 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6456 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6457 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6458 | mWindow->assertNoEvents(); |
| 6459 | } |
| 6460 | |
| 6461 | // A focused & mirrored window remains focused until both windows are removed. |
| 6462 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 6463 | setFocusedWindow(mMirror); |
| 6464 | |
| 6465 | // window gets focused |
| 6466 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6467 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6468 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6469 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6470 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6471 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6472 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6473 | |
| 6474 | // single window is removed but the window token remains focused |
| 6475 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 6476 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6477 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6478 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6479 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6480 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6481 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6482 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6483 | |
| 6484 | // Both windows are removed |
| 6485 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 6486 | mWindow->consumeFocusEvent(false); |
| 6487 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6488 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6489 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6490 | mWindow->assertNoEvents(); |
| 6491 | } |
| 6492 | |
| 6493 | // Focus request can be pending until one window becomes visible. |
| 6494 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 6495 | // Request focus on an invisible mirror. |
| 6496 | mWindow->setVisible(false); |
| 6497 | mMirror->setVisible(false); |
| 6498 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6499 | setFocusedWindow(mMirror); |
| 6500 | |
| 6501 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6502 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6503 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6504 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6505 | |
| 6506 | mMirror->setVisible(true); |
| 6507 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6508 | |
| 6509 | // window gets focused |
| 6510 | mWindow->consumeFocusEvent(true); |
| 6511 | // window gets the pending key event |
| 6512 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6513 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6514 | |
| 6515 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 6516 | protected: |
| 6517 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6518 | sp<FakeWindowHandle> mWindow; |
| 6519 | sp<FakeWindowHandle> mSecondWindow; |
| 6520 | |
| 6521 | void SetUp() override { |
| 6522 | InputDispatcherTest::SetUp(); |
| 6523 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6524 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6525 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6526 | mSecondWindow = |
| 6527 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6528 | mSecondWindow->setFocusable(true); |
| 6529 | |
| 6530 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6531 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 6532 | |
| 6533 | setFocusedWindow(mWindow); |
| 6534 | mWindow->consumeFocusEvent(true); |
| 6535 | } |
| 6536 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6537 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 6538 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6539 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 6540 | } |
| 6541 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6542 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 6543 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6544 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6545 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 6546 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6547 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6548 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6549 | } |
| 6550 | }; |
| 6551 | |
| 6552 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 6553 | // Ensure that capture cannot be obtained for unfocused windows. |
| 6554 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 6555 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6556 | mSecondWindow->assertNoEvents(); |
| 6557 | |
| 6558 | // Ensure that capture can be enabled from the focus window. |
| 6559 | requestAndVerifyPointerCapture(mWindow, true); |
| 6560 | |
| 6561 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 6562 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 6563 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6564 | |
| 6565 | // Ensure that capture can be disabled from the window with capture. |
| 6566 | requestAndVerifyPointerCapture(mWindow, false); |
| 6567 | } |
| 6568 | |
| 6569 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6570 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6571 | |
| 6572 | setFocusedWindow(mSecondWindow); |
| 6573 | |
| 6574 | // Ensure that the capture disabled event was sent first. |
| 6575 | mWindow->consumeCaptureEvent(false); |
| 6576 | mWindow->consumeFocusEvent(false); |
| 6577 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6578 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6579 | |
| 6580 | // 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] | 6581 | notifyPointerCaptureChanged({}); |
| 6582 | notifyPointerCaptureChanged(request); |
| 6583 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6584 | mWindow->assertNoEvents(); |
| 6585 | mSecondWindow->assertNoEvents(); |
| 6586 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6587 | } |
| 6588 | |
| 6589 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6590 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6591 | |
| 6592 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6593 | notifyPointerCaptureChanged({}); |
| 6594 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6595 | |
| 6596 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6597 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6598 | mWindow->consumeCaptureEvent(false); |
| 6599 | mWindow->assertNoEvents(); |
| 6600 | } |
| 6601 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6602 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 6603 | requestAndVerifyPointerCapture(mWindow, true); |
| 6604 | |
| 6605 | // The first window loses focus. |
| 6606 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6607 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6608 | mWindow->consumeCaptureEvent(false); |
| 6609 | |
| 6610 | // Request Pointer Capture from the second window before the notification from InputReader |
| 6611 | // arrives. |
| 6612 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6613 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6614 | |
| 6615 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6616 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6617 | |
| 6618 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6619 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6620 | |
| 6621 | mSecondWindow->consumeFocusEvent(true); |
| 6622 | mSecondWindow->consumeCaptureEvent(true); |
| 6623 | } |
| 6624 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6625 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 6626 | // App repeatedly enables and disables capture. |
| 6627 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6628 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6629 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6630 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6631 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6632 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6633 | |
| 6634 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 6635 | // first request is now stale, this should do nothing. |
| 6636 | notifyPointerCaptureChanged(firstRequest); |
| 6637 | mWindow->assertNoEvents(); |
| 6638 | |
| 6639 | // InputReader notifies that the second request was enabled. |
| 6640 | notifyPointerCaptureChanged(secondRequest); |
| 6641 | mWindow->consumeCaptureEvent(true); |
| 6642 | } |
| 6643 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 6644 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 6645 | requestAndVerifyPointerCapture(mWindow, true); |
| 6646 | |
| 6647 | // App toggles pointer capture off and on. |
| 6648 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6649 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6650 | |
| 6651 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6652 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6653 | |
| 6654 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 6655 | // preceding "disable" request. |
| 6656 | notifyPointerCaptureChanged(enableRequest); |
| 6657 | |
| 6658 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 6659 | // any notifications. |
| 6660 | mWindow->assertNoEvents(); |
| 6661 | } |
| 6662 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6663 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 6664 | protected: |
| 6665 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6666 | |
| 6667 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 6668 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 6669 | |
| 6670 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 6671 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6672 | |
| 6673 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 6674 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 6675 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6676 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 6677 | MAXIMUM_OBSCURING_OPACITY); |
| 6678 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6679 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6680 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6681 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6682 | |
| 6683 | sp<FakeWindowHandle> mTouchWindow; |
| 6684 | |
| 6685 | virtual void SetUp() override { |
| 6686 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6687 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6688 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 6689 | } |
| 6690 | |
| 6691 | virtual void TearDown() override { |
| 6692 | InputDispatcherTest::TearDown(); |
| 6693 | mTouchWindow.clear(); |
| 6694 | } |
| 6695 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6696 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 6697 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6698 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6699 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6700 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6701 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6702 | return window; |
| 6703 | } |
| 6704 | |
| 6705 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 6706 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 6707 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6708 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6709 | // Generate an arbitrary PID based on the UID |
| 6710 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 6711 | return window; |
| 6712 | } |
| 6713 | |
| 6714 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 6715 | NotifyMotionArgs args = |
| 6716 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6717 | ADISPLAY_ID_DEFAULT, points); |
| 6718 | mDispatcher->notifyMotion(&args); |
| 6719 | } |
| 6720 | }; |
| 6721 | |
| 6722 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6723 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6724 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6725 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6726 | |
| 6727 | touch(); |
| 6728 | |
| 6729 | mTouchWindow->assertNoEvents(); |
| 6730 | } |
| 6731 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6732 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6733 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 6734 | const sp<FakeWindowHandle>& w = |
| 6735 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 6736 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6737 | |
| 6738 | touch(); |
| 6739 | |
| 6740 | mTouchWindow->assertNoEvents(); |
| 6741 | } |
| 6742 | |
| 6743 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6744 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 6745 | const sp<FakeWindowHandle>& w = |
| 6746 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6747 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6748 | |
| 6749 | touch(); |
| 6750 | |
| 6751 | w->assertNoEvents(); |
| 6752 | } |
| 6753 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6754 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6755 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 6756 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6757 | |
| 6758 | touch(); |
| 6759 | |
| 6760 | mTouchWindow->consumeAnyMotionDown(); |
| 6761 | } |
| 6762 | |
| 6763 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6764 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6765 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6766 | w->setFrame(Rect(0, 0, 50, 50)); |
| 6767 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6768 | |
| 6769 | touch({PointF{100, 100}}); |
| 6770 | |
| 6771 | mTouchWindow->consumeAnyMotionDown(); |
| 6772 | } |
| 6773 | |
| 6774 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6775 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6776 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6777 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6778 | |
| 6779 | touch(); |
| 6780 | |
| 6781 | mTouchWindow->consumeAnyMotionDown(); |
| 6782 | } |
| 6783 | |
| 6784 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 6785 | const sp<FakeWindowHandle>& w = |
| 6786 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6787 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6788 | |
| 6789 | touch(); |
| 6790 | |
| 6791 | mTouchWindow->consumeAnyMotionDown(); |
| 6792 | } |
| 6793 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6794 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 6795 | const sp<FakeWindowHandle>& w = |
| 6796 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6797 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6798 | |
| 6799 | touch(); |
| 6800 | |
| 6801 | w->assertNoEvents(); |
| 6802 | } |
| 6803 | |
| 6804 | /** |
| 6805 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 6806 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 6807 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 6808 | */ |
| 6809 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6810 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 6811 | const sp<FakeWindowHandle>& w = |
| 6812 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6813 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6814 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6815 | |
| 6816 | touch(); |
| 6817 | |
| 6818 | w->consumeMotionOutside(); |
| 6819 | } |
| 6820 | |
| 6821 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 6822 | const sp<FakeWindowHandle>& w = |
| 6823 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6824 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6825 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6826 | |
| 6827 | touch(); |
| 6828 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6829 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6830 | } |
| 6831 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6832 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6833 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6834 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6835 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6836 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6837 | |
| 6838 | touch(); |
| 6839 | |
| 6840 | mTouchWindow->consumeAnyMotionDown(); |
| 6841 | } |
| 6842 | |
| 6843 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 6844 | const sp<FakeWindowHandle>& w = |
| 6845 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6846 | MAXIMUM_OBSCURING_OPACITY); |
| 6847 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6848 | |
| 6849 | touch(); |
| 6850 | |
| 6851 | mTouchWindow->consumeAnyMotionDown(); |
| 6852 | } |
| 6853 | |
| 6854 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6855 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6856 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6857 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6858 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6859 | |
| 6860 | touch(); |
| 6861 | |
| 6862 | mTouchWindow->assertNoEvents(); |
| 6863 | } |
| 6864 | |
| 6865 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 6866 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 6867 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6868 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6869 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6870 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6871 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6872 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6873 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6874 | |
| 6875 | touch(); |
| 6876 | |
| 6877 | mTouchWindow->assertNoEvents(); |
| 6878 | } |
| 6879 | |
| 6880 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 6881 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 6882 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6883 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6884 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6885 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6886 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6887 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6889 | |
| 6890 | touch(); |
| 6891 | |
| 6892 | mTouchWindow->consumeAnyMotionDown(); |
| 6893 | } |
| 6894 | |
| 6895 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6896 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 6897 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6898 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6899 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6900 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6901 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6902 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6903 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6904 | |
| 6905 | touch(); |
| 6906 | |
| 6907 | mTouchWindow->consumeAnyMotionDown(); |
| 6908 | } |
| 6909 | |
| 6910 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 6911 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6912 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6913 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6914 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6915 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6916 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6917 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6918 | |
| 6919 | touch(); |
| 6920 | |
| 6921 | mTouchWindow->assertNoEvents(); |
| 6922 | } |
| 6923 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6924 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6925 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 6926 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6927 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6928 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6929 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6930 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6931 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6932 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6933 | |
| 6934 | touch(); |
| 6935 | |
| 6936 | mTouchWindow->assertNoEvents(); |
| 6937 | } |
| 6938 | |
| 6939 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6940 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 6941 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6942 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6943 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6944 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6945 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6946 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6947 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6948 | |
| 6949 | touch(); |
| 6950 | |
| 6951 | mTouchWindow->consumeAnyMotionDown(); |
| 6952 | } |
| 6953 | |
| 6954 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 6955 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6956 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6957 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6958 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6959 | |
| 6960 | touch(); |
| 6961 | |
| 6962 | mTouchWindow->consumeAnyMotionDown(); |
| 6963 | } |
| 6964 | |
| 6965 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 6966 | const sp<FakeWindowHandle>& w = |
| 6967 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6968 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6969 | |
| 6970 | touch(); |
| 6971 | |
| 6972 | mTouchWindow->consumeAnyMotionDown(); |
| 6973 | } |
| 6974 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6975 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6976 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 6977 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6978 | const sp<FakeWindowHandle>& w = |
| 6979 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 6980 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6981 | |
| 6982 | touch(); |
| 6983 | |
| 6984 | mTouchWindow->assertNoEvents(); |
| 6985 | } |
| 6986 | |
| 6987 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 6988 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6989 | const sp<FakeWindowHandle>& w = |
| 6990 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 6991 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6992 | |
| 6993 | touch(); |
| 6994 | |
| 6995 | mTouchWindow->consumeAnyMotionDown(); |
| 6996 | } |
| 6997 | |
| 6998 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6999 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 7000 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 7001 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7002 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7003 | OPACITY_ABOVE_THRESHOLD); |
| 7004 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7005 | |
| 7006 | touch(); |
| 7007 | |
| 7008 | mTouchWindow->consumeAnyMotionDown(); |
| 7009 | } |
| 7010 | |
| 7011 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7012 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 7013 | const sp<FakeWindowHandle>& w1 = |
| 7014 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7015 | OPACITY_BELOW_THRESHOLD); |
| 7016 | const sp<FakeWindowHandle>& w2 = |
| 7017 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7018 | OPACITY_BELOW_THRESHOLD); |
| 7019 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7020 | |
| 7021 | touch(); |
| 7022 | |
| 7023 | mTouchWindow->assertNoEvents(); |
| 7024 | } |
| 7025 | |
| 7026 | /** |
| 7027 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 7028 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 7029 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 7030 | */ |
| 7031 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7032 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 7033 | const sp<FakeWindowHandle>& wB = |
| 7034 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7035 | OPACITY_BELOW_THRESHOLD); |
| 7036 | const sp<FakeWindowHandle>& wC = |
| 7037 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7038 | OPACITY_BELOW_THRESHOLD); |
| 7039 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7040 | |
| 7041 | touch(); |
| 7042 | |
| 7043 | mTouchWindow->assertNoEvents(); |
| 7044 | } |
| 7045 | |
| 7046 | /** |
| 7047 | * This test is testing that a window from a different UID but with same application token doesn't |
| 7048 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 7049 | */ |
| 7050 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7051 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 7052 | const sp<FakeWindowHandle>& w = |
| 7053 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7054 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7055 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7056 | |
| 7057 | touch(); |
| 7058 | |
| 7059 | mTouchWindow->consumeAnyMotionDown(); |
| 7060 | } |
| 7061 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7062 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 7063 | protected: |
| 7064 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7065 | sp<FakeWindowHandle> mWindow; |
| 7066 | sp<FakeWindowHandle> mSecondWindow; |
| 7067 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7068 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7069 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 7070 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7071 | |
| 7072 | void SetUp() override { |
| 7073 | InputDispatcherTest::SetUp(); |
| 7074 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7075 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7076 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7077 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7078 | mSecondWindow = |
| 7079 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7080 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7081 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7082 | mSpyWindow = |
| 7083 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7084 | mSpyWindow->setSpy(true); |
| 7085 | mSpyWindow->setTrustedOverlay(true); |
| 7086 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 7087 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7088 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7090 | } |
| 7091 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7092 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 7093 | switch (fromSource) { |
| 7094 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 7095 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7096 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7097 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7098 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7099 | break; |
| 7100 | case AINPUT_SOURCE_STYLUS: |
| 7101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7102 | injectMotionEvent( |
| 7103 | mDispatcher, |
| 7104 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7105 | AINPUT_SOURCE_STYLUS) |
| 7106 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 7107 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7108 | .x(50) |
| 7109 | .y(50)) |
| 7110 | .build())); |
| 7111 | break; |
| 7112 | case AINPUT_SOURCE_MOUSE: |
| 7113 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7114 | injectMotionEvent( |
| 7115 | mDispatcher, |
| 7116 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7117 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7118 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7119 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7120 | .x(50) |
| 7121 | .y(50)) |
| 7122 | .build())); |
| 7123 | break; |
| 7124 | default: |
| 7125 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 7126 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7127 | |
| 7128 | // Window should receive motion event. |
| 7129 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7130 | // Spy window should also receive motion event |
| 7131 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7132 | } |
| 7133 | |
| 7134 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 7135 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 7136 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7137 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7138 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7139 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7140 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7141 | |
| 7142 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7143 | mDragWindow = |
| 7144 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7145 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7146 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7147 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7148 | |
| 7149 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7150 | bool transferred = |
| 7151 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7152 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7153 | if (transferred) { |
| 7154 | mWindow->consumeMotionCancel(); |
| 7155 | mDragWindow->consumeMotionDown(); |
| 7156 | } |
| 7157 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7158 | } |
| 7159 | }; |
| 7160 | |
| 7161 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7162 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7163 | |
| 7164 | // Move on window. |
| 7165 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7166 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7167 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7168 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7169 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7170 | mWindow->consumeDragEvent(false, 50, 50); |
| 7171 | mSecondWindow->assertNoEvents(); |
| 7172 | |
| 7173 | // Move to another window. |
| 7174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7175 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7176 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7177 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7178 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7179 | mWindow->consumeDragEvent(true, 150, 50); |
| 7180 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7181 | |
| 7182 | // Move back to original window. |
| 7183 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7184 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7185 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7186 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7187 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7188 | mWindow->consumeDragEvent(false, 50, 50); |
| 7189 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 7190 | |
| 7191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7192 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7193 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7194 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7195 | mWindow->assertNoEvents(); |
| 7196 | mSecondWindow->assertNoEvents(); |
| 7197 | } |
| 7198 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7199 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7200 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7201 | |
| 7202 | // No cancel event after drag start |
| 7203 | mSpyWindow->assertNoEvents(); |
| 7204 | |
| 7205 | const MotionEvent secondFingerDownEvent = |
| 7206 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7207 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7208 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7209 | .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] | 7210 | .build(); |
| 7211 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7212 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7213 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7214 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7215 | |
| 7216 | // Receives cancel for first pointer after next pointer down |
| 7217 | mSpyWindow->consumeMotionCancel(); |
| 7218 | mSpyWindow->consumeMotionDown(); |
| 7219 | |
| 7220 | mSpyWindow->assertNoEvents(); |
| 7221 | } |
| 7222 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7223 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7224 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7225 | |
| 7226 | // Move on window. |
| 7227 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7228 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7229 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7230 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7231 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7232 | mWindow->consumeDragEvent(false, 50, 50); |
| 7233 | mSecondWindow->assertNoEvents(); |
| 7234 | |
| 7235 | // Move to another window. |
| 7236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7237 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7238 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7239 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7240 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7241 | mWindow->consumeDragEvent(true, 150, 50); |
| 7242 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7243 | |
| 7244 | // drop to another window. |
| 7245 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7246 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7247 | {150, 50})) |
| 7248 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7249 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7250 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7251 | mWindow->assertNoEvents(); |
| 7252 | mSecondWindow->assertNoEvents(); |
| 7253 | } |
| 7254 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7255 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7256 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7257 | |
| 7258 | // Move on window and keep button pressed. |
| 7259 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7260 | injectMotionEvent(mDispatcher, |
| 7261 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7262 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 7263 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7264 | .x(50) |
| 7265 | .y(50)) |
| 7266 | .build())) |
| 7267 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7268 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7269 | mWindow->consumeDragEvent(false, 50, 50); |
| 7270 | mSecondWindow->assertNoEvents(); |
| 7271 | |
| 7272 | // Move to another window and release button, expect to drop item. |
| 7273 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7274 | injectMotionEvent(mDispatcher, |
| 7275 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7276 | .buttonState(0) |
| 7277 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7278 | .x(150) |
| 7279 | .y(50)) |
| 7280 | .build())) |
| 7281 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7282 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7283 | mWindow->assertNoEvents(); |
| 7284 | mSecondWindow->assertNoEvents(); |
| 7285 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7286 | |
| 7287 | // nothing to the window. |
| 7288 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7289 | injectMotionEvent(mDispatcher, |
| 7290 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 7291 | .buttonState(0) |
| 7292 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7293 | .x(150) |
| 7294 | .y(50)) |
| 7295 | .build())) |
| 7296 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7297 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7298 | mWindow->assertNoEvents(); |
| 7299 | mSecondWindow->assertNoEvents(); |
| 7300 | } |
| 7301 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7302 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7303 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 7304 | |
| 7305 | // Set second window invisible. |
| 7306 | mSecondWindow->setVisible(false); |
| 7307 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 7308 | |
| 7309 | // Move on window. |
| 7310 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7311 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7312 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7313 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7314 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7315 | mWindow->consumeDragEvent(false, 50, 50); |
| 7316 | mSecondWindow->assertNoEvents(); |
| 7317 | |
| 7318 | // Move to another window. |
| 7319 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7320 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7321 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7322 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7323 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7324 | mWindow->consumeDragEvent(true, 150, 50); |
| 7325 | mSecondWindow->assertNoEvents(); |
| 7326 | |
| 7327 | // drop to another window. |
| 7328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7329 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7330 | {150, 50})) |
| 7331 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7332 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7333 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 7334 | mWindow->assertNoEvents(); |
| 7335 | mSecondWindow->assertNoEvents(); |
| 7336 | } |
| 7337 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7338 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7339 | // Ensure window could track pointerIds if it didn't support split touch. |
| 7340 | mWindow->setPreventSplitting(true); |
| 7341 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7343 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7344 | {50, 50})) |
| 7345 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7346 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7347 | |
| 7348 | const MotionEvent secondFingerDownEvent = |
| 7349 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7350 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7351 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7352 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7353 | .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] | 7354 | .build(); |
| 7355 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7356 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7357 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7358 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7359 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7360 | |
| 7361 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7362 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
| 7365 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 7366 | // First down on second window. |
| 7367 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7368 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7369 | {150, 50})) |
| 7370 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7371 | |
| 7372 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7373 | |
| 7374 | // Second down on first window. |
| 7375 | const MotionEvent secondFingerDownEvent = |
| 7376 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7377 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7378 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7379 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7380 | .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] | 7381 | .build(); |
| 7382 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7383 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7384 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7385 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7386 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7387 | |
| 7388 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7389 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7390 | |
| 7391 | // Move on window. |
| 7392 | const MotionEvent secondFingerMoveEvent = |
| 7393 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7394 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7395 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7396 | .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] | 7397 | .build(); |
| 7398 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7399 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 7400 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 7401 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7402 | mWindow->consumeDragEvent(false, 50, 50); |
| 7403 | mSecondWindow->consumeMotionMove(); |
| 7404 | |
| 7405 | // Release the drag pointer should perform drop. |
| 7406 | const MotionEvent secondFingerUpEvent = |
| 7407 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7408 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7409 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7410 | .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] | 7411 | .build(); |
| 7412 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7413 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 7414 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 7415 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7416 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 7417 | mWindow->assertNoEvents(); |
| 7418 | mSecondWindow->consumeMotionMove(); |
| 7419 | } |
| 7420 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7421 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7422 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7423 | |
| 7424 | // Update window of second display. |
| 7425 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7426 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7427 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 7428 | |
| 7429 | // Let second display has a touch state. |
| 7430 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7431 | injectMotionEvent(mDispatcher, |
| 7432 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7433 | AINPUT_SOURCE_TOUCHSCREEN) |
| 7434 | .displayId(SECOND_DISPLAY_ID) |
| 7435 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7436 | .x(100) |
| 7437 | .y(100)) |
| 7438 | .build())); |
| 7439 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7440 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7441 | // Update window again. |
| 7442 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 7443 | |
| 7444 | // Move on window. |
| 7445 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7446 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7447 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7448 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7449 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7450 | mWindow->consumeDragEvent(false, 50, 50); |
| 7451 | mSecondWindow->assertNoEvents(); |
| 7452 | |
| 7453 | // Move to another window. |
| 7454 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7455 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7456 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7457 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7458 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7459 | mWindow->consumeDragEvent(true, 150, 50); |
| 7460 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7461 | |
| 7462 | // drop to another window. |
| 7463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7464 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7465 | {150, 50})) |
| 7466 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7467 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7468 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7469 | mWindow->assertNoEvents(); |
| 7470 | mSecondWindow->assertNoEvents(); |
| 7471 | } |
| 7472 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7473 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 7474 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 7475 | // Move on window. |
| 7476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7477 | injectMotionEvent(mDispatcher, |
| 7478 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 7479 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7480 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7481 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7482 | .x(50) |
| 7483 | .y(50)) |
| 7484 | .build())) |
| 7485 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7486 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7487 | mWindow->consumeDragEvent(false, 50, 50); |
| 7488 | mSecondWindow->assertNoEvents(); |
| 7489 | |
| 7490 | // Move to another window. |
| 7491 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7492 | injectMotionEvent(mDispatcher, |
| 7493 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 7494 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7495 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7496 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7497 | .x(150) |
| 7498 | .y(50)) |
| 7499 | .build())) |
| 7500 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7501 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7502 | mWindow->consumeDragEvent(true, 150, 50); |
| 7503 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7504 | |
| 7505 | // drop to another window. |
| 7506 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7507 | injectMotionEvent(mDispatcher, |
| 7508 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 7509 | .buttonState(0) |
| 7510 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7511 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7512 | .x(150) |
| 7513 | .y(50)) |
| 7514 | .build())) |
| 7515 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7516 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7517 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7518 | mWindow->assertNoEvents(); |
| 7519 | mSecondWindow->assertNoEvents(); |
| 7520 | } |
| 7521 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7522 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 7523 | |
| 7524 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 7525 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7526 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7527 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7528 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7529 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7530 | window->setFocusable(true); |
| 7531 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7532 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7533 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7534 | |
| 7535 | // With the flag set, window should not get any input |
| 7536 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7537 | mDispatcher->notifyKey(&keyArgs); |
| 7538 | window->assertNoEvents(); |
| 7539 | |
| 7540 | NotifyMotionArgs motionArgs = |
| 7541 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7542 | ADISPLAY_ID_DEFAULT); |
| 7543 | mDispatcher->notifyMotion(&motionArgs); |
| 7544 | window->assertNoEvents(); |
| 7545 | |
| 7546 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7547 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7548 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7549 | |
| 7550 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7551 | mDispatcher->notifyKey(&keyArgs); |
| 7552 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7553 | |
| 7554 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7555 | ADISPLAY_ID_DEFAULT); |
| 7556 | mDispatcher->notifyMotion(&motionArgs); |
| 7557 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7558 | window->assertNoEvents(); |
| 7559 | } |
| 7560 | |
| 7561 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 7562 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7563 | std::make_shared<FakeApplicationHandle>(); |
| 7564 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7565 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7566 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7567 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7568 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7569 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7570 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7571 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7572 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7573 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7574 | window->setOwnerInfo(222, 222); |
| 7575 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7576 | window->setFocusable(true); |
| 7577 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7578 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7579 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7580 | |
| 7581 | // With the flag set, window should not get any input |
| 7582 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7583 | mDispatcher->notifyKey(&keyArgs); |
| 7584 | window->assertNoEvents(); |
| 7585 | |
| 7586 | NotifyMotionArgs motionArgs = |
| 7587 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7588 | ADISPLAY_ID_DEFAULT); |
| 7589 | mDispatcher->notifyMotion(&motionArgs); |
| 7590 | window->assertNoEvents(); |
| 7591 | |
| 7592 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7593 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7594 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7595 | |
| 7596 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7597 | mDispatcher->notifyKey(&keyArgs); |
| 7598 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7599 | |
| 7600 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7601 | ADISPLAY_ID_DEFAULT); |
| 7602 | mDispatcher->notifyMotion(&motionArgs); |
| 7603 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7604 | window->assertNoEvents(); |
| 7605 | } |
| 7606 | |
| 7607 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 7608 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7609 | std::make_shared<FakeApplicationHandle>(); |
| 7610 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7611 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7612 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7613 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7614 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7615 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7616 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7617 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7618 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7619 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7620 | window->setOwnerInfo(222, 222); |
| 7621 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7622 | window->setFocusable(true); |
| 7623 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7624 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7625 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7626 | |
| 7627 | // With the flag set, window should not get any input |
| 7628 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7629 | mDispatcher->notifyKey(&keyArgs); |
| 7630 | window->assertNoEvents(); |
| 7631 | |
| 7632 | NotifyMotionArgs motionArgs = |
| 7633 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7634 | ADISPLAY_ID_DEFAULT); |
| 7635 | mDispatcher->notifyMotion(&motionArgs); |
| 7636 | window->assertNoEvents(); |
| 7637 | |
| 7638 | // When the window is no longer obscured because it went on top, it should get input |
| 7639 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 7640 | |
| 7641 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7642 | mDispatcher->notifyKey(&keyArgs); |
| 7643 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7644 | |
| 7645 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7646 | ADISPLAY_ID_DEFAULT); |
| 7647 | mDispatcher->notifyMotion(&motionArgs); |
| 7648 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7649 | window->assertNoEvents(); |
| 7650 | } |
| 7651 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7652 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 7653 | protected: |
| 7654 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7655 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7656 | sp<FakeWindowHandle> mWindow; |
| 7657 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7658 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7659 | |
| 7660 | void SetUp() override { |
| 7661 | InputDispatcherTest::SetUp(); |
| 7662 | |
| 7663 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7664 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7665 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7666 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7667 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7668 | mSecondWindow = |
| 7669 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7670 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7671 | mThirdWindow = |
| 7672 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 7673 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 7674 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7675 | |
| 7676 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7677 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 7678 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 7679 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7680 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7681 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7682 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7683 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7684 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7685 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 7686 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7687 | mThirdWindow->assertNoEvents(); |
| 7688 | } |
| 7689 | |
| 7690 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 7691 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7692 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7693 | SECOND_DISPLAY_ID)) { |
| 7694 | mWindow->assertNoEvents(); |
| 7695 | mSecondWindow->assertNoEvents(); |
| 7696 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7697 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7698 | } |
| 7699 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7700 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 7701 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7702 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 7703 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7704 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 7705 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7706 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7707 | } |
| 7708 | }; |
| 7709 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7710 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7711 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7712 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 7713 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7714 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7715 | } |
| 7716 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7717 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 7718 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7719 | int32_t ownerPid = windowInfo.ownerPid; |
| 7720 | int32_t ownerUid = windowInfo.ownerUid; |
| 7721 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 7722 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7723 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7724 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7725 | mWindow->assertNoEvents(); |
| 7726 | mSecondWindow->assertNoEvents(); |
| 7727 | } |
| 7728 | |
| 7729 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 7730 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7731 | int32_t ownerPid = windowInfo.ownerPid; |
| 7732 | int32_t ownerUid = windowInfo.ownerUid; |
| 7733 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7734 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7735 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7736 | } |
| 7737 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7738 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7739 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7740 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 7741 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7742 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7743 | mWindow->assertNoEvents(); |
| 7744 | mSecondWindow->assertNoEvents(); |
| 7745 | } |
| 7746 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7747 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 7748 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 7749 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7750 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7751 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7752 | mWindow->assertNoEvents(); |
| 7753 | mSecondWindow->assertNoEvents(); |
| 7754 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 7755 | } |
| 7756 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7757 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 7758 | // Interact with the window first. |
| 7759 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 7760 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7761 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7762 | |
| 7763 | // Then remove focus. |
| 7764 | mWindow->setFocusable(false); |
| 7765 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7766 | |
| 7767 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 7768 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7769 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7770 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7771 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7772 | } |
| 7773 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7774 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 7775 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7776 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7777 | std::shared_ptr<FakeApplicationHandle> application = |
| 7778 | std::make_shared<FakeApplicationHandle>(); |
| 7779 | std::string name = "Fake Spy "; |
| 7780 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7781 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7782 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7783 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7784 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7785 | return spy; |
| 7786 | } |
| 7787 | |
| 7788 | sp<FakeWindowHandle> createForeground() { |
| 7789 | std::shared_ptr<FakeApplicationHandle> application = |
| 7790 | std::make_shared<FakeApplicationHandle>(); |
| 7791 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7792 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7793 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7794 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7795 | return window; |
| 7796 | } |
| 7797 | |
| 7798 | private: |
| 7799 | int mSpyCount{0}; |
| 7800 | }; |
| 7801 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7802 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7803 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7804 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 7805 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7806 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 7807 | ScopedSilentDeath _silentDeath; |
| 7808 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7809 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7810 | spy->setTrustedOverlay(false); |
| 7811 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 7812 | ".* not a trusted overlay"); |
| 7813 | } |
| 7814 | |
| 7815 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7816 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 7817 | */ |
| 7818 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7819 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7820 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 7821 | |
| 7822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7823 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7824 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7825 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7826 | } |
| 7827 | |
| 7828 | /** |
| 7829 | * Verify the order in which different input windows receive events. The touched foreground window |
| 7830 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 7831 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 7832 | * receive events before ones belows it. |
| 7833 | * |
| 7834 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 7835 | * spy1, spy2, window, spy3. |
| 7836 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 7837 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 7838 | * window. |
| 7839 | */ |
| 7840 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 7841 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7842 | auto spy1 = createSpy(); |
| 7843 | auto spy2 = createSpy(); |
| 7844 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7845 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 7846 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 7847 | const size_t numChannels = channels.size(); |
| 7848 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 7849 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7850 | if (!epollFd.ok()) { |
| 7851 | FAIL() << "Failed to create epoll fd"; |
| 7852 | } |
| 7853 | |
| 7854 | for (size_t i = 0; i < numChannels; i++) { |
| 7855 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 7856 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 7857 | FAIL() << "Failed to add fd to epoll"; |
| 7858 | } |
| 7859 | } |
| 7860 | |
| 7861 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7862 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7863 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7864 | |
| 7865 | std::vector<size_t> eventOrder; |
| 7866 | std::vector<struct epoll_event> events(numChannels); |
| 7867 | for (;;) { |
| 7868 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 7869 | (100ms).count()); |
| 7870 | if (nFds < 0) { |
| 7871 | FAIL() << "Failed to call epoll_wait"; |
| 7872 | } |
| 7873 | if (nFds == 0) { |
| 7874 | break; // epoll_wait timed out |
| 7875 | } |
| 7876 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 7877 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 7878 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7879 | channels[i]->consumeMotionDown(); |
| 7880 | } |
| 7881 | } |
| 7882 | |
| 7883 | // Verify the order in which the events were received. |
| 7884 | EXPECT_EQ(3u, eventOrder.size()); |
| 7885 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 7886 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 7887 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 7888 | } |
| 7889 | |
| 7890 | /** |
| 7891 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 7892 | */ |
| 7893 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 7894 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7895 | auto spy = createSpy(); |
| 7896 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7897 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7898 | |
| 7899 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7900 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7901 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7902 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7903 | spy->assertNoEvents(); |
| 7904 | } |
| 7905 | |
| 7906 | /** |
| 7907 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 7908 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 7909 | * to the window. |
| 7910 | */ |
| 7911 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 7912 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7913 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7914 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 7915 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7916 | |
| 7917 | // Inject an event outside the spy window's touchable region. |
| 7918 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7919 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7920 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7921 | window->consumeMotionDown(); |
| 7922 | spy->assertNoEvents(); |
| 7923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7924 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7925 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7926 | window->consumeMotionUp(); |
| 7927 | spy->assertNoEvents(); |
| 7928 | |
| 7929 | // Inject an event inside the spy window's touchable region. |
| 7930 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7931 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7932 | {5, 10})) |
| 7933 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7934 | window->consumeMotionDown(); |
| 7935 | spy->consumeMotionDown(); |
| 7936 | } |
| 7937 | |
| 7938 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7939 | * 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] | 7940 | * 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] | 7941 | */ |
| 7942 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 7943 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7944 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7945 | auto spy = createSpy(); |
| 7946 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7947 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7948 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 7949 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7950 | |
| 7951 | // Inject an event outside the spy window's frame and touchable region. |
| 7952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7953 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7954 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7955 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7956 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7957 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7958 | } |
| 7959 | |
| 7960 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7961 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 7962 | * pointers that are down within its bounds. |
| 7963 | */ |
| 7964 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 7965 | auto windowLeft = createForeground(); |
| 7966 | windowLeft->setFrame({0, 0, 100, 200}); |
| 7967 | auto windowRight = createForeground(); |
| 7968 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7969 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7970 | spy->setFrame({0, 0, 200, 200}); |
| 7971 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 7972 | |
| 7973 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7974 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7975 | {50, 50})) |
| 7976 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7977 | windowLeft->consumeMotionDown(); |
| 7978 | spy->consumeMotionDown(); |
| 7979 | |
| 7980 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7981 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7982 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7983 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7984 | .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] | 7985 | .build(); |
| 7986 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7987 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7988 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7989 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7990 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7991 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7992 | } |
| 7993 | |
| 7994 | /** |
| 7995 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 7996 | * the spy should receive the second pointer with ACTION_DOWN. |
| 7997 | */ |
| 7998 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 7999 | auto window = createForeground(); |
| 8000 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8001 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8002 | spyRight->setFrame({100, 0, 200, 200}); |
| 8003 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 8004 | |
| 8005 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8006 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8007 | {50, 50})) |
| 8008 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8009 | window->consumeMotionDown(); |
| 8010 | spyRight->assertNoEvents(); |
| 8011 | |
| 8012 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8013 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8014 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8015 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8016 | .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] | 8017 | .build(); |
| 8018 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8019 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8020 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8021 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8022 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8023 | spyRight->consumeMotionDown(); |
| 8024 | } |
| 8025 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8026 | /** |
| 8027 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 8028 | * windows should be allowed to control split touch. |
| 8029 | */ |
| 8030 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8031 | // 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] | 8032 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8033 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8034 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8035 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8036 | auto window = createForeground(); |
| 8037 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8038 | |
| 8039 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8040 | |
| 8041 | // First finger down, no window touched. |
| 8042 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8043 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8044 | {100, 200})) |
| 8045 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8046 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8047 | window->assertNoEvents(); |
| 8048 | |
| 8049 | // Second finger down on window, the window should receive touch down. |
| 8050 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8051 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8052 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8053 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8054 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 8055 | .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] | 8056 | .build(); |
| 8057 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8058 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8059 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8060 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8061 | |
| 8062 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8063 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8064 | } |
| 8065 | |
| 8066 | /** |
| 8067 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 8068 | * do not receive key events. |
| 8069 | */ |
| 8070 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8071 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8072 | spy->setFocusable(false); |
| 8073 | |
| 8074 | auto window = createForeground(); |
| 8075 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8076 | setFocusedWindow(window); |
| 8077 | window->consumeFocusEvent(true); |
| 8078 | |
| 8079 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 8080 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8081 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8082 | |
| 8083 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 8084 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8085 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8086 | |
| 8087 | spy->assertNoEvents(); |
| 8088 | } |
| 8089 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8090 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 8091 | |
| 8092 | /** |
| 8093 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 8094 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 8095 | */ |
| 8096 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 8097 | auto window = createForeground(); |
| 8098 | auto spy1 = createSpy(); |
| 8099 | auto spy2 = createSpy(); |
| 8100 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 8101 | |
| 8102 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8103 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8104 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8105 | window->consumeMotionDown(); |
| 8106 | spy1->consumeMotionDown(); |
| 8107 | spy2->consumeMotionDown(); |
| 8108 | |
| 8109 | // Pilfer pointers from the second spy window. |
| 8110 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 8111 | spy2->assertNoEvents(); |
| 8112 | spy1->consumeMotionCancel(); |
| 8113 | window->consumeMotionCancel(); |
| 8114 | |
| 8115 | // The rest of the gesture should only be sent to the second spy window. |
| 8116 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8117 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8118 | ADISPLAY_ID_DEFAULT)) |
| 8119 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8120 | spy2->consumeMotionMove(); |
| 8121 | spy1->assertNoEvents(); |
| 8122 | window->assertNoEvents(); |
| 8123 | } |
| 8124 | |
| 8125 | /** |
| 8126 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 8127 | * in the middle of the gesture. |
| 8128 | */ |
| 8129 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 8130 | auto window = createForeground(); |
| 8131 | auto spy = createSpy(); |
| 8132 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8133 | |
| 8134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8135 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8136 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8137 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8138 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8139 | |
| 8140 | window->releaseChannel(); |
| 8141 | |
| 8142 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8143 | |
| 8144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8145 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8146 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8147 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8148 | } |
| 8149 | |
| 8150 | /** |
| 8151 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 8152 | * the spy, but not to any other windows. |
| 8153 | */ |
| 8154 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 8155 | auto spy = createSpy(); |
| 8156 | auto window = createForeground(); |
| 8157 | |
| 8158 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8159 | |
| 8160 | // First finger down on the window and the spy. |
| 8161 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8162 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8163 | {100, 200})) |
| 8164 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8165 | spy->consumeMotionDown(); |
| 8166 | window->consumeMotionDown(); |
| 8167 | |
| 8168 | // Spy window pilfers the pointers. |
| 8169 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8170 | window->consumeMotionCancel(); |
| 8171 | |
| 8172 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 8173 | const MotionEvent secondFingerDownEvent = |
| 8174 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8175 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8176 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8177 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 8178 | .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] | 8179 | .build(); |
| 8180 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8181 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8182 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8183 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8184 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8185 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8186 | |
| 8187 | // Third finger goes down outside all windows, so injection should fail. |
| 8188 | const MotionEvent thirdFingerDownEvent = |
| 8189 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8190 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8191 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8192 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 8193 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8194 | .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] | 8195 | .build(); |
| 8196 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 8197 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8198 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8199 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8200 | |
| 8201 | spy->assertNoEvents(); |
| 8202 | window->assertNoEvents(); |
| 8203 | } |
| 8204 | |
| 8205 | /** |
| 8206 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 8207 | */ |
| 8208 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 8209 | auto spy = createSpy(); |
| 8210 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8211 | auto window = createForeground(); |
| 8212 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8213 | |
| 8214 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8215 | |
| 8216 | // First finger down on the window only |
| 8217 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8218 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8219 | {150, 150})) |
| 8220 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8221 | window->consumeMotionDown(); |
| 8222 | |
| 8223 | // Second finger down on the spy and window |
| 8224 | const MotionEvent secondFingerDownEvent = |
| 8225 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8226 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8227 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8228 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
| 8229 | .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] | 8230 | .build(); |
| 8231 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8232 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8233 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8234 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8235 | spy->consumeMotionDown(); |
| 8236 | window->consumeMotionPointerDown(1); |
| 8237 | |
| 8238 | // Third finger down on the spy and window |
| 8239 | const MotionEvent thirdFingerDownEvent = |
| 8240 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8241 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8242 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8243 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
| 8244 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8245 | .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] | 8246 | .build(); |
| 8247 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8248 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8249 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8250 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8251 | spy->consumeMotionPointerDown(1); |
| 8252 | window->consumeMotionPointerDown(2); |
| 8253 | |
| 8254 | // Spy window pilfers the pointers. |
| 8255 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8256 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8257 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8258 | |
| 8259 | spy->assertNoEvents(); |
| 8260 | window->assertNoEvents(); |
| 8261 | } |
| 8262 | |
| 8263 | /** |
| 8264 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 8265 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 8266 | * window, then that window should receive ACTION_CANCEL. |
| 8267 | */ |
| 8268 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 8269 | auto spy = createSpy(); |
| 8270 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8271 | auto window = createForeground(); |
| 8272 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8273 | |
| 8274 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8275 | |
| 8276 | // First finger down on both spy and window |
| 8277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8278 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8279 | {10, 10})) |
| 8280 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8281 | window->consumeMotionDown(); |
| 8282 | spy->consumeMotionDown(); |
| 8283 | |
| 8284 | // Second finger down on the spy and window |
| 8285 | const MotionEvent secondFingerDownEvent = |
| 8286 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8287 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8288 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8289 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8290 | .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] | 8291 | .build(); |
| 8292 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8293 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8294 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8295 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8296 | spy->consumeMotionPointerDown(1); |
| 8297 | window->consumeMotionPointerDown(1); |
| 8298 | |
| 8299 | // Spy window pilfers the pointers. |
| 8300 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8301 | window->consumeMotionCancel(); |
| 8302 | |
| 8303 | spy->assertNoEvents(); |
| 8304 | window->assertNoEvents(); |
| 8305 | } |
| 8306 | |
| 8307 | /** |
| 8308 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 8309 | * be sent to other windows |
| 8310 | */ |
| 8311 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 8312 | auto spy = createSpy(); |
| 8313 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8314 | auto window = createForeground(); |
| 8315 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8316 | |
| 8317 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8318 | |
| 8319 | // First finger down on both window and spy |
| 8320 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8321 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8322 | {10, 10})) |
| 8323 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8324 | window->consumeMotionDown(); |
| 8325 | spy->consumeMotionDown(); |
| 8326 | |
| 8327 | // Spy window pilfers the pointers. |
| 8328 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8329 | window->consumeMotionCancel(); |
| 8330 | |
| 8331 | // Second finger down on the window only |
| 8332 | const MotionEvent secondFingerDownEvent = |
| 8333 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8334 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8335 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8336 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8337 | .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] | 8338 | .build(); |
| 8339 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8340 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8341 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8342 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8343 | window->consumeMotionDown(); |
| 8344 | window->assertNoEvents(); |
| 8345 | |
| 8346 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 8347 | spy->consumeMotionMove(); |
| 8348 | spy->assertNoEvents(); |
| 8349 | } |
| 8350 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8351 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 8352 | public: |
| 8353 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 8354 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8355 | std::make_shared<FakeApplicationHandle>(); |
| 8356 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8357 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 8358 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8359 | overlay->setFocusable(false); |
| 8360 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8361 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8362 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8363 | overlay->setTrustedOverlay(true); |
| 8364 | |
| 8365 | std::shared_ptr<FakeApplicationHandle> application = |
| 8366 | std::make_shared<FakeApplicationHandle>(); |
| 8367 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8368 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 8369 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8370 | window->setFocusable(true); |
| 8371 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8372 | |
| 8373 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8374 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8375 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8376 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8377 | return {std::move(overlay), std::move(window)}; |
| 8378 | } |
| 8379 | |
| 8380 | void sendFingerEvent(int32_t action) { |
| 8381 | NotifyMotionArgs motionArgs = |
| 8382 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 8383 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 8384 | mDispatcher->notifyMotion(&motionArgs); |
| 8385 | } |
| 8386 | |
| 8387 | void sendStylusEvent(int32_t action) { |
| 8388 | NotifyMotionArgs motionArgs = |
| 8389 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 8390 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 8391 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 8392 | mDispatcher->notifyMotion(&motionArgs); |
| 8393 | } |
| 8394 | }; |
| 8395 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8396 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 8397 | |
| 8398 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 8399 | ScopedSilentDeath _silentDeath; |
| 8400 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8401 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8402 | overlay->setTrustedOverlay(false); |
| 8403 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 8404 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 8405 | ".* not a trusted overlay"); |
| 8406 | } |
| 8407 | |
| 8408 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 8409 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8410 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8411 | |
| 8412 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8413 | overlay->consumeMotionDown(); |
| 8414 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8415 | overlay->consumeMotionUp(); |
| 8416 | |
| 8417 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8418 | window->consumeMotionDown(); |
| 8419 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 8420 | window->consumeMotionUp(); |
| 8421 | |
| 8422 | overlay->assertNoEvents(); |
| 8423 | window->assertNoEvents(); |
| 8424 | } |
| 8425 | |
| 8426 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 8427 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8428 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8429 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8430 | |
| 8431 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8432 | overlay->consumeMotionDown(); |
| 8433 | window->consumeMotionDown(); |
| 8434 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8435 | overlay->consumeMotionUp(); |
| 8436 | window->consumeMotionUp(); |
| 8437 | |
| 8438 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8439 | window->consumeMotionDown(); |
| 8440 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 8441 | window->consumeMotionUp(); |
| 8442 | |
| 8443 | overlay->assertNoEvents(); |
| 8444 | window->assertNoEvents(); |
| 8445 | } |
| 8446 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 8447 | /** |
| 8448 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 8449 | * The scenario is as follows: |
| 8450 | * - The stylus interceptor overlay is configured as a spy window. |
| 8451 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 8452 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 8453 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 8454 | */ |
| 8455 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 8456 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8457 | overlay->setSpy(true); |
| 8458 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8459 | |
| 8460 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8461 | overlay->consumeMotionDown(); |
| 8462 | window->consumeMotionDown(); |
| 8463 | |
| 8464 | // The interceptor pilfers the pointers. |
| 8465 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 8466 | window->consumeMotionCancel(); |
| 8467 | |
| 8468 | // The interceptor configures itself so that it is no longer a spy. |
| 8469 | overlay->setSpy(false); |
| 8470 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8471 | |
| 8472 | // It continues to receive the rest of the stylus gesture. |
| 8473 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 8474 | overlay->consumeMotionMove(); |
| 8475 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8476 | overlay->consumeMotionUp(); |
| 8477 | |
| 8478 | window->assertNoEvents(); |
| 8479 | } |
| 8480 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8481 | struct User { |
| 8482 | int32_t mPid; |
| 8483 | int32_t mUid; |
| 8484 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 8485 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 8486 | |
| 8487 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 8488 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 8489 | |
| 8490 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 8491 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 8492 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 8493 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8494 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 8495 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8496 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 8497 | } |
| 8498 | |
| 8499 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8500 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8501 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8502 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8503 | mPolicyFlags); |
| 8504 | } |
| 8505 | |
| 8506 | sp<FakeWindowHandle> createWindow() const { |
| 8507 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8508 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8509 | sp<FakeWindowHandle> window = |
| 8510 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 8511 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8512 | window->setOwnerInfo(mPid, mUid); |
| 8513 | return window; |
| 8514 | } |
| 8515 | }; |
| 8516 | |
| 8517 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 8518 | |
| 8519 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 8520 | auto owner = User(mDispatcher, 10, 11); |
| 8521 | auto window = owner.createWindow(); |
| 8522 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8523 | |
| 8524 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8525 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8526 | window->consumeMotionDown(); |
| 8527 | |
| 8528 | setFocusedWindow(window); |
| 8529 | window->consumeFocusEvent(true); |
| 8530 | |
| 8531 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8532 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8533 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8534 | } |
| 8535 | |
| 8536 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 8537 | auto owner = User(mDispatcher, 10, 11); |
| 8538 | auto window = owner.createWindow(); |
| 8539 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8540 | |
| 8541 | auto rando = User(mDispatcher, 20, 21); |
| 8542 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8543 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8544 | |
| 8545 | setFocusedWindow(window); |
| 8546 | window->consumeFocusEvent(true); |
| 8547 | |
| 8548 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8549 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8550 | window->assertNoEvents(); |
| 8551 | } |
| 8552 | |
| 8553 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 8554 | auto owner = User(mDispatcher, 10, 11); |
| 8555 | auto window = owner.createWindow(); |
| 8556 | auto spy = owner.createWindow(); |
| 8557 | spy->setSpy(true); |
| 8558 | spy->setTrustedOverlay(true); |
| 8559 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8560 | |
| 8561 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8562 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8563 | spy->consumeMotionDown(); |
| 8564 | window->consumeMotionDown(); |
| 8565 | } |
| 8566 | |
| 8567 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 8568 | auto owner = User(mDispatcher, 10, 11); |
| 8569 | auto window = owner.createWindow(); |
| 8570 | |
| 8571 | auto rando = User(mDispatcher, 20, 21); |
| 8572 | auto randosSpy = rando.createWindow(); |
| 8573 | randosSpy->setSpy(true); |
| 8574 | randosSpy->setTrustedOverlay(true); |
| 8575 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8576 | |
| 8577 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 8578 | // not receive the event. |
| 8579 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8580 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8581 | randosSpy->assertNoEvents(); |
| 8582 | window->consumeMotionDown(); |
| 8583 | } |
| 8584 | |
| 8585 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 8586 | auto owner = User(mDispatcher, 10, 11); |
| 8587 | auto window = owner.createWindow(); |
| 8588 | |
| 8589 | auto rando = User(mDispatcher, 20, 21); |
| 8590 | auto randosSpy = rando.createWindow(); |
| 8591 | randosSpy->setSpy(true); |
| 8592 | randosSpy->setTrustedOverlay(true); |
| 8593 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8594 | |
| 8595 | // A user that has injection permission can inject into any window. |
| 8596 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8597 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8598 | ADISPLAY_ID_DEFAULT)); |
| 8599 | randosSpy->consumeMotionDown(); |
| 8600 | window->consumeMotionDown(); |
| 8601 | |
| 8602 | setFocusedWindow(randosSpy); |
| 8603 | randosSpy->consumeFocusEvent(true); |
| 8604 | |
| 8605 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 8606 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8607 | window->assertNoEvents(); |
| 8608 | } |
| 8609 | |
| 8610 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 8611 | auto owner = User(mDispatcher, 10, 11); |
| 8612 | auto window = owner.createWindow(); |
| 8613 | |
| 8614 | auto rando = User(mDispatcher, 20, 21); |
| 8615 | auto randosWindow = rando.createWindow(); |
| 8616 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 8617 | randosWindow->setWatchOutsideTouch(true); |
| 8618 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 8619 | |
| 8620 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 8621 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8622 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8623 | window->consumeMotionDown(); |
| 8624 | randosWindow->consumeMotionOutside(); |
| 8625 | } |
| 8626 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 8627 | } // namespace android::inputdispatcher |