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 | } |
| 163 | return arg.getX(0 /*pointerIndex*/) == x && arg.getY(0 /*pointerIndex*/) == y; |
| 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 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 549 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 638 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 647 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 677 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 678 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | << "Should reject motion events with 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 689 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 690 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 691 | << "Should reject motion events with pointer 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 701 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 702 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 703 | << "Should reject motion events with 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 713 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 725 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 726 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 727 | << "Should reject motion events with pointer 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 737 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 738 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | << "Should reject motion events with 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 748 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 761 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 773 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 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, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 787 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, InputEventInjectionSync::NONE, |
| 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; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 796 | NotifyConfigurationChangedArgs args(10 /*id*/, 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) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 804 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 805 | 2 /*switchMask*/); |
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) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 866 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -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, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1410 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 1411 | repeatCount, 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) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1422 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 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) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1430 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId, |
| 1431 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
| 1432 | /* allowKeyRepeat */ false); |
| 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) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1437 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 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) |
| 1593 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 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. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1618 | NotifyKeyArgs args(/* id */ 0, currentTime, 0 /*readTime*/, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 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. |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1647 | NotifyMotionArgs args(/* id */ 0, currentTime, 0 /*readTime*/, 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) { |
| 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)) |
| 1898 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1899 | .x(100) |
| 1900 | .y(100)) |
| 1901 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1902 | .x(150) |
| 1903 | .y(150)) |
| 1904 | .build(); |
| 1905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1906 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1907 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1908 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1909 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1910 | foregroundWindow->consumeMotionPointerDown(1 /* pointerIndex */); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1911 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1912 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1913 | |
| 1914 | const MotionEvent secondFingerUpEvent = |
| 1915 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1916 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1917 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1918 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1919 | .x(100) |
| 1920 | .y(100)) |
| 1921 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1922 | .x(150) |
| 1923 | .y(150)) |
| 1924 | .build(); |
| 1925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1926 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1927 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1928 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1929 | foregroundWindow->consumeMotionPointerUp(0); |
| 1930 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1931 | |
| 1932 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1933 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1934 | {100, 100})) |
| 1935 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1936 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1937 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | /** |
| 1941 | * Two windows: a window on the left and window on the right. |
| 1942 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1943 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1944 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1945 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1946 | * ACTION_POINTER_DOWN(1). |
| 1947 | */ |
| 1948 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1949 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1950 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1951 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1952 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1953 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1954 | |
| 1955 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1956 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1957 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1958 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1959 | |
| 1960 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1961 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1962 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1963 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1964 | |
| 1965 | mDispatcher->setInputWindows( |
| 1966 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1967 | |
| 1968 | // Touch down on left window |
| 1969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1970 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1971 | {100, 100})) |
| 1972 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1973 | |
| 1974 | // Both foreground window and its wallpaper should receive the touch down |
| 1975 | leftWindow->consumeMotionDown(); |
| 1976 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1977 | |
| 1978 | // Second finger down on the right window |
| 1979 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1980 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1981 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1982 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1983 | .x(100) |
| 1984 | .y(100)) |
| 1985 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1986 | .x(300) |
| 1987 | .y(100)) |
| 1988 | .build(); |
| 1989 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1990 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1991 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1992 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1993 | |
| 1994 | leftWindow->consumeMotionMove(); |
| 1995 | // Since the touch is split, right window gets ACTION_DOWN |
| 1996 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1997 | wallpaperWindow->consumeMotionPointerDown(1 /* pointerIndex */, ADISPLAY_ID_DEFAULT, |
| 1998 | expectedWallpaperFlags); |
| 1999 | |
| 2000 | // Now, leftWindow, which received the first finger, disappears. |
| 2001 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 2002 | leftWindow->consumeMotionCancel(); |
| 2003 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2004 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2005 | |
| 2006 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2007 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2008 | const MotionEvent secondFingerMoveEvent = |
| 2009 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2010 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2011 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2012 | .x(100) |
| 2013 | .y(100)) |
| 2014 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2015 | .x(310) |
| 2016 | .y(110)) |
| 2017 | .build(); |
| 2018 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2019 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2020 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2021 | rightWindow->consumeMotionMove(); |
| 2022 | |
| 2023 | leftWindow->assertNoEvents(); |
| 2024 | rightWindow->assertNoEvents(); |
| 2025 | wallpaperWindow->assertNoEvents(); |
| 2026 | } |
| 2027 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2028 | /** |
| 2029 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2030 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2031 | * The right window should receive ACTION_DOWN. |
| 2032 | */ |
| 2033 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2034 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2035 | sp<FakeWindowHandle> leftWindow = |
| 2036 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2037 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2038 | leftWindow->setDupTouchToWallpaper(true); |
| 2039 | leftWindow->setSlippery(true); |
| 2040 | |
| 2041 | sp<FakeWindowHandle> rightWindow = |
| 2042 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2043 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2044 | |
| 2045 | sp<FakeWindowHandle> wallpaperWindow = |
| 2046 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2047 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2048 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2049 | mDispatcher->setInputWindows( |
| 2050 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2051 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2052 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2053 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2054 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2055 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2056 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Both foreground window and its wallpaper should receive the touch down |
| 2059 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2060 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2061 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2062 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2063 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2064 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2065 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2066 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2067 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2068 | leftWindow->consumeMotionCancel(); |
| 2069 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2070 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2073 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame^] | 2074 | * Two windows: a window on the left and a window on the right. |
| 2075 | * Mouse is hovered from the right window into the left window. |
| 2076 | * Next, we tap on the left window, where the cursor was last seen. |
| 2077 | * The second tap is done onto the right window. |
| 2078 | * The mouse and tap are from two different devices. |
| 2079 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2080 | * explicitly helps during debugging. |
| 2081 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2082 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2083 | */ |
| 2084 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2085 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2086 | sp<FakeWindowHandle> leftWindow = |
| 2087 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2088 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2089 | |
| 2090 | sp<FakeWindowHandle> rightWindow = |
| 2091 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2092 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2093 | |
| 2094 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2095 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2096 | // stale. |
| 2097 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2098 | const int32_t mouseDeviceId = 6; |
| 2099 | const int32_t touchDeviceId = 4; |
| 2100 | // Move the cursor from right |
| 2101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2102 | injectMotionEvent(mDispatcher, |
| 2103 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2104 | AINPUT_SOURCE_MOUSE) |
| 2105 | .deviceId(mouseDeviceId) |
| 2106 | .downTime(baseTime + 10) |
| 2107 | .eventTime(baseTime + 20) |
| 2108 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2109 | .x(300) |
| 2110 | .y(100)) |
| 2111 | .build())); |
| 2112 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2113 | |
| 2114 | // .. to the left window |
| 2115 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2116 | injectMotionEvent(mDispatcher, |
| 2117 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2118 | AINPUT_SOURCE_MOUSE) |
| 2119 | .deviceId(mouseDeviceId) |
| 2120 | .downTime(baseTime + 10) |
| 2121 | .eventTime(baseTime + 30) |
| 2122 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2123 | .x(110) |
| 2124 | .y(100)) |
| 2125 | .build())); |
| 2126 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2127 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2128 | // Now tap the left window |
| 2129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2130 | injectMotionEvent(mDispatcher, |
| 2131 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2132 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2133 | .deviceId(touchDeviceId) |
| 2134 | .downTime(baseTime + 40) |
| 2135 | .eventTime(baseTime + 40) |
| 2136 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2137 | .x(100) |
| 2138 | .y(100)) |
| 2139 | .build())); |
| 2140 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2141 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2142 | |
| 2143 | // release tap |
| 2144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2145 | injectMotionEvent(mDispatcher, |
| 2146 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2147 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2148 | .deviceId(touchDeviceId) |
| 2149 | .downTime(baseTime + 40) |
| 2150 | .eventTime(baseTime + 50) |
| 2151 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2152 | .x(100) |
| 2153 | .y(100)) |
| 2154 | .build())); |
| 2155 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2156 | |
| 2157 | // Tap the window on the right |
| 2158 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2159 | injectMotionEvent(mDispatcher, |
| 2160 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2161 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2162 | .deviceId(touchDeviceId) |
| 2163 | .downTime(baseTime + 60) |
| 2164 | .eventTime(baseTime + 60) |
| 2165 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2166 | .x(300) |
| 2167 | .y(100)) |
| 2168 | .build())); |
| 2169 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2170 | |
| 2171 | // release tap |
| 2172 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2173 | injectMotionEvent(mDispatcher, |
| 2174 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2175 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2176 | .deviceId(touchDeviceId) |
| 2177 | .downTime(baseTime + 60) |
| 2178 | .eventTime(baseTime + 70) |
| 2179 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2180 | .x(300) |
| 2181 | .y(100)) |
| 2182 | .build())); |
| 2183 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2184 | |
| 2185 | // No more events |
| 2186 | leftWindow->assertNoEvents(); |
| 2187 | rightWindow->assertNoEvents(); |
| 2188 | } |
| 2189 | |
| 2190 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2191 | * On the display, have a single window, and also an area where there's no window. |
| 2192 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2193 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2194 | */ |
| 2195 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2196 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2197 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2198 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2199 | |
| 2200 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 2201 | NotifyMotionArgs args; |
| 2202 | |
| 2203 | // Touch down on the empty space |
| 2204 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 2205 | |
| 2206 | mDispatcher->waitForIdle(); |
| 2207 | window->assertNoEvents(); |
| 2208 | |
| 2209 | // Now touch down on the window with another pointer |
| 2210 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 2211 | mDispatcher->waitForIdle(); |
| 2212 | window->consumeMotionDown(); |
| 2213 | } |
| 2214 | |
| 2215 | /** |
| 2216 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2217 | * non-touchable window. |
| 2218 | */ |
| 2219 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2220 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2221 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2222 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2223 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2224 | window1->setTouchable(false); |
| 2225 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2226 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2227 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2228 | |
| 2229 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2230 | |
| 2231 | NotifyMotionArgs args; |
| 2232 | // Touch down on the non-touchable window |
| 2233 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2234 | |
| 2235 | mDispatcher->waitForIdle(); |
| 2236 | window1->assertNoEvents(); |
| 2237 | window2->assertNoEvents(); |
| 2238 | |
| 2239 | // Now touch down on the window with another pointer |
| 2240 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2241 | mDispatcher->waitForIdle(); |
| 2242 | window2->consumeMotionDown(); |
| 2243 | } |
| 2244 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2245 | /** |
| 2246 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2247 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2248 | */ |
| 2249 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2250 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2251 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2252 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2253 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2254 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2255 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2256 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2257 | |
| 2258 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2259 | |
| 2260 | NotifyMotionArgs args; |
| 2261 | // Touch down on the first window |
| 2262 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2263 | |
| 2264 | mDispatcher->waitForIdle(); |
| 2265 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2266 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2267 | window2->assertNoEvents(); |
| 2268 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2269 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2270 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2271 | |
| 2272 | // Now touch down on the window with another pointer |
| 2273 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2274 | mDispatcher->waitForIdle(); |
| 2275 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2276 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2277 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2278 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2279 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2280 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2281 | |
| 2282 | // Now move the pointer on the second window |
| 2283 | mDispatcher->notifyMotion( |
| 2284 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); |
| 2285 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2286 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2287 | |
| 2288 | // Now add new touch down on the second window |
| 2289 | mDispatcher->notifyMotion( |
| 2290 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); |
| 2291 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2292 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2293 | |
| 2294 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2295 | window1->consumeMotionMove(); |
| 2296 | window1->assertNoEvents(); |
| 2297 | |
| 2298 | // Now move the pointer on the first window |
| 2299 | mDispatcher->notifyMotion( |
| 2300 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}}))); |
| 2301 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2302 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2303 | |
| 2304 | mDispatcher->notifyMotion(&( |
| 2305 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); |
| 2306 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2307 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2310 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2311 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2312 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2313 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2314 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2315 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2316 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2317 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2318 | |
| 2319 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2320 | |
| 2321 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 2322 | |
| 2323 | // 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] | 2324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2325 | injectMotionEvent(mDispatcher, |
| 2326 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2327 | AINPUT_SOURCE_MOUSE) |
| 2328 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2329 | .x(900) |
| 2330 | .y(400)) |
| 2331 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2332 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2333 | |
| 2334 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2335 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2336 | injectMotionEvent(mDispatcher, |
| 2337 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2338 | AINPUT_SOURCE_MOUSE) |
| 2339 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2340 | .x(300) |
| 2341 | .y(400)) |
| 2342 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2343 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2344 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2345 | |
| 2346 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2347 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2348 | injectMotionEvent(mDispatcher, |
| 2349 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2350 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2351 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2352 | .x(300) |
| 2353 | .y(400)) |
| 2354 | .build())); |
| 2355 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2356 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2358 | injectMotionEvent(mDispatcher, |
| 2359 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2360 | AINPUT_SOURCE_MOUSE) |
| 2361 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2362 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2363 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2364 | .x(300) |
| 2365 | .y(400)) |
| 2366 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2367 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2368 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2369 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2370 | injectMotionEvent(mDispatcher, |
| 2371 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2372 | AINPUT_SOURCE_MOUSE) |
| 2373 | .buttonState(0) |
| 2374 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2375 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2376 | .x(300) |
| 2377 | .y(400)) |
| 2378 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2379 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2380 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2381 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2382 | injectMotionEvent(mDispatcher, |
| 2383 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2384 | .buttonState(0) |
| 2385 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2386 | .x(300) |
| 2387 | .y(400)) |
| 2388 | .build())); |
| 2389 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2390 | |
| 2391 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2392 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2393 | injectMotionEvent(mDispatcher, |
| 2394 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2395 | AINPUT_SOURCE_MOUSE) |
| 2396 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2397 | .x(900) |
| 2398 | .y(400)) |
| 2399 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2400 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2401 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2402 | |
| 2403 | // No more events |
| 2404 | windowLeft->assertNoEvents(); |
| 2405 | windowRight->assertNoEvents(); |
| 2406 | } |
| 2407 | |
| 2408 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 2409 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2410 | |
| 2411 | sp<FakeWindowHandle> spyWindow = |
| 2412 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2413 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2414 | spyWindow->setTrustedOverlay(true); |
| 2415 | spyWindow->setSpy(true); |
| 2416 | sp<FakeWindowHandle> window = |
| 2417 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2418 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2419 | |
| 2420 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2421 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2422 | |
| 2423 | // Send mouse cursor to the window |
| 2424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2425 | injectMotionEvent(mDispatcher, |
| 2426 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2427 | AINPUT_SOURCE_MOUSE) |
| 2428 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2429 | .x(100) |
| 2430 | .y(100)) |
| 2431 | .build())); |
| 2432 | |
| 2433 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2434 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2435 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2436 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2437 | |
| 2438 | window->assertNoEvents(); |
| 2439 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 2442 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 2443 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2444 | |
| 2445 | sp<FakeWindowHandle> spyWindow = |
| 2446 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2447 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 2448 | spyWindow->setTrustedOverlay(true); |
| 2449 | spyWindow->setSpy(true); |
| 2450 | sp<FakeWindowHandle> window = |
| 2451 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2452 | window->setFrame(Rect(0, 0, 600, 800)); |
| 2453 | |
| 2454 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2455 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2456 | |
| 2457 | // Send mouse cursor to the window |
| 2458 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2459 | injectMotionEvent(mDispatcher, |
| 2460 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2461 | AINPUT_SOURCE_MOUSE) |
| 2462 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2463 | .x(100) |
| 2464 | .y(100)) |
| 2465 | .build())); |
| 2466 | |
| 2467 | // Move mouse cursor |
| 2468 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2469 | injectMotionEvent(mDispatcher, |
| 2470 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2471 | AINPUT_SOURCE_MOUSE) |
| 2472 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2473 | .x(110) |
| 2474 | .y(110)) |
| 2475 | .build())); |
| 2476 | |
| 2477 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2478 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2479 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2480 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2481 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2482 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2483 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2484 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2485 | // Touch down on the window |
| 2486 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2487 | injectMotionEvent(mDispatcher, |
| 2488 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2489 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2490 | .deviceId(SECOND_DEVICE_ID) |
| 2491 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2492 | .x(200) |
| 2493 | .y(200)) |
| 2494 | .build())); |
| 2495 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2496 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2497 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2498 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 2499 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2500 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2501 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2502 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2503 | |
| 2504 | // pilfer the motion, retaining the gesture on the spy window. |
| 2505 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2506 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 2507 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2508 | |
| 2509 | // Touch UP on the window |
| 2510 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2511 | injectMotionEvent(mDispatcher, |
| 2512 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2513 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2514 | .deviceId(SECOND_DEVICE_ID) |
| 2515 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2516 | .x(200) |
| 2517 | .y(200)) |
| 2518 | .build())); |
| 2519 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2520 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2521 | |
| 2522 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 2523 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 2524 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 2525 | |
| 2526 | // One more tap - DOWN |
| 2527 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2528 | injectMotionEvent(mDispatcher, |
| 2529 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2530 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2531 | .deviceId(SECOND_DEVICE_ID) |
| 2532 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2533 | .x(250) |
| 2534 | .y(250)) |
| 2535 | .build())); |
| 2536 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2537 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2538 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2539 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2540 | |
| 2541 | // Touch UP on the window |
| 2542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2543 | injectMotionEvent(mDispatcher, |
| 2544 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2545 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2546 | .deviceId(SECOND_DEVICE_ID) |
| 2547 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2548 | .x(250) |
| 2549 | .y(250)) |
| 2550 | .build())); |
| 2551 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2552 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2553 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2554 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 2555 | |
| 2556 | window->assertNoEvents(); |
| 2557 | spyWindow->assertNoEvents(); |
| 2558 | } |
| 2559 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2560 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 2561 | // directly in this test. |
| 2562 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2563 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2564 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2565 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2566 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2567 | |
| 2568 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2569 | |
| 2570 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2571 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2572 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2573 | injectMotionEvent(mDispatcher, |
| 2574 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2575 | AINPUT_SOURCE_MOUSE) |
| 2576 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2577 | .x(300) |
| 2578 | .y(400)) |
| 2579 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2580 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2581 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2582 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2583 | injectMotionEvent(mDispatcher, |
| 2584 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2585 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2586 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2587 | .x(300) |
| 2588 | .y(400)) |
| 2589 | .build())); |
| 2590 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2591 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2592 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2593 | injectMotionEvent(mDispatcher, |
| 2594 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 2595 | AINPUT_SOURCE_MOUSE) |
| 2596 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2597 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2598 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2599 | .x(300) |
| 2600 | .y(400)) |
| 2601 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2602 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2603 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2604 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2605 | injectMotionEvent(mDispatcher, |
| 2606 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 2607 | AINPUT_SOURCE_MOUSE) |
| 2608 | .buttonState(0) |
| 2609 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2610 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2611 | .x(300) |
| 2612 | .y(400)) |
| 2613 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2614 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2615 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2617 | injectMotionEvent(mDispatcher, |
| 2618 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 2619 | .buttonState(0) |
| 2620 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2621 | .x(300) |
| 2622 | .y(400)) |
| 2623 | .build())); |
| 2624 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2625 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2627 | injectMotionEvent(mDispatcher, |
| 2628 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 2629 | AINPUT_SOURCE_MOUSE) |
| 2630 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2631 | .x(300) |
| 2632 | .y(400)) |
| 2633 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2634 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2637 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2638 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 2639 | * is generated. |
| 2640 | */ |
| 2641 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 2642 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2643 | sp<FakeWindowHandle> window = |
| 2644 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2645 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 2646 | |
| 2647 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2648 | |
| 2649 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2650 | |
| 2651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2652 | injectMotionEvent(mDispatcher, |
| 2653 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2654 | AINPUT_SOURCE_MOUSE) |
| 2655 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2656 | .x(300) |
| 2657 | .y(400)) |
| 2658 | .build())); |
| 2659 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2660 | |
| 2661 | // Remove the window, but keep the channel. |
| 2662 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 2663 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2664 | } |
| 2665 | |
| 2666 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2667 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2668 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 2669 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2670 | */ |
| 2671 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 2672 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2673 | sp<FakeWindowHandle> window = |
| 2674 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2675 | window->setFrame(Rect(0, 0, 100, 100)); |
| 2676 | |
| 2677 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2678 | |
| 2679 | // Inject a hover_move from mouse. |
| 2680 | NotifyMotionArgs motionArgs = |
| 2681 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 2682 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 2683 | motionArgs.xCursorPosition = 50; |
| 2684 | motionArgs.yCursorPosition = 50; |
| 2685 | mDispatcher->notifyMotion(&motionArgs); |
| 2686 | ASSERT_NO_FATAL_FAILURE( |
| 2687 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 2688 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2689 | |
| 2690 | // Tap on the window |
| 2691 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2692 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2693 | mDispatcher->notifyMotion(&motionArgs); |
| 2694 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 2695 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 2696 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 2697 | |
| 2698 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 2699 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 2700 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2701 | |
| 2702 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2703 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 2704 | mDispatcher->notifyMotion(&motionArgs); |
| 2705 | ASSERT_NO_FATAL_FAILURE( |
| 2706 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 2707 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 2708 | } |
| 2709 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2710 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 2711 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2712 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 2713 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 2714 | ADISPLAY_ID_DEFAULT); |
| 2715 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2716 | sp<FakeWindowHandle> windowSecondDisplay = |
| 2717 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 2718 | SECOND_DISPLAY_ID); |
| 2719 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 2720 | |
| 2721 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2722 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2723 | |
| 2724 | // Set cursor position in window in default display and check that hover enter and move |
| 2725 | // events are generated. |
| 2726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2727 | injectMotionEvent(mDispatcher, |
| 2728 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2729 | AINPUT_SOURCE_MOUSE) |
| 2730 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2731 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2732 | .x(300) |
| 2733 | .y(600)) |
| 2734 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2735 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2736 | |
| 2737 | // Remove all windows in secondary display and check that no event happens on window in |
| 2738 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2739 | mDispatcher->setInputWindows( |
| 2740 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2741 | windowDefaultDisplay->assertNoEvents(); |
| 2742 | |
| 2743 | // Move cursor position in window in default display and check that only hover move |
| 2744 | // event is generated and not hover enter event. |
| 2745 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 2746 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 2747 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2748 | injectMotionEvent(mDispatcher, |
| 2749 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2750 | AINPUT_SOURCE_MOUSE) |
| 2751 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2752 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2753 | .x(400) |
| 2754 | .y(700)) |
| 2755 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2756 | windowDefaultDisplay->consumeMotionEvent( |
| 2757 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 2758 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 2759 | windowDefaultDisplay->assertNoEvents(); |
| 2760 | } |
| 2761 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2762 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2763 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2764 | |
| 2765 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2766 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2767 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2768 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2769 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2770 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2771 | |
| 2772 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2773 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2774 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2775 | |
| 2776 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 2777 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2779 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2780 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2781 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 2782 | windowRight->assertNoEvents(); |
| 2783 | } |
| 2784 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2785 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2786 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2787 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2788 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2789 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2790 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2791 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2792 | setFocusedWindow(window); |
| 2793 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2794 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2795 | |
| 2796 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2797 | mDispatcher->notifyKey(&keyArgs); |
| 2798 | |
| 2799 | // Window should receive key down event. |
| 2800 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2801 | |
| 2802 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 2803 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2804 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2805 | mDispatcher->notifyDeviceReset(&args); |
| 2806 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2807 | AKEY_EVENT_FLAG_CANCELED); |
| 2808 | } |
| 2809 | |
| 2810 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2811 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2812 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2813 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2814 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2815 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2816 | |
| 2817 | NotifyMotionArgs motionArgs = |
| 2818 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2819 | ADISPLAY_ID_DEFAULT); |
| 2820 | mDispatcher->notifyMotion(&motionArgs); |
| 2821 | |
| 2822 | // Window should receive motion down event. |
| 2823 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2824 | |
| 2825 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 2826 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2827 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2828 | mDispatcher->notifyDeviceReset(&args); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 2829 | window->consumeMotionEvent( |
| 2830 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2831 | } |
| 2832 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2833 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 2834 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2835 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2836 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2837 | window->setFocusable(true); |
| 2838 | |
| 2839 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2840 | setFocusedWindow(window); |
| 2841 | |
| 2842 | window->consumeFocusEvent(true); |
| 2843 | |
| 2844 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2845 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 2846 | const nsecs_t injectTime = keyArgs.eventTime; |
| 2847 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 2848 | mDispatcher->notifyKey(&keyArgs); |
| 2849 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 2850 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2851 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 2852 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 2853 | } |
| 2854 | |
| 2855 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 2856 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2857 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2858 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 2859 | window->setFocusable(true); |
| 2860 | |
| 2861 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2862 | setFocusedWindow(window); |
| 2863 | |
| 2864 | window->consumeFocusEvent(true); |
| 2865 | |
| 2866 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2867 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2868 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 2869 | mDispatcher->notifyKey(&keyDown); |
| 2870 | mDispatcher->notifyKey(&keyUp); |
| 2871 | |
| 2872 | // Window should receive key event immediately when same key up. |
| 2873 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2874 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2875 | } |
| 2876 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 2877 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 2878 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 2879 | * WATCH_OUTSIDE_TOUCH. |
| 2880 | * Both windows are owned by the same UID. |
| 2881 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 2882 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 2883 | */ |
| 2884 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 2885 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2886 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2887 | "First Window", ADISPLAY_ID_DEFAULT); |
| 2888 | window->setFrame(Rect{0, 0, 100, 100}); |
| 2889 | |
| 2890 | sp<FakeWindowHandle> outsideWindow = |
| 2891 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2892 | ADISPLAY_ID_DEFAULT); |
| 2893 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 2894 | outsideWindow->setWatchOutsideTouch(true); |
| 2895 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 2896 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 2897 | |
| 2898 | // Tap on first window. |
| 2899 | NotifyMotionArgs motionArgs = |
| 2900 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2901 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 2902 | mDispatcher->notifyMotion(&motionArgs); |
| 2903 | window->consumeMotionDown(); |
| 2904 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 2905 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 2906 | outsideWindow->consumeMotionEvent( |
| 2907 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 2908 | } |
| 2909 | |
| 2910 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2911 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 2912 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 2913 | * ACTION_OUTSIDE event is sent per gesture. |
| 2914 | */ |
| 2915 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 2916 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 2917 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2918 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2919 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2920 | window->setWatchOutsideTouch(true); |
| 2921 | window->setFrame(Rect{0, 0, 100, 100}); |
| 2922 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2923 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 2924 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2925 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 2926 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2927 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 2928 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2929 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 2930 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 2931 | |
| 2932 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 2933 | NotifyMotionArgs motionArgs = |
| 2934 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2935 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 2936 | mDispatcher->notifyMotion(&motionArgs); |
| 2937 | window->assertNoEvents(); |
| 2938 | secondWindow->assertNoEvents(); |
| 2939 | |
| 2940 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 2941 | // Now, `window` should get ACTION_OUTSIDE. |
| 2942 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2943 | {PointF{-10, -10}, PointF{105, 105}}); |
| 2944 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 2945 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 2946 | window->consumeMotionEvent( |
| 2947 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 2948 | secondWindow->consumeMotionDown(); |
| 2949 | thirdWindow->assertNoEvents(); |
| 2950 | |
| 2951 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 2952 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 2953 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2954 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 2955 | mDispatcher->notifyMotion(&motionArgs); |
| 2956 | window->assertNoEvents(); |
| 2957 | secondWindow->consumeMotionMove(); |
| 2958 | thirdWindow->consumeMotionDown(); |
| 2959 | } |
| 2960 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2961 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 2962 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2963 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2964 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 2965 | window->setFocusable(true); |
| 2966 | |
| 2967 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 2968 | setFocusedWindow(window); |
| 2969 | |
| 2970 | window->consumeFocusEvent(true); |
| 2971 | |
| 2972 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 2973 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 2974 | mDispatcher->notifyKey(&keyDown); |
| 2975 | mDispatcher->notifyKey(&keyUp); |
| 2976 | |
| 2977 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2978 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 2979 | |
| 2980 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
| 2981 | mDispatcher->onWindowInfosChanged({}, {}); |
| 2982 | |
| 2983 | window->consumeFocusEvent(false); |
| 2984 | |
| 2985 | mDispatcher->notifyKey(&keyDown); |
| 2986 | mDispatcher->notifyKey(&keyUp); |
| 2987 | window->assertNoEvents(); |
| 2988 | } |
| 2989 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 2990 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 2991 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2992 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2993 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2994 | // Ensure window is non-split and have some transform. |
| 2995 | window->setPreventSplitting(true); |
| 2996 | window->setWindowOffset(20, 40); |
| 2997 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 2998 | |
| 2999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3000 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3001 | {50, 50})) |
| 3002 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3003 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3004 | |
| 3005 | const MotionEvent secondFingerDownEvent = |
| 3006 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3007 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3008 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 3009 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 3010 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 3011 | .x(-30) |
| 3012 | .y(-50)) |
| 3013 | .build(); |
| 3014 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3015 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3016 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3017 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3018 | |
| 3019 | const MotionEvent* event = window->consumeMotion(); |
| 3020 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3021 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3022 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3023 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3024 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3025 | } |
| 3026 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3027 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3028 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 3029 | * |
| 3030 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 3031 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 3032 | * space. |
| 3033 | */ |
| 3034 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 3035 | public: |
| 3036 | void SetUp() override { |
| 3037 | InputDispatcherTest::SetUp(); |
| 3038 | mDisplayInfos.clear(); |
| 3039 | mWindowInfos.clear(); |
| 3040 | } |
| 3041 | |
| 3042 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 3043 | gui::DisplayInfo info; |
| 3044 | info.displayId = displayId; |
| 3045 | info.transform = transform; |
| 3046 | mDisplayInfos.push_back(std::move(info)); |
| 3047 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3048 | } |
| 3049 | |
| 3050 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 3051 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 3052 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3053 | } |
| 3054 | |
| 3055 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 3056 | // on the display. |
| 3057 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 3058 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 3059 | // respectively. |
| 3060 | ui::Transform displayTransform; |
| 3061 | displayTransform.set(2, 0, 0, 4); |
| 3062 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 3063 | |
| 3064 | std::shared_ptr<FakeApplicationHandle> application = |
| 3065 | std::make_shared<FakeApplicationHandle>(); |
| 3066 | |
| 3067 | // Add two windows to the display. Their frames are represented in the display space. |
| 3068 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3069 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3070 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3071 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 3072 | addWindow(firstWindow); |
| 3073 | |
| 3074 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3075 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3076 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3077 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 3078 | addWindow(secondWindow); |
| 3079 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 3080 | } |
| 3081 | |
| 3082 | private: |
| 3083 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 3084 | std::vector<gui::WindowInfo> mWindowInfos; |
| 3085 | }; |
| 3086 | |
| 3087 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 3088 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3089 | // Send down to the first window. The point is represented in the display space. The point is |
| 3090 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 3091 | // end up in the incorrect window. |
| 3092 | NotifyMotionArgs downMotionArgs = |
| 3093 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3094 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 3095 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3096 | |
| 3097 | firstWindow->consumeMotionDown(); |
| 3098 | secondWindow->assertNoEvents(); |
| 3099 | } |
| 3100 | |
| 3101 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 3102 | // the event should be treated as being in the logical display space. |
| 3103 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 3104 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3105 | // Send down to the first window. The point is represented in the logical display space. The |
| 3106 | // point is selected so that if the hit test was done in logical display space, then it would |
| 3107 | // end up in the incorrect window. |
| 3108 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3109 | PointF{75 * 2, 55 * 4}); |
| 3110 | |
| 3111 | firstWindow->consumeMotionDown(); |
| 3112 | secondWindow->assertNoEvents(); |
| 3113 | } |
| 3114 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3115 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 3116 | // event should be treated as being in the logical display space. |
| 3117 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 3118 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3119 | |
| 3120 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 3121 | ui::Transform injectedEventTransform; |
| 3122 | injectedEventTransform.set(matrix); |
| 3123 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 3124 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 3125 | |
| 3126 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3127 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3128 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 3129 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 3130 | .x(untransformedPoint.x) |
| 3131 | .y(untransformedPoint.y)) |
| 3132 | .build(); |
| 3133 | event.transform(matrix); |
| 3134 | |
| 3135 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 3136 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 3137 | |
| 3138 | firstWindow->consumeMotionDown(); |
| 3139 | secondWindow->assertNoEvents(); |
| 3140 | } |
| 3141 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3142 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 3143 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3144 | |
| 3145 | // Send down to the second window. |
| 3146 | NotifyMotionArgs downMotionArgs = |
| 3147 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3148 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 3149 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3150 | |
| 3151 | firstWindow->assertNoEvents(); |
| 3152 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 3153 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 3154 | |
| 3155 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 3156 | EXPECT_EQ(300, event->getRawX(0)); |
| 3157 | EXPECT_EQ(880, event->getRawY(0)); |
| 3158 | |
| 3159 | // Ensure that the x and y values are in the window's coordinate space. |
| 3160 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 3161 | // the logical display space. This will be the origin of the window space. |
| 3162 | EXPECT_EQ(100, event->getX(0)); |
| 3163 | EXPECT_EQ(80, event->getY(0)); |
| 3164 | } |
| 3165 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3166 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3167 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3168 | |
| 3169 | class TransferTouchFixture : public InputDispatcherTest, |
| 3170 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 3171 | |
| 3172 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3173 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3174 | |
| 3175 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3176 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3177 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3178 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3179 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3180 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3181 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3182 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3183 | sp<FakeWindowHandle> wallpaper = |
| 3184 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 3185 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3186 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3187 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3188 | |
| 3189 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3190 | NotifyMotionArgs downMotionArgs = |
| 3191 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3192 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3193 | mDispatcher->notifyMotion(&downMotionArgs); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3194 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3195 | // Only the first window should get the down event |
| 3196 | firstWindow->consumeMotionDown(); |
| 3197 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3198 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3199 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3200 | // Transfer touch to the second window |
| 3201 | TransferFunction f = GetParam(); |
| 3202 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3203 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3204 | // The first window gets cancel and the second gets down |
| 3205 | firstWindow->consumeMotionCancel(); |
| 3206 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3207 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3208 | |
| 3209 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3210 | NotifyMotionArgs upMotionArgs = |
| 3211 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3212 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3213 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3214 | // The first window gets no events and the second gets up |
| 3215 | firstWindow->assertNoEvents(); |
| 3216 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3217 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3218 | } |
| 3219 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3220 | /** |
| 3221 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 3222 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 3223 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 3224 | * natural to the user. |
| 3225 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 3226 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 3227 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 3228 | * the first window to the second. |
| 3229 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 3230 | * the other API, as well. |
| 3231 | */ |
| 3232 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 3233 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3234 | |
| 3235 | // Create a couple of windows + a spy window |
| 3236 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3237 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3238 | spyWindow->setTrustedOverlay(true); |
| 3239 | spyWindow->setSpy(true); |
| 3240 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3241 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3242 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3243 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3244 | |
| 3245 | // Add the windows to the dispatcher |
| 3246 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 3247 | |
| 3248 | // Send down to the first window |
| 3249 | NotifyMotionArgs downMotionArgs = |
| 3250 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3251 | ADISPLAY_ID_DEFAULT); |
| 3252 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3253 | // Only the first window and spy should get the down event |
| 3254 | spyWindow->consumeMotionDown(); |
| 3255 | firstWindow->consumeMotionDown(); |
| 3256 | |
| 3257 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 3258 | // if f === 'transferTouch'. |
| 3259 | TransferFunction f = GetParam(); |
| 3260 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3261 | ASSERT_TRUE(success); |
| 3262 | // The first window gets cancel and the second gets down |
| 3263 | firstWindow->consumeMotionCancel(); |
| 3264 | secondWindow->consumeMotionDown(); |
| 3265 | |
| 3266 | // Send up event to the second window |
| 3267 | NotifyMotionArgs upMotionArgs = |
| 3268 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3269 | ADISPLAY_ID_DEFAULT); |
| 3270 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3271 | // The first window gets no events and the second+spy get up |
| 3272 | firstWindow->assertNoEvents(); |
| 3273 | spyWindow->consumeMotionUp(); |
| 3274 | secondWindow->consumeMotionUp(); |
| 3275 | } |
| 3276 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3277 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3278 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3279 | |
| 3280 | PointF touchPoint = {10, 10}; |
| 3281 | |
| 3282 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3283 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3284 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3285 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3286 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3287 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3288 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3289 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 3290 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3291 | |
| 3292 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3293 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3294 | |
| 3295 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3296 | NotifyMotionArgs downMotionArgs = |
| 3297 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3298 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3299 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3300 | // Only the first window should get the down event |
| 3301 | firstWindow->consumeMotionDown(); |
| 3302 | secondWindow->assertNoEvents(); |
| 3303 | |
| 3304 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3305 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3306 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3307 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3308 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 3309 | // Only the first window should get the pointer down event |
| 3310 | firstWindow->consumeMotionPointerDown(1); |
| 3311 | secondWindow->assertNoEvents(); |
| 3312 | |
| 3313 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3314 | TransferFunction f = GetParam(); |
| 3315 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3316 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3317 | // The first window gets cancel and the second gets down and pointer down |
| 3318 | firstWindow->consumeMotionCancel(); |
| 3319 | secondWindow->consumeMotionDown(); |
| 3320 | secondWindow->consumeMotionPointerDown(1); |
| 3321 | |
| 3322 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3323 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3324 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3325 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3326 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3327 | // The first window gets nothing and the second gets pointer up |
| 3328 | firstWindow->assertNoEvents(); |
| 3329 | secondWindow->consumeMotionPointerUp(1); |
| 3330 | |
| 3331 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3332 | NotifyMotionArgs upMotionArgs = |
| 3333 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3334 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3335 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3336 | // The first window gets nothing and the second gets up |
| 3337 | firstWindow->assertNoEvents(); |
| 3338 | secondWindow->consumeMotionUp(); |
| 3339 | } |
| 3340 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3341 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 3342 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3343 | |
| 3344 | // Create a couple of windows |
| 3345 | sp<FakeWindowHandle> firstWindow = |
| 3346 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3347 | ADISPLAY_ID_DEFAULT); |
| 3348 | firstWindow->setDupTouchToWallpaper(true); |
| 3349 | sp<FakeWindowHandle> secondWindow = |
| 3350 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3351 | ADISPLAY_ID_DEFAULT); |
| 3352 | secondWindow->setDupTouchToWallpaper(true); |
| 3353 | |
| 3354 | sp<FakeWindowHandle> wallpaper1 = |
| 3355 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 3356 | wallpaper1->setIsWallpaper(true); |
| 3357 | |
| 3358 | sp<FakeWindowHandle> wallpaper2 = |
| 3359 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 3360 | wallpaper2->setIsWallpaper(true); |
| 3361 | // Add the windows to the dispatcher |
| 3362 | mDispatcher->setInputWindows( |
| 3363 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 3364 | |
| 3365 | // Send down to the first window |
| 3366 | NotifyMotionArgs downMotionArgs = |
| 3367 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3368 | ADISPLAY_ID_DEFAULT); |
| 3369 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3370 | |
| 3371 | // Only the first window should get the down event |
| 3372 | firstWindow->consumeMotionDown(); |
| 3373 | secondWindow->assertNoEvents(); |
| 3374 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3375 | wallpaper2->assertNoEvents(); |
| 3376 | |
| 3377 | // Transfer touch focus to the second window |
| 3378 | TransferFunction f = GetParam(); |
| 3379 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3380 | ASSERT_TRUE(success); |
| 3381 | |
| 3382 | // The first window gets cancel and the second gets down |
| 3383 | firstWindow->consumeMotionCancel(); |
| 3384 | secondWindow->consumeMotionDown(); |
| 3385 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3386 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3387 | |
| 3388 | // Send up event to the second window |
| 3389 | NotifyMotionArgs upMotionArgs = |
| 3390 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3391 | ADISPLAY_ID_DEFAULT); |
| 3392 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3393 | // The first window gets no events and the second gets up |
| 3394 | firstWindow->assertNoEvents(); |
| 3395 | secondWindow->consumeMotionUp(); |
| 3396 | wallpaper1->assertNoEvents(); |
| 3397 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 3398 | } |
| 3399 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3400 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 3401 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 3402 | // for the case where there are multiple pointers split across several windows. |
| 3403 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 3404 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3405 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3406 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3407 | return dispatcher->transferTouch(destChannelToken, |
| 3408 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3409 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3410 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3411 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3412 | return dispatcher->transferTouchFocus(from, to, |
| 3413 | false /*isDragAndDrop*/); |
| 3414 | })); |
| 3415 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3416 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3417 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3418 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3419 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3420 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3421 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3422 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3423 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3424 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3425 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3426 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3427 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3428 | |
| 3429 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3430 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3431 | |
| 3432 | PointF pointInFirst = {300, 200}; |
| 3433 | PointF pointInSecond = {300, 600}; |
| 3434 | |
| 3435 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3436 | NotifyMotionArgs firstDownMotionArgs = |
| 3437 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3438 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3439 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3440 | // Only the first window should get the down event |
| 3441 | firstWindow->consumeMotionDown(); |
| 3442 | secondWindow->assertNoEvents(); |
| 3443 | |
| 3444 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3445 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3446 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3447 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3448 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3449 | // The first window gets a move and the second a down |
| 3450 | firstWindow->consumeMotionMove(); |
| 3451 | secondWindow->consumeMotionDown(); |
| 3452 | |
| 3453 | // Transfer touch focus to the second window |
| 3454 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 3455 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 3456 | firstWindow->consumeMotionCancel(); |
| 3457 | secondWindow->consumeMotionPointerDown(1); |
| 3458 | |
| 3459 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3460 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3461 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3462 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3463 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3464 | // The first window gets nothing and the second gets pointer up |
| 3465 | firstWindow->assertNoEvents(); |
| 3466 | secondWindow->consumeMotionPointerUp(1); |
| 3467 | |
| 3468 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3469 | NotifyMotionArgs upMotionArgs = |
| 3470 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3471 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3472 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3473 | // The first window gets nothing and the second gets up |
| 3474 | firstWindow->assertNoEvents(); |
| 3475 | secondWindow->consumeMotionUp(); |
| 3476 | } |
| 3477 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3478 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 3479 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 3480 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 3481 | // window should get nothing. |
| 3482 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 3483 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3484 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3485 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3486 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3487 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3488 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3489 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3490 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3491 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3492 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3493 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3494 | |
| 3495 | // Add the windows to the dispatcher |
| 3496 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3497 | |
| 3498 | PointF pointInFirst = {300, 200}; |
| 3499 | PointF pointInSecond = {300, 600}; |
| 3500 | |
| 3501 | // Send down to the first window |
| 3502 | NotifyMotionArgs firstDownMotionArgs = |
| 3503 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3504 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3505 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3506 | // Only the first window should get the down event |
| 3507 | firstWindow->consumeMotionDown(); |
| 3508 | secondWindow->assertNoEvents(); |
| 3509 | |
| 3510 | // Send down to the second window |
| 3511 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3512 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3513 | {pointInFirst, pointInSecond}); |
| 3514 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3515 | // The first window gets a move and the second a down |
| 3516 | firstWindow->consumeMotionMove(); |
| 3517 | secondWindow->consumeMotionDown(); |
| 3518 | |
| 3519 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3520 | const bool transferred = |
| 3521 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3522 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 3523 | ASSERT_FALSE(transferred); |
| 3524 | firstWindow->assertNoEvents(); |
| 3525 | secondWindow->assertNoEvents(); |
| 3526 | |
| 3527 | // The rest of the dispatch should proceed as normal |
| 3528 | // Send pointer up to the second window |
| 3529 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3530 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3531 | {pointInFirst, pointInSecond}); |
| 3532 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3533 | // The first window gets MOVE and the second gets pointer up |
| 3534 | firstWindow->consumeMotionMove(); |
| 3535 | secondWindow->consumeMotionUp(); |
| 3536 | |
| 3537 | // Send up event to the first window |
| 3538 | NotifyMotionArgs upMotionArgs = |
| 3539 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3540 | ADISPLAY_ID_DEFAULT); |
| 3541 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3542 | // The first window gets nothing and the second gets up |
| 3543 | firstWindow->consumeMotionUp(); |
| 3544 | secondWindow->assertNoEvents(); |
| 3545 | } |
| 3546 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3547 | // This case will create two windows and one mirrored window on the default display and mirror |
| 3548 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 3549 | // the windows info of second display before default display. |
| 3550 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 3551 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3552 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3553 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3554 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3555 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3556 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3557 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3558 | |
| 3559 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3560 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3561 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3562 | |
| 3563 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3564 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3565 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3566 | |
| 3567 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3568 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3569 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3570 | |
| 3571 | // Update window info, let it find window handle of second display first. |
| 3572 | mDispatcher->setInputWindows( |
| 3573 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3574 | {ADISPLAY_ID_DEFAULT, |
| 3575 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3576 | |
| 3577 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3578 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3579 | {50, 50})) |
| 3580 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3581 | |
| 3582 | // Window should receive motion event. |
| 3583 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3584 | |
| 3585 | // Transfer touch focus |
| 3586 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 3587 | secondWindowInPrimary->getToken())); |
| 3588 | // The first window gets cancel. |
| 3589 | firstWindowInPrimary->consumeMotionCancel(); |
| 3590 | secondWindowInPrimary->consumeMotionDown(); |
| 3591 | |
| 3592 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3593 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3594 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 3595 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3596 | firstWindowInPrimary->assertNoEvents(); |
| 3597 | secondWindowInPrimary->consumeMotionMove(); |
| 3598 | |
| 3599 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3600 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3601 | {150, 50})) |
| 3602 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3603 | firstWindowInPrimary->assertNoEvents(); |
| 3604 | secondWindowInPrimary->consumeMotionUp(); |
| 3605 | } |
| 3606 | |
| 3607 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 3608 | // 'transferTouch' api. |
| 3609 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 3610 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3611 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3612 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3613 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3614 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3615 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3616 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3617 | |
| 3618 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 3619 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 3620 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3621 | |
| 3622 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 3623 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3624 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3625 | |
| 3626 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 3627 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 3628 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3629 | |
| 3630 | // Update window info, let it find window handle of second display first. |
| 3631 | mDispatcher->setInputWindows( |
| 3632 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 3633 | {ADISPLAY_ID_DEFAULT, |
| 3634 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 3635 | |
| 3636 | // Touch on second display. |
| 3637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3638 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 3639 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3640 | |
| 3641 | // Window should receive motion event. |
| 3642 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3643 | |
| 3644 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 3645 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 3646 | |
| 3647 | // The first window gets cancel. |
| 3648 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 3649 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 3650 | |
| 3651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3652 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3653 | SECOND_DISPLAY_ID, {150, 50})) |
| 3654 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3655 | firstWindowInPrimary->assertNoEvents(); |
| 3656 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 3657 | |
| 3658 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3659 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 3660 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3661 | firstWindowInPrimary->assertNoEvents(); |
| 3662 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 3663 | } |
| 3664 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3665 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3666 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3667 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3668 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3669 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3670 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3671 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3672 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3673 | |
| 3674 | window->consumeFocusEvent(true); |
| 3675 | |
| 3676 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3677 | mDispatcher->notifyKey(&keyArgs); |
| 3678 | |
| 3679 | // Window should receive key down event. |
| 3680 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3681 | } |
| 3682 | |
| 3683 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3684 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3685 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3686 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3687 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3688 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3689 | |
| 3690 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3691 | mDispatcher->notifyKey(&keyArgs); |
| 3692 | mDispatcher->waitForIdle(); |
| 3693 | |
| 3694 | window->assertNoEvents(); |
| 3695 | } |
| 3696 | |
| 3697 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 3698 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3699 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3700 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3701 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3702 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3703 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3704 | |
| 3705 | // Send key |
| 3706 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3707 | mDispatcher->notifyKey(&keyArgs); |
| 3708 | // Send motion |
| 3709 | NotifyMotionArgs motionArgs = |
| 3710 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3711 | ADISPLAY_ID_DEFAULT); |
| 3712 | mDispatcher->notifyMotion(&motionArgs); |
| 3713 | |
| 3714 | // Window should receive only the motion event |
| 3715 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3716 | window->assertNoEvents(); // Key event or focus event will not be received |
| 3717 | } |
| 3718 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3719 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 3720 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3721 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3722 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3723 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3724 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3725 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3726 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3727 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3728 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3729 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3730 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3731 | |
| 3732 | // Add the windows to the dispatcher |
| 3733 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 3734 | |
| 3735 | PointF pointInFirst = {300, 200}; |
| 3736 | PointF pointInSecond = {300, 600}; |
| 3737 | |
| 3738 | // Send down to the first window |
| 3739 | NotifyMotionArgs firstDownMotionArgs = |
| 3740 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3741 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 3742 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 3743 | // Only the first window should get the down event |
| 3744 | firstWindow->consumeMotionDown(); |
| 3745 | secondWindow->assertNoEvents(); |
| 3746 | |
| 3747 | // Send down to the second window |
| 3748 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3749 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3750 | {pointInFirst, pointInSecond}); |
| 3751 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 3752 | // The first window gets a move and the second a down |
| 3753 | firstWindow->consumeMotionMove(); |
| 3754 | secondWindow->consumeMotionDown(); |
| 3755 | |
| 3756 | // Send pointer cancel to the second window |
| 3757 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3758 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 3759 | {pointInFirst, pointInSecond}); |
| 3760 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 3761 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 3762 | // The first window gets move and the second gets cancel. |
| 3763 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3764 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 3765 | |
| 3766 | // Send up event. |
| 3767 | NotifyMotionArgs upMotionArgs = |
| 3768 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3769 | ADISPLAY_ID_DEFAULT); |
| 3770 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3771 | // The first window gets up and the second gets nothing. |
| 3772 | firstWindow->consumeMotionUp(); |
| 3773 | secondWindow->assertNoEvents(); |
| 3774 | } |
| 3775 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3776 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 3777 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3778 | |
| 3779 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3780 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 3781 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3782 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 3783 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 3784 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 3785 | |
| 3786 | window->sendTimeline(1 /*inputEventId*/, graphicsTimeline); |
| 3787 | window->assertNoEvents(); |
| 3788 | mDispatcher->waitForIdle(); |
| 3789 | } |
| 3790 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3791 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3792 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3793 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3794 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3795 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 3796 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 3797 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3800 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 3801 | |
| 3802 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3803 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 3804 | expectedDisplayId, expectedFlags); |
| 3805 | } |
| 3806 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3807 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 3808 | |
| 3809 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 3810 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3811 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3812 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 3813 | expectedDisplayId, expectedFlags); |
| 3814 | } |
| 3815 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3816 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3817 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 3818 | expectedDisplayId, expectedFlags); |
| 3819 | } |
| 3820 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3821 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 3822 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 3823 | expectedDisplayId, expectedFlags); |
| 3824 | } |
| 3825 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3826 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3827 | mInputReceiver->consumeMotionEvent( |
| 3828 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3829 | WithDisplayId(expectedDisplayId), |
| 3830 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 3833 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 3834 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 3835 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3836 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
| 3837 | 0 /*expectedFlags*/); |
| 3838 | } |
| 3839 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3840 | MotionEvent* consumeMotion() { |
| 3841 | InputEvent* event = mInputReceiver->consume(); |
| 3842 | if (!event) { |
| 3843 | ADD_FAILURE() << "No event was produced"; |
| 3844 | return nullptr; |
| 3845 | } |
| 3846 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 3847 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 3848 | return nullptr; |
| 3849 | } |
| 3850 | return static_cast<MotionEvent*>(event); |
| 3851 | } |
| 3852 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3853 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 3854 | |
| 3855 | private: |
| 3856 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3857 | }; |
| 3858 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3859 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 3860 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3861 | /** |
| 3862 | * Two entities that receive touch: A window, and a global monitor. |
| 3863 | * The touch goes to the window, and then the window disappears. |
| 3864 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 3865 | * for the monitor, as well. |
| 3866 | * 1. foregroundWindow |
| 3867 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 3868 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3869 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3870 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3871 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3872 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3873 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3874 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 3875 | |
| 3876 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3877 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3878 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3879 | {100, 200})) |
| 3880 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3881 | |
| 3882 | // Both the foreground window and the global monitor should receive the touch down |
| 3883 | window->consumeMotionDown(); |
| 3884 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3885 | |
| 3886 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3887 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3888 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 3889 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3890 | |
| 3891 | window->consumeMotionMove(); |
| 3892 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3893 | |
| 3894 | // Now the foreground window goes away |
| 3895 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3896 | window->consumeMotionCancel(); |
| 3897 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 3898 | |
| 3899 | // If more events come in, there will be no more foreground window to send them to. This will |
| 3900 | // cause a cancel for the monitor, as well. |
| 3901 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 3902 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3903 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 3904 | << "Injection should fail because the window was removed"; |
| 3905 | window->assertNoEvents(); |
| 3906 | // Global monitor now gets the cancel |
| 3907 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 3908 | } |
| 3909 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3910 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3911 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3912 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3913 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3914 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3915 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3916 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3917 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3918 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3919 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3920 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3921 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3922 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3925 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 3926 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3927 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3928 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3929 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3930 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3931 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3932 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3933 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3934 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3935 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 3936 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3937 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3938 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3939 | // Pilfer pointers from the monitor. |
| 3940 | // This should not do anything and the window should continue to receive events. |
| 3941 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3942 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3944 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 3945 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3946 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3947 | |
| 3948 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 3949 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3952 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3953 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3954 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3955 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3956 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3957 | window->setWindowOffset(20, 40); |
| 3958 | window->setWindowTransform(0, 1, -1, 0); |
| 3959 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3960 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 3961 | |
| 3962 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3963 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 3964 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3965 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3966 | MotionEvent* event = monitor.consumeMotion(); |
| 3967 | // Even though window has transform, gesture monitor must not. |
| 3968 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 3969 | } |
| 3970 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3971 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3972 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3973 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3974 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3975 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3976 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 3977 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 3978 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3981 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3982 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3983 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3984 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3985 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3986 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3987 | |
| 3988 | NotifyMotionArgs motionArgs = |
| 3989 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3990 | ADISPLAY_ID_DEFAULT); |
| 3991 | |
| 3992 | mDispatcher->notifyMotion(&motionArgs); |
| 3993 | // Window should receive motion down event. |
| 3994 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3995 | |
| 3996 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 3997 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 3998 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3999 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4000 | motionArgs.pointerCoords[0].getX() - 10); |
| 4001 | |
| 4002 | mDispatcher->notifyMotion(&motionArgs); |
| 4003 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 4004 | 0 /*expectedFlags*/); |
| 4005 | } |
| 4006 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4007 | /** |
| 4008 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 4009 | * the device default right away. In the test scenario, we check both the default value, |
| 4010 | * and the action of enabling / disabling. |
| 4011 | */ |
| 4012 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4013 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4014 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4015 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4016 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4017 | |
| 4018 | // Set focused application. |
| 4019 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4020 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4021 | |
| 4022 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4023 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4024 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4025 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 4026 | |
| 4027 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4028 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4029 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4030 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 4031 | |
| 4032 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4033 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 4034 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4035 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4036 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4037 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4038 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4039 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 4040 | |
| 4041 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4042 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4043 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4044 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 4045 | |
| 4046 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4047 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 4048 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4049 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4050 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4051 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4052 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4053 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 4054 | |
| 4055 | window->assertNoEvents(); |
| 4056 | } |
| 4057 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4058 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4059 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4060 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4061 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4062 | |
| 4063 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4064 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4065 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4066 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4067 | setFocusedWindow(window); |
| 4068 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4069 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 4070 | |
| 4071 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4072 | mDispatcher->notifyKey(&keyArgs); |
| 4073 | |
| 4074 | InputEvent* event = window->consume(); |
| 4075 | ASSERT_NE(event, nullptr); |
| 4076 | |
| 4077 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4078 | ASSERT_NE(verified, nullptr); |
| 4079 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 4080 | |
| 4081 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 4082 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 4083 | ASSERT_EQ(keyArgs.source, verified->source); |
| 4084 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 4085 | |
| 4086 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 4087 | |
| 4088 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4089 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4090 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4091 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 4092 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 4093 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 4094 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 4095 | } |
| 4096 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4097 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4098 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4099 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4100 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4101 | |
| 4102 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4103 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4104 | ui::Transform transform; |
| 4105 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4106 | |
| 4107 | gui::DisplayInfo displayInfo; |
| 4108 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 4109 | displayInfo.transform = transform; |
| 4110 | |
| 4111 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4112 | |
| 4113 | NotifyMotionArgs motionArgs = |
| 4114 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4115 | ADISPLAY_ID_DEFAULT); |
| 4116 | mDispatcher->notifyMotion(&motionArgs); |
| 4117 | |
| 4118 | InputEvent* event = window->consume(); |
| 4119 | ASSERT_NE(event, nullptr); |
| 4120 | |
| 4121 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4122 | ASSERT_NE(verified, nullptr); |
| 4123 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 4124 | |
| 4125 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 4126 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 4127 | EXPECT_EQ(motionArgs.source, verified->source); |
| 4128 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 4129 | |
| 4130 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 4131 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4132 | const vec2 rawXY = |
| 4133 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 4134 | motionArgs.pointerCoords[0].getXYValue()); |
| 4135 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 4136 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4137 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4138 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4139 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4140 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 4141 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 4142 | } |
| 4143 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4144 | /** |
| 4145 | * Ensure that separate calls to sign the same data are generating the same key. |
| 4146 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 4147 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 4148 | * tests. |
| 4149 | */ |
| 4150 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 4151 | KeyEvent event = getTestKeyEvent(); |
| 4152 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4153 | |
| 4154 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 4155 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 4156 | ASSERT_EQ(hmac1, hmac2); |
| 4157 | } |
| 4158 | |
| 4159 | /** |
| 4160 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 4161 | */ |
| 4162 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 4163 | KeyEvent event = getTestKeyEvent(); |
| 4164 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4165 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 4166 | |
| 4167 | verifiedEvent.deviceId += 1; |
| 4168 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4169 | |
| 4170 | verifiedEvent.source += 1; |
| 4171 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4172 | |
| 4173 | verifiedEvent.eventTimeNanos += 1; |
| 4174 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4175 | |
| 4176 | verifiedEvent.displayId += 1; |
| 4177 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4178 | |
| 4179 | verifiedEvent.action += 1; |
| 4180 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4181 | |
| 4182 | verifiedEvent.downTimeNanos += 1; |
| 4183 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4184 | |
| 4185 | verifiedEvent.flags += 1; |
| 4186 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4187 | |
| 4188 | verifiedEvent.keyCode += 1; |
| 4189 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4190 | |
| 4191 | verifiedEvent.scanCode += 1; |
| 4192 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4193 | |
| 4194 | verifiedEvent.metaState += 1; |
| 4195 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4196 | |
| 4197 | verifiedEvent.repeatCount += 1; |
| 4198 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4199 | } |
| 4200 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4201 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 4202 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4203 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4204 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4205 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4206 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4207 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4208 | |
| 4209 | // Top window is also focusable but is not granted focus. |
| 4210 | windowTop->setFocusable(true); |
| 4211 | windowSecond->setFocusable(true); |
| 4212 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4213 | setFocusedWindow(windowSecond); |
| 4214 | |
| 4215 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4217 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4218 | |
| 4219 | // Focused window should receive event. |
| 4220 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4221 | windowTop->assertNoEvents(); |
| 4222 | } |
| 4223 | |
| 4224 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 4225 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4226 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4227 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4228 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4229 | |
| 4230 | window->setFocusable(true); |
| 4231 | // Release channel for window is no longer valid. |
| 4232 | window->releaseChannel(); |
| 4233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4234 | setFocusedWindow(window); |
| 4235 | |
| 4236 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4237 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4238 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4239 | |
| 4240 | // window channel is invalid, so it should not receive any input event. |
| 4241 | window->assertNoEvents(); |
| 4242 | } |
| 4243 | |
| 4244 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 4245 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4246 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4247 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4248 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4249 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4250 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4251 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4252 | setFocusedWindow(window); |
| 4253 | |
| 4254 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4255 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4256 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4257 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4258 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4259 | window->assertNoEvents(); |
| 4260 | } |
| 4261 | |
| 4262 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 4263 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4264 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4265 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4266 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4267 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4268 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4269 | |
| 4270 | windowTop->setFocusable(true); |
| 4271 | windowSecond->setFocusable(true); |
| 4272 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4273 | setFocusedWindow(windowTop); |
| 4274 | windowTop->consumeFocusEvent(true); |
| 4275 | |
| 4276 | setFocusedWindow(windowSecond, windowTop); |
| 4277 | windowSecond->consumeFocusEvent(true); |
| 4278 | windowTop->consumeFocusEvent(false); |
| 4279 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4281 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4282 | |
| 4283 | // Focused window should receive event. |
| 4284 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 4285 | } |
| 4286 | |
| 4287 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 4288 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4289 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4290 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4291 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4292 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4293 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4294 | |
| 4295 | windowTop->setFocusable(true); |
| 4296 | windowSecond->setFocusable(true); |
| 4297 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4298 | setFocusedWindow(windowSecond, windowTop); |
| 4299 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4300 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 4301 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4302 | |
| 4303 | // Event should be dropped. |
| 4304 | windowTop->assertNoEvents(); |
| 4305 | windowSecond->assertNoEvents(); |
| 4306 | } |
| 4307 | |
| 4308 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 4309 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4310 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4311 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4312 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4313 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 4314 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4315 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4316 | |
| 4317 | window->setFocusable(true); |
| 4318 | previousFocusedWindow->setFocusable(true); |
| 4319 | window->setVisible(false); |
| 4320 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 4321 | setFocusedWindow(previousFocusedWindow); |
| 4322 | previousFocusedWindow->consumeFocusEvent(true); |
| 4323 | |
| 4324 | // Requesting focus on invisible window takes focus from currently focused window. |
| 4325 | setFocusedWindow(window); |
| 4326 | previousFocusedWindow->consumeFocusEvent(false); |
| 4327 | |
| 4328 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4330 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4331 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4332 | |
| 4333 | // Window does not get focus event or key down. |
| 4334 | window->assertNoEvents(); |
| 4335 | |
| 4336 | // Window becomes visible. |
| 4337 | window->setVisible(true); |
| 4338 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4339 | |
| 4340 | // Window receives focus event. |
| 4341 | window->consumeFocusEvent(true); |
| 4342 | // Focused window receives key down. |
| 4343 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4344 | } |
| 4345 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4346 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 4347 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4348 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4349 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 4350 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4351 | |
| 4352 | // window is granted focus. |
| 4353 | window->setFocusable(true); |
| 4354 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4355 | setFocusedWindow(window); |
| 4356 | window->consumeFocusEvent(true); |
| 4357 | |
| 4358 | // When a display is removed window loses focus. |
| 4359 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 4360 | window->consumeFocusEvent(false); |
| 4361 | } |
| 4362 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4363 | /** |
| 4364 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 4365 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 4366 | * of the 'slipperyEnterWindow'. |
| 4367 | * |
| 4368 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 4369 | * a way so that the touched location is no longer covered by the top window. |
| 4370 | * |
| 4371 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 4372 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 4373 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 4374 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 4375 | * with ACTION_DOWN). |
| 4376 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 4377 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 4378 | * |
| 4379 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 4380 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 4381 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 4382 | * |
| 4383 | * In this test, we ensure that the event received by the bottom window has |
| 4384 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 4385 | */ |
| 4386 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4387 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 4388 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4389 | |
| 4390 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4391 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4392 | |
| 4393 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4394 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 4395 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4396 | // Make sure this one overlaps the bottom window |
| 4397 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 4398 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 4399 | // one. Windows with the same owner are not considered to be occluding each other. |
| 4400 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 4401 | |
| 4402 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4403 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4404 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4405 | |
| 4406 | mDispatcher->setInputWindows( |
| 4407 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4408 | |
| 4409 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 4410 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4411 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 4412 | mDispatcher->notifyMotion(&args); |
| 4413 | slipperyExitWindow->consumeMotionDown(); |
| 4414 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 4415 | mDispatcher->setInputWindows( |
| 4416 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 4417 | |
| 4418 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4419 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 4420 | mDispatcher->notifyMotion(&args); |
| 4421 | |
| 4422 | slipperyExitWindow->consumeMotionCancel(); |
| 4423 | |
| 4424 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 4425 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 4426 | } |
| 4427 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4428 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 4429 | protected: |
| 4430 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 4431 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 4432 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4433 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4434 | sp<FakeWindowHandle> mWindow; |
| 4435 | |
| 4436 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4437 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4438 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4439 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4440 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 4441 | ASSERT_EQ(OK, mDispatcher->start()); |
| 4442 | |
| 4443 | setUpWindow(); |
| 4444 | } |
| 4445 | |
| 4446 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4447 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4448 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4449 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4450 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4452 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4453 | mWindow->consumeFocusEvent(true); |
| 4454 | } |
| 4455 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4456 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4457 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4458 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4459 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 4460 | mDispatcher->notifyKey(&keyArgs); |
| 4461 | |
| 4462 | // Window should receive key down event. |
| 4463 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4464 | } |
| 4465 | |
| 4466 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 4467 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 4468 | InputEvent* repeatEvent = mWindow->consume(); |
| 4469 | ASSERT_NE(nullptr, repeatEvent); |
| 4470 | |
| 4471 | uint32_t eventType = repeatEvent->getType(); |
| 4472 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 4473 | |
| 4474 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 4475 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 4476 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 4477 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 4478 | } |
| 4479 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4480 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4481 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4482 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4483 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 4484 | mDispatcher->notifyKey(&keyArgs); |
| 4485 | |
| 4486 | // Window should receive key down event. |
| 4487 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 4488 | 0 /*expectedFlags*/); |
| 4489 | } |
| 4490 | }; |
| 4491 | |
| 4492 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4493 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4494 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4495 | expectKeyRepeatOnce(repeatCount); |
| 4496 | } |
| 4497 | } |
| 4498 | |
| 4499 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 4500 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4501 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4502 | expectKeyRepeatOnce(repeatCount); |
| 4503 | } |
| 4504 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4505 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4506 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4507 | expectKeyRepeatOnce(repeatCount); |
| 4508 | } |
| 4509 | } |
| 4510 | |
| 4511 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4512 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4513 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4514 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 4515 | mWindow->assertNoEvents(); |
| 4516 | } |
| 4517 | |
| 4518 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 4519 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4520 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4521 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4522 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4523 | // Stale key up from device 1. |
| 4524 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 4525 | // Device 2 is still down, keep repeating |
| 4526 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 4527 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 4528 | // Device 2 key up |
| 4529 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 4530 | mWindow->assertNoEvents(); |
| 4531 | } |
| 4532 | |
| 4533 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 4534 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 4535 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4536 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 4537 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4538 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 4539 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 4540 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4541 | mWindow->assertNoEvents(); |
| 4542 | } |
| 4543 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 4544 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 4545 | sendAndConsumeKeyDown(DEVICE_ID); |
| 4546 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 4547 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
| 4548 | mDispatcher->notifyDeviceReset(&args); |
| 4549 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 4550 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 4551 | mWindow->assertNoEvents(); |
| 4552 | } |
| 4553 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4554 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4555 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4556 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4557 | InputEvent* repeatEvent = mWindow->consume(); |
| 4558 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4559 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 4560 | IdGenerator::getSource(repeatEvent->getId())); |
| 4561 | } |
| 4562 | } |
| 4563 | |
| 4564 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 4565 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4566 | |
| 4567 | std::unordered_set<int32_t> idSet; |
| 4568 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 4569 | InputEvent* repeatEvent = mWindow->consume(); |
| 4570 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 4571 | int32_t id = repeatEvent->getId(); |
| 4572 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 4573 | idSet.insert(id); |
| 4574 | } |
| 4575 | } |
| 4576 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4577 | /* Test InputDispatcher for MultiDisplay */ |
| 4578 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 4579 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4580 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4581 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4582 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4583 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4584 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4585 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4586 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4587 | // Set focus window for primary display, but focused display would be second one. |
| 4588 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4589 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4590 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4591 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4592 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4593 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4594 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4595 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4596 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4597 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4598 | // Set focus display to second one. |
| 4599 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 4600 | // Set focus window for second display. |
| 4601 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4602 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4603 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4604 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4605 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4606 | } |
| 4607 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4608 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4609 | InputDispatcherTest::TearDown(); |
| 4610 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4611 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4612 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4613 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4614 | windowInSecondary.clear(); |
| 4615 | } |
| 4616 | |
| 4617 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4618 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4619 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4620 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4621 | sp<FakeWindowHandle> windowInSecondary; |
| 4622 | }; |
| 4623 | |
| 4624 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 4625 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4627 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4628 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4629 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4630 | windowInSecondary->assertNoEvents(); |
| 4631 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4632 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4634 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4635 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4636 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4637 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4638 | } |
| 4639 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4640 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4641 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4643 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4644 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4645 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 4646 | windowInSecondary->assertNoEvents(); |
| 4647 | |
| 4648 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4650 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4651 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4652 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4653 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 4654 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4655 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4656 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4657 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4658 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 4659 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4660 | |
| 4661 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 4662 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4663 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4664 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4665 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 4666 | windowInSecondary->assertNoEvents(); |
| 4667 | } |
| 4668 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4669 | // Test per-display input monitors for motion event. |
| 4670 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4671 | FakeMonitorReceiver monitorInPrimary = |
| 4672 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4673 | FakeMonitorReceiver monitorInSecondary = |
| 4674 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4675 | |
| 4676 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4678 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4679 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4680 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4681 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4682 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4683 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4684 | |
| 4685 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4686 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4687 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4688 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4689 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4690 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4691 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4692 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4693 | |
| 4694 | // Test inject a non-pointer motion event. |
| 4695 | // If specific a display, it will dispatch to the focused window of particular display, |
| 4696 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4697 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4698 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 4699 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4700 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4701 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4702 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4703 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | // Test per-display input monitors for key event. |
| 4707 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4708 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4709 | FakeMonitorReceiver monitorInPrimary = |
| 4710 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4711 | FakeMonitorReceiver monitorInSecondary = |
| 4712 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4713 | |
| 4714 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4715 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 4716 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4717 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4718 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4719 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4720 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 4721 | } |
| 4722 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4723 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 4724 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4725 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4726 | secondWindowInPrimary->setFocusable(true); |
| 4727 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 4728 | setFocusedWindow(secondWindowInPrimary); |
| 4729 | windowInPrimary->consumeFocusEvent(false); |
| 4730 | secondWindowInPrimary->consumeFocusEvent(true); |
| 4731 | |
| 4732 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 4734 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4735 | windowInPrimary->assertNoEvents(); |
| 4736 | windowInSecondary->assertNoEvents(); |
| 4737 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4738 | } |
| 4739 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 4740 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 4741 | FakeMonitorReceiver monitorInPrimary = |
| 4742 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 4743 | FakeMonitorReceiver monitorInSecondary = |
| 4744 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 4745 | |
| 4746 | // Test touch down on primary display. |
| 4747 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4748 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4749 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4750 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4751 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4752 | |
| 4753 | // Test touch down on second display. |
| 4754 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4755 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 4756 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4757 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4758 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 4759 | |
| 4760 | // Trigger cancel touch. |
| 4761 | mDispatcher->cancelCurrentTouch(); |
| 4762 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4763 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4764 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4765 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4766 | |
| 4767 | // Test inject a move motion event, no window/monitor should receive the event. |
| 4768 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4769 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4770 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4771 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4772 | windowInPrimary->assertNoEvents(); |
| 4773 | monitorInPrimary.assertNoEvents(); |
| 4774 | |
| 4775 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4776 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4777 | SECOND_DISPLAY_ID, {110, 200})) |
| 4778 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 4779 | windowInSecondary->assertNoEvents(); |
| 4780 | monitorInSecondary.assertNoEvents(); |
| 4781 | } |
| 4782 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4783 | class InputFilterTest : public InputDispatcherTest { |
| 4784 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4785 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 4786 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4787 | NotifyMotionArgs motionArgs; |
| 4788 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4789 | motionArgs = |
| 4790 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4791 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4792 | motionArgs = |
| 4793 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4794 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4795 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4796 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4797 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 4798 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4799 | } else { |
| 4800 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | void testNotifyKey(bool expectToBeFiltered) { |
| 4805 | NotifyKeyArgs keyArgs; |
| 4806 | |
| 4807 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4808 | mDispatcher->notifyKey(&keyArgs); |
| 4809 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 4810 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4811 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4812 | |
| 4813 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 4814 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 4815 | } else { |
| 4816 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 4817 | } |
| 4818 | } |
| 4819 | }; |
| 4820 | |
| 4821 | // Test InputFilter for MotionEvent |
| 4822 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 4823 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 4824 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4825 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4826 | |
| 4827 | // Enable InputFilter |
| 4828 | mDispatcher->setInputFilterEnabled(true); |
| 4829 | // Test touch on both primary and second display, and check if both events are filtered. |
| 4830 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 4831 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 4832 | |
| 4833 | // Disable InputFilter |
| 4834 | mDispatcher->setInputFilterEnabled(false); |
| 4835 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 4836 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 4837 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 4838 | } |
| 4839 | |
| 4840 | // Test InputFilter for KeyEvent |
| 4841 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 4842 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 4843 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4844 | |
| 4845 | // Enable InputFilter |
| 4846 | mDispatcher->setInputFilterEnabled(true); |
| 4847 | // Send a key event, and check if it is filtered. |
| 4848 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 4849 | |
| 4850 | // Disable InputFilter |
| 4851 | mDispatcher->setInputFilterEnabled(false); |
| 4852 | // Send a key event, and check if it isn't filtered. |
| 4853 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 4854 | } |
| 4855 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 4856 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 4857 | // logical display coordinate space. |
| 4858 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 4859 | ui::Transform firstDisplayTransform; |
| 4860 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4861 | ui::Transform secondDisplayTransform; |
| 4862 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 4863 | |
| 4864 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 4865 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 4866 | displayInfos[0].transform = firstDisplayTransform; |
| 4867 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 4868 | displayInfos[1].transform = secondDisplayTransform; |
| 4869 | |
| 4870 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 4871 | |
| 4872 | // Enable InputFilter |
| 4873 | mDispatcher->setInputFilterEnabled(true); |
| 4874 | |
| 4875 | // Ensure the correct transforms are used for the displays. |
| 4876 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 4877 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 4878 | } |
| 4879 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4880 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 4881 | protected: |
| 4882 | virtual void SetUp() override { |
| 4883 | InputDispatcherTest::SetUp(); |
| 4884 | |
| 4885 | /** |
| 4886 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 4887 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 4888 | * on. |
| 4889 | */ |
| 4890 | mDispatcher->setInputFilterEnabled(true); |
| 4891 | |
| 4892 | std::shared_ptr<InputApplicationHandle> application = |
| 4893 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4894 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 4895 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4896 | |
| 4897 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4898 | mWindow->setFocusable(true); |
| 4899 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 4900 | setFocusedWindow(mWindow); |
| 4901 | mWindow->consumeFocusEvent(true); |
| 4902 | } |
| 4903 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4904 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4905 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4906 | KeyEvent event; |
| 4907 | |
| 4908 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4909 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 4910 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
| 4911 | KEY_A, AMETA_NONE, 0 /*repeatCount*/, eventTime, eventTime); |
| 4912 | const int32_t additionalPolicyFlags = |
| 4913 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 4914 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4915 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4916 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4917 | policyFlags | additionalPolicyFlags)); |
| 4918 | |
| 4919 | InputEvent* received = mWindow->consume(); |
| 4920 | ASSERT_NE(nullptr, received); |
| 4921 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4922 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 4923 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 4924 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 4925 | } |
| 4926 | |
| 4927 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 4928 | int32_t flags) { |
| 4929 | MotionEvent event; |
| 4930 | PointerProperties pointerProperties[1]; |
| 4931 | PointerCoords pointerCoords[1]; |
| 4932 | pointerProperties[0].clear(); |
| 4933 | pointerProperties[0].id = 0; |
| 4934 | pointerCoords[0].clear(); |
| 4935 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 4936 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 4937 | |
| 4938 | ui::Transform identityTransform; |
| 4939 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4940 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 4941 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 4942 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 4943 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 4944 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 4945 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4946 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 4947 | |
| 4948 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 4949 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 4950 | mDispatcher->injectInputEvent(&event, {} /*targetUid*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4951 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 4952 | policyFlags | additionalPolicyFlags)); |
| 4953 | |
| 4954 | InputEvent* received = mWindow->consume(); |
| 4955 | ASSERT_NE(nullptr, received); |
| 4956 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 4957 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 4958 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 4959 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
| 4962 | private: |
| 4963 | sp<FakeWindowHandle> mWindow; |
| 4964 | }; |
| 4965 | |
| 4966 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4967 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 4968 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 4969 | // injected device id will be overwritten. |
| 4970 | testInjectedKey(POLICY_FLAG_FILTERED, 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4971 | 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4974 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4975 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4976 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4977 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4978 | } |
| 4979 | |
| 4980 | TEST_F(InputFilterInjectionPolicyTest, |
| 4981 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 4982 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
| 4983 | 3 /*injectedDeviceId*/, 3 /*resolvedDeviceId*/, |
| 4984 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
| 4988 | testInjectedKey(0 /*policyFlags*/, 3 /*injectedDeviceId*/, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 4989 | VIRTUAL_KEYBOARD_ID /*resolvedDeviceId*/, 0 /*flags*/); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4992 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 4993 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 4994 | InputDispatcherTest::SetUp(); |
| 4995 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4996 | std::shared_ptr<FakeApplicationHandle> application = |
| 4997 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4998 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4999 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5000 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5001 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5002 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5003 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5004 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5005 | |
| 5006 | // Set focused application. |
| 5007 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5008 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5009 | |
| 5010 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5011 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5012 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5013 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5014 | } |
| 5015 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5016 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5017 | InputDispatcherTest::TearDown(); |
| 5018 | |
| 5019 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5020 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | protected: |
| 5024 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5025 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5026 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5027 | }; |
| 5028 | |
| 5029 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5030 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 5031 | // the onPointerDownOutsideFocus callback. |
| 5032 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5033 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5034 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5035 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5036 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5037 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5038 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5039 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5040 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 5041 | } |
| 5042 | |
| 5043 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 5044 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 5045 | // onPointerDownOutsideFocus callback. |
| 5046 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5048 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5049 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5050 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5051 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5052 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5053 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5054 | } |
| 5055 | |
| 5056 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 5057 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 5058 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5059 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5060 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5061 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5062 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5063 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5064 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5065 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5069 | // DOWN on the window that already has focus. Ensure no window received the |
| 5070 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5071 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5073 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5074 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5075 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5076 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5077 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5078 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5079 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5080 | } |
| 5081 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 5082 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 5083 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 5084 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 5085 | const MotionEvent event = |
| 5086 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5087 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 5088 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
| 5089 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 5090 | .build(); |
| 5091 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 5092 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5093 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 5094 | |
| 5095 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5096 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 5097 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 5098 | mUnfocusedWindow->assertNoEvents(); |
| 5099 | } |
| 5100 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5101 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 5102 | // windows that point to the same client token. |
| 5103 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 5104 | virtual void SetUp() override { |
| 5105 | InputDispatcherTest::SetUp(); |
| 5106 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5107 | std::shared_ptr<FakeApplicationHandle> application = |
| 5108 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5109 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 5110 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5111 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 5112 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5113 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 5114 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5115 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 5116 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5117 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | protected: |
| 5121 | sp<FakeWindowHandle> mWindow1; |
| 5122 | sp<FakeWindowHandle> mWindow2; |
| 5123 | |
| 5124 | // 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] | 5125 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5126 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 5127 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 5131 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5132 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5133 | InputEvent* event = window->consume(); |
| 5134 | |
| 5135 | ASSERT_NE(nullptr, event) << name.c_str() |
| 5136 | << ": consumer should have returned non-NULL event."; |
| 5137 | |
| 5138 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 5139 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 5140 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 5141 | |
| 5142 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5143 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 5144 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5145 | |
| 5146 | for (size_t i = 0; i < points.size(); i++) { |
| 5147 | float expectedX = points[i].x; |
| 5148 | float expectedY = points[i].y; |
| 5149 | |
| 5150 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 5151 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 5152 | << ", got " << motionEvent.getX(i); |
| 5153 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 5154 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 5155 | << ", got " << motionEvent.getY(i); |
| 5156 | } |
| 5157 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5158 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5159 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5160 | std::vector<PointF> expectedPoints) { |
| 5161 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 5162 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 5163 | mDispatcher->notifyMotion(&motionArgs); |
| 5164 | |
| 5165 | // Always consume from window1 since it's the window that has the InputReceiver |
| 5166 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 5167 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5168 | }; |
| 5169 | |
| 5170 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 5171 | // Touch Window 1 |
| 5172 | PointF touchedPoint = {10, 10}; |
| 5173 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5174 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5175 | |
| 5176 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5177 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5178 | |
| 5179 | // Touch Window 2 |
| 5180 | touchedPoint = {150, 150}; |
| 5181 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5182 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5183 | } |
| 5184 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5185 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 5186 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5187 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5188 | |
| 5189 | // Touch Window 1 |
| 5190 | PointF touchedPoint = {10, 10}; |
| 5191 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5192 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5193 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5194 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5195 | |
| 5196 | // Touch Window 2 |
| 5197 | touchedPoint = {150, 150}; |
| 5198 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5199 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 5200 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5201 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5202 | // Update the transform so rotation is set |
| 5203 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5204 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 5205 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5208 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5209 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5210 | |
| 5211 | // Touch Window 1 |
| 5212 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5213 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5214 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5215 | |
| 5216 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5217 | touchedPoints.push_back(PointF{150, 150}); |
| 5218 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5219 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5220 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5221 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5222 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5223 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5224 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5225 | // Update the transform so rotation is set for Window 2 |
| 5226 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5227 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5228 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5231 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5232 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5233 | |
| 5234 | // Touch Window 1 |
| 5235 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5236 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5237 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5238 | |
| 5239 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5240 | touchedPoints.push_back(PointF{150, 150}); |
| 5241 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5242 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5243 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5244 | |
| 5245 | // Move both windows |
| 5246 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5247 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5248 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5249 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5250 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5251 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5252 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5253 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5254 | expectedPoints.pop_back(); |
| 5255 | |
| 5256 | // Touch Window 2 |
| 5257 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5258 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5259 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5260 | |
| 5261 | // Move both windows |
| 5262 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5263 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5264 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5265 | |
| 5266 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
| 5269 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 5270 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 5271 | |
| 5272 | // Touch Window 1 |
| 5273 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 5274 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5275 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5276 | |
| 5277 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5278 | touchedPoints.push_back(PointF{150, 150}); |
| 5279 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5280 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5281 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5282 | |
| 5283 | // Move both windows |
| 5284 | touchedPoints = {{20, 20}, {175, 175}}; |
| 5285 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 5286 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 5287 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5288 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5291 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 5292 | virtual void SetUp() override { |
| 5293 | InputDispatcherTest::SetUp(); |
| 5294 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5295 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5296 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5297 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 5298 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5299 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5300 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5301 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5302 | |
| 5303 | // Set focused application. |
| 5304 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 5305 | |
| 5306 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5307 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5308 | mWindow->consumeFocusEvent(true); |
| 5309 | } |
| 5310 | |
| 5311 | virtual void TearDown() override { |
| 5312 | InputDispatcherTest::TearDown(); |
| 5313 | mWindow.clear(); |
| 5314 | } |
| 5315 | |
| 5316 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5317 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5318 | sp<FakeWindowHandle> mWindow; |
| 5319 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 5320 | |
| 5321 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5322 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5323 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5324 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5325 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5326 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5327 | WINDOW_LOCATION)); |
| 5328 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5329 | |
| 5330 | sp<FakeWindowHandle> addSpyWindow() { |
| 5331 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5332 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5333 | spy->setTrustedOverlay(true); |
| 5334 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 5335 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5336 | spy->setDispatchingTimeout(30ms); |
| 5337 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 5338 | return spy; |
| 5339 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5340 | }; |
| 5341 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5342 | // Send a tap and respond, which should not cause an ANR. |
| 5343 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 5344 | tapOnWindow(); |
| 5345 | mWindow->consumeMotionDown(); |
| 5346 | mWindow->consumeMotionUp(); |
| 5347 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5348 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5349 | } |
| 5350 | |
| 5351 | // Send a regular key and respond, which should not cause an ANR. |
| 5352 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5354 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5355 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5356 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5357 | } |
| 5358 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5359 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 5360 | mWindow->setFocusable(false); |
| 5361 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5362 | mWindow->consumeFocusEvent(false); |
| 5363 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5364 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5365 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5366 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 5367 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 5369 | // Key will not go to window because we have no focused window. |
| 5370 | // The 'no focused window' ANR timer should start instead. |
| 5371 | |
| 5372 | // Now, the focused application goes away. |
| 5373 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 5374 | // The key should get dropped and there should be no ANR. |
| 5375 | |
| 5376 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5377 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5378 | } |
| 5379 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5380 | // 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] | 5381 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 5382 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5383 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5385 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5386 | WINDOW_LOCATION)); |
| 5387 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5388 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 5389 | ASSERT_TRUE(sequenceNum); |
| 5390 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5391 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5392 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5393 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5394 | mWindow->consumeMotionEvent( |
| 5395 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5396 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5397 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | // Send a key to the app and have the app not respond right away. |
| 5401 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 5402 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5403 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5404 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 5405 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5406 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5407 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 5408 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5409 | } |
| 5410 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5411 | // We have a focused application, but no focused window |
| 5412 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5413 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5414 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5415 | mWindow->consumeFocusEvent(false); |
| 5416 | |
| 5417 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5418 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5419 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5420 | WINDOW_LOCATION)); |
| 5421 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 5422 | mDispatcher->waitForIdle(); |
| 5423 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5424 | |
| 5425 | // Once a focused event arrives, we get an ANR for this application |
| 5426 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5427 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5428 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5429 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5430 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5431 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5432 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5433 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5434 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5435 | } |
| 5436 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5437 | /** |
| 5438 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 5439 | * there will not be an ANR. |
| 5440 | */ |
| 5441 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 5442 | mWindow->setFocusable(false); |
| 5443 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5444 | mWindow->consumeFocusEvent(false); |
| 5445 | |
| 5446 | KeyEvent event; |
| 5447 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 5448 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 5449 | |
| 5450 | // Define a valid key down event that is stale (too old). |
| 5451 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 5452 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 5453 | AMETA_NONE, 1 /*repeatCount*/, eventTime, eventTime); |
| 5454 | |
| 5455 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 5456 | |
| 5457 | InputEventInjectionResult result = |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5458 | mDispatcher->injectInputEvent(&event, {} /* targetUid */, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 5459 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 5460 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 5461 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 5462 | << "Injection should fail because the event is stale"; |
| 5463 | |
| 5464 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5465 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5466 | mWindow->assertNoEvents(); |
| 5467 | } |
| 5468 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5469 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5470 | // Make sure that we don't notify policy twice about the same ANR. |
| 5471 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5472 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5473 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5474 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5475 | |
| 5476 | // Once a focused event arrives, we get an ANR for this application |
| 5477 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 5478 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5479 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5480 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5481 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5482 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5483 | const std::chrono::duration appTimeout = |
| 5484 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5485 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5486 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5487 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5488 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 5489 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5490 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5491 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 5492 | // '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] | 5493 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5494 | } |
| 5495 | |
| 5496 | // We have a focused application, but no focused window |
| 5497 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5498 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5499 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5500 | mWindow->consumeFocusEvent(false); |
| 5501 | |
| 5502 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5503 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5504 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5505 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5506 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5507 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 5508 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 5509 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5510 | |
| 5511 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5512 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5513 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5514 | mWindow->assertNoEvents(); |
| 5515 | } |
| 5516 | |
| 5517 | /** |
| 5518 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 5519 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 5520 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 5521 | * the ANR mechanism should still work. |
| 5522 | * |
| 5523 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 5524 | * DOWN event, while not responding on the second one. |
| 5525 | */ |
| 5526 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 5527 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5528 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5529 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5530 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5531 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5532 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5533 | |
| 5534 | // Now send ACTION_UP, with identical timestamp |
| 5535 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5536 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 5537 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 5538 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5539 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5540 | |
| 5541 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 5542 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5543 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5544 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5545 | } |
| 5546 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5547 | // A spy window can receive an ANR |
| 5548 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 5549 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 5550 | |
| 5551 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5552 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5553 | WINDOW_LOCATION)); |
| 5554 | mWindow->consumeMotionDown(); |
| 5555 | |
| 5556 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 5557 | ASSERT_TRUE(sequenceNum); |
| 5558 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5559 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5560 | |
| 5561 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5562 | spy->consumeMotionEvent( |
| 5563 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5564 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5565 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | // 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] | 5569 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5570 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 5571 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5572 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5574 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5575 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5577 | |
| 5578 | // Stuck on the ACTION_UP |
| 5579 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5580 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5581 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5582 | // 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] | 5583 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5584 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5585 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5586 | |
| 5587 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5588 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5589 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5590 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5591 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5592 | } |
| 5593 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5594 | // 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] | 5595 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5596 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 5597 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5598 | |
| 5599 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5600 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5601 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5602 | |
| 5603 | mWindow->consumeMotionDown(); |
| 5604 | // Stuck on the ACTION_UP |
| 5605 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5606 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5607 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5608 | // 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] | 5609 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5610 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5611 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5612 | |
| 5613 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 5614 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5615 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5616 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5617 | spy->assertNoEvents(); |
| 5618 | } |
| 5619 | |
| 5620 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 5621 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 5622 | |
| 5623 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5624 | |
| 5625 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5626 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5627 | WINDOW_LOCATION)); |
| 5628 | |
| 5629 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5630 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 5631 | ASSERT_TRUE(consumeSeq); |
| 5632 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5633 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5634 | |
| 5635 | monitor.finishEvent(*consumeSeq); |
| 5636 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5637 | |
| 5638 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5639 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5640 | } |
| 5641 | |
| 5642 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 5643 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 5644 | // get an ANR again. |
| 5645 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 5646 | // 2. consume all pending events (= queue becomes healthy again) |
| 5647 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 5648 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 5649 | tapOnWindow(); |
| 5650 | |
| 5651 | mWindow->consumeMotionDown(); |
| 5652 | // Block on ACTION_UP |
| 5653 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5654 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5655 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 5656 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5657 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5658 | mWindow->assertNoEvents(); |
| 5659 | |
| 5660 | tapOnWindow(); |
| 5661 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5662 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5663 | mWindow->consumeMotionUp(); |
| 5664 | |
| 5665 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5666 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5667 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5668 | mWindow->assertNoEvents(); |
| 5669 | } |
| 5670 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5671 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 5672 | // it. |
| 5673 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5675 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5676 | WINDOW_LOCATION)); |
| 5677 | |
| 5678 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5679 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5680 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5681 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 5682 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5683 | // 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] | 5684 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5685 | mWindow->consumeMotionEvent( |
| 5686 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5687 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5688 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5689 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5690 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5691 | } |
| 5692 | |
| 5693 | /** |
| 5694 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5695 | * not to to the focused window until the motion is processed. |
| 5696 | * |
| 5697 | * Warning!!! |
| 5698 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5699 | * and the injection timeout that we specify when injecting the key. |
| 5700 | * We must have the injection timeout (10ms) be smaller than |
| 5701 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5702 | * |
| 5703 | * If that value changes, this test should also change. |
| 5704 | */ |
| 5705 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 5706 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5708 | |
| 5709 | tapOnWindow(); |
| 5710 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5711 | ASSERT_TRUE(downSequenceNum); |
| 5712 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5713 | ASSERT_TRUE(upSequenceNum); |
| 5714 | // Don't finish the events yet, and send a key |
| 5715 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 5716 | // window even if motions are still being processed. But because the injection timeout is short, |
| 5717 | // we will receive INJECTION_TIMED_OUT as the result. |
| 5718 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5719 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5720 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5721 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 5722 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5723 | // Key will not be sent to the window, yet, because the window is still processing events |
| 5724 | // and the key remains pending, waiting for the touch events to be processed |
| 5725 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5726 | ASSERT_FALSE(keySequenceNum); |
| 5727 | |
| 5728 | std::this_thread::sleep_for(500ms); |
| 5729 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 5730 | // to the focused window, even though we have not yet finished the motion event |
| 5731 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5732 | mWindow->finishEvent(*downSequenceNum); |
| 5733 | mWindow->finishEvent(*upSequenceNum); |
| 5734 | } |
| 5735 | |
| 5736 | /** |
| 5737 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5738 | * not go to the focused window until the motion is processed. |
| 5739 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 5740 | * focused window right away. |
| 5741 | */ |
| 5742 | TEST_F(InputDispatcherSingleWindowAnr, |
| 5743 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 5744 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 5745 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5746 | |
| 5747 | tapOnWindow(); |
| 5748 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 5749 | ASSERT_TRUE(downSequenceNum); |
| 5750 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 5751 | ASSERT_TRUE(upSequenceNum); |
| 5752 | // Don't finish the events yet, and send a key |
| 5753 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5754 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5755 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5756 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5757 | // At this point, key is still pending, and should not be sent to the application yet. |
| 5758 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 5759 | ASSERT_FALSE(keySequenceNum); |
| 5760 | |
| 5761 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 5762 | tapOnWindow(); |
| 5763 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 5764 | // the other events yet. We can finish events in any order. |
| 5765 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 5766 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 5767 | mWindow->consumeMotionDown(); |
| 5768 | mWindow->consumeMotionUp(); |
| 5769 | mWindow->assertNoEvents(); |
| 5770 | } |
| 5771 | |
| 5772 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 5773 | virtual void SetUp() override { |
| 5774 | InputDispatcherTest::SetUp(); |
| 5775 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5776 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5777 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5778 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 5779 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5780 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5781 | // 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] | 5782 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5783 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5784 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 5785 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 5786 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5787 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5788 | |
| 5789 | // Set focused application. |
| 5790 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5791 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5792 | |
| 5793 | // Expect one focus window exist in display. |
| 5794 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5795 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5796 | mFocusedWindow->consumeFocusEvent(true); |
| 5797 | } |
| 5798 | |
| 5799 | virtual void TearDown() override { |
| 5800 | InputDispatcherTest::TearDown(); |
| 5801 | |
| 5802 | mUnfocusedWindow.clear(); |
| 5803 | mFocusedWindow.clear(); |
| 5804 | } |
| 5805 | |
| 5806 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5807 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5808 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 5809 | sp<FakeWindowHandle> mFocusedWindow; |
| 5810 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 5811 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 5812 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 5813 | |
| 5814 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 5815 | |
| 5816 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 5817 | |
| 5818 | private: |
| 5819 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5820 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5821 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5822 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5823 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5824 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5825 | location)); |
| 5826 | } |
| 5827 | }; |
| 5828 | |
| 5829 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 5830 | // should be ANR'd first. |
| 5831 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
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 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5835 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5836 | mFocusedWindow->consumeMotionDown(); |
| 5837 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5838 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5839 | // We consumed all events, so no ANR |
| 5840 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5841 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5842 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5843 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5844 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5845 | FOCUSED_WINDOW_LOCATION)); |
| 5846 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5847 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5848 | |
| 5849 | const std::chrono::duration timeout = |
| 5850 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5851 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5852 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 5853 | // sequence to make it consistent |
| 5854 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5855 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5856 | mFocusedWindow->consumeMotionDown(); |
| 5857 | // This cancel is generated because the connection was unresponsive |
| 5858 | mFocusedWindow->consumeMotionCancel(); |
| 5859 | mFocusedWindow->assertNoEvents(); |
| 5860 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5861 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5862 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5863 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5864 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 5868 | // it doesn't matter which order they should have ANR. |
| 5869 | // But we should receive ANR for both. |
| 5870 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 5871 | // Set the timeout for unfocused window to match the focused window |
| 5872 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 5873 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5874 | |
| 5875 | tapOnFocusedWindow(); |
| 5876 | // 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] | 5877 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 5878 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 5879 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5880 | |
| 5881 | // 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] | 5882 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 5883 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 5884 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 5885 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5886 | |
| 5887 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5888 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5889 | |
| 5890 | mFocusedWindow->consumeMotionDown(); |
| 5891 | mFocusedWindow->consumeMotionUp(); |
| 5892 | mUnfocusedWindow->consumeMotionOutside(); |
| 5893 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5894 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 5895 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 5896 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5897 | |
| 5898 | // Both applications should be marked as responsive, in any order |
| 5899 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 5900 | mFocusedWindow->getToken() == responsiveToken2); |
| 5901 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 5902 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 5903 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5904 | } |
| 5905 | |
| 5906 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 5907 | // We should also log an error to account for the dropped event (not tested here). |
| 5908 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 5909 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 5910 | tapOnFocusedWindow(); |
| 5911 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 5912 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 5913 | // Receive the events, but don't respond |
| 5914 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 5915 | ASSERT_TRUE(downEventSequenceNum); |
| 5916 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 5917 | ASSERT_TRUE(upEventSequenceNum); |
| 5918 | const std::chrono::duration timeout = |
| 5919 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5920 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5921 | |
| 5922 | // Tap once again |
| 5923 | // 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] | 5924 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5925 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5926 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5927 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5928 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5929 | FOCUSED_WINDOW_LOCATION)); |
| 5930 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 5931 | // valid touch target |
| 5932 | mUnfocusedWindow->assertNoEvents(); |
| 5933 | |
| 5934 | // Consume the first tap |
| 5935 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 5936 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 5937 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5938 | // The second tap did not go to the focused window |
| 5939 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 5940 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 5941 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 5942 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5943 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5944 | } |
| 5945 | |
| 5946 | // If you tap outside of all windows, there will not be ANR |
| 5947 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5948 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5949 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5950 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 5951 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5952 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5953 | } |
| 5954 | |
| 5955 | // Since the focused window is paused, tapping on it should not produce any events |
| 5956 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 5957 | mFocusedWindow->setPaused(true); |
| 5958 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 5959 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5960 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5961 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5962 | FOCUSED_WINDOW_LOCATION)); |
| 5963 | |
| 5964 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 5965 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5966 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 5967 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 5968 | |
| 5969 | mFocusedWindow->assertNoEvents(); |
| 5970 | mUnfocusedWindow->assertNoEvents(); |
| 5971 | } |
| 5972 | |
| 5973 | /** |
| 5974 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 5975 | * not to to the focused window until the motion is processed. |
| 5976 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 5977 | * |
| 5978 | * Warning!!! |
| 5979 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 5980 | * and the injection timeout that we specify when injecting the key. |
| 5981 | * We must have the injection timeout (10ms) be smaller than |
| 5982 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 5983 | * |
| 5984 | * If that value changes, this test should also change. |
| 5985 | */ |
| 5986 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 5987 | // Set a long ANR timeout to prevent it from triggering |
| 5988 | mFocusedWindow->setDispatchingTimeout(2s); |
| 5989 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 5990 | |
| 5991 | tapOnUnfocusedWindow(); |
| 5992 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5993 | ASSERT_TRUE(downSequenceNum); |
| 5994 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 5995 | ASSERT_TRUE(upSequenceNum); |
| 5996 | // Don't finish the events yet, and send a key |
| 5997 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 5998 | // window even if motions are still being processed. |
| 5999 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6000 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6001 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6002 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 6003 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6004 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6005 | // and the key remains pending, waiting for the touch events to be processed |
| 6006 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 6007 | ASSERT_FALSE(keySequenceNum); |
| 6008 | |
| 6009 | // 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] | 6010 | mFocusedWindow->setFocusable(false); |
| 6011 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6012 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6013 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6014 | |
| 6015 | // Focus events should precede the key events |
| 6016 | mUnfocusedWindow->consumeFocusEvent(true); |
| 6017 | mFocusedWindow->consumeFocusEvent(false); |
| 6018 | |
| 6019 | // Finish the tap events, which should unblock dispatcher |
| 6020 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 6021 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 6022 | |
| 6023 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 6024 | // can finally go to the newly focused "mUnfocusedWindow". |
| 6025 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6026 | mFocusedWindow->assertNoEvents(); |
| 6027 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6028 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6029 | } |
| 6030 | |
| 6031 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 6032 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 6033 | // The other window should not be affected by that. |
| 6034 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 6035 | // Touch Window 1 |
| 6036 | NotifyMotionArgs motionArgs = |
| 6037 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6038 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 6039 | mDispatcher->notifyMotion(&motionArgs); |
| 6040 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 6041 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 6042 | |
| 6043 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6044 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6045 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6046 | mDispatcher->notifyMotion(&motionArgs); |
| 6047 | |
| 6048 | const std::chrono::duration timeout = |
| 6049 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6050 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6051 | |
| 6052 | mUnfocusedWindow->consumeMotionDown(); |
| 6053 | mFocusedWindow->consumeMotionDown(); |
| 6054 | // Focused window may or may not receive ACTION_MOVE |
| 6055 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 6056 | InputEvent* event; |
| 6057 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 6058 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 6059 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 6060 | ASSERT_NE(nullptr, event); |
| 6061 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 6062 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 6063 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 6064 | mFocusedWindow->consumeMotionCancel(); |
| 6065 | } else { |
| 6066 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 6067 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6068 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6069 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6070 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6071 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6072 | mUnfocusedWindow->assertNoEvents(); |
| 6073 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6074 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6075 | } |
| 6076 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6077 | /** |
| 6078 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 6079 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 6080 | * |
| 6081 | * If the user touches another application during this time, the key should be dropped. |
| 6082 | * Next, if a new focused window comes in, without toggling the focused application, |
| 6083 | * then no ANR should occur. |
| 6084 | * |
| 6085 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 6086 | * but in some cases the policy may not update the focused application. |
| 6087 | */ |
| 6088 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 6089 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 6090 | std::make_shared<FakeApplicationHandle>(); |
| 6091 | focusedApplication->setDispatchingTimeout(60ms); |
| 6092 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 6093 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 6094 | mFocusedWindow->setFocusable(false); |
| 6095 | |
| 6096 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6097 | mFocusedWindow->consumeFocusEvent(false); |
| 6098 | |
| 6099 | // Send a key. The ANR timer should start because there is no focused window. |
| 6100 | // 'focusedApplication' will get blamed if this timer completes. |
| 6101 | // 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] | 6102 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6103 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6104 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/, |
| 6105 | false /* allowKeyRepeat */); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6106 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6107 | |
| 6108 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 6109 | // then the injected touches won't cause the focused event to get dropped. |
| 6110 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 6111 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 6112 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 6113 | // For this test, it means that the key would get delivered to the window once it becomes |
| 6114 | // focused. |
| 6115 | std::this_thread::sleep_for(10ms); |
| 6116 | |
| 6117 | // Touch unfocused window. This should force the pending key to get dropped. |
| 6118 | NotifyMotionArgs motionArgs = |
| 6119 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6120 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 6121 | mDispatcher->notifyMotion(&motionArgs); |
| 6122 | |
| 6123 | // We do not consume the motion right away, because that would require dispatcher to first |
| 6124 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 6125 | // Set the focused window first. |
| 6126 | mFocusedWindow->setFocusable(true); |
| 6127 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6128 | setFocusedWindow(mFocusedWindow); |
| 6129 | mFocusedWindow->consumeFocusEvent(true); |
| 6130 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 6131 | // to another application. This could be a bug / behaviour in the policy. |
| 6132 | |
| 6133 | mUnfocusedWindow->consumeMotionDown(); |
| 6134 | |
| 6135 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6136 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 6137 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 6138 | } |
| 6139 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6140 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 6141 | // that has feature NO_INPUT_CHANNEL. |
| 6142 | // Layout: |
| 6143 | // Top (closest to user) |
| 6144 | // mNoInputWindow (above all windows) |
| 6145 | // mBottomWindow |
| 6146 | // Bottom (furthest from user) |
| 6147 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 6148 | virtual void SetUp() override { |
| 6149 | InputDispatcherTest::SetUp(); |
| 6150 | |
| 6151 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6152 | mNoInputWindow = |
| 6153 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 6154 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 6155 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6156 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6157 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6158 | // It's perfectly valid for this window to not have an associated input channel |
| 6159 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6160 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 6161 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6162 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6163 | |
| 6164 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 6165 | } |
| 6166 | |
| 6167 | protected: |
| 6168 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 6169 | sp<FakeWindowHandle> mNoInputWindow; |
| 6170 | sp<FakeWindowHandle> mBottomWindow; |
| 6171 | }; |
| 6172 | |
| 6173 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 6174 | PointF touchedPoint = {10, 10}; |
| 6175 | |
| 6176 | NotifyMotionArgs motionArgs = |
| 6177 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6178 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 6179 | mDispatcher->notifyMotion(&motionArgs); |
| 6180 | |
| 6181 | mNoInputWindow->assertNoEvents(); |
| 6182 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 6183 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 6184 | // and therefore should prevent mBottomWindow from receiving touches |
| 6185 | mBottomWindow->assertNoEvents(); |
| 6186 | } |
| 6187 | |
| 6188 | /** |
| 6189 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 6190 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 6191 | */ |
| 6192 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 6193 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6194 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 6195 | "Window with input channel and NO_INPUT_CHANNEL", |
| 6196 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6197 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6198 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6199 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6200 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 6201 | |
| 6202 | PointF touchedPoint = {10, 10}; |
| 6203 | |
| 6204 | NotifyMotionArgs motionArgs = |
| 6205 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6206 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 6207 | mDispatcher->notifyMotion(&motionArgs); |
| 6208 | |
| 6209 | mNoInputWindow->assertNoEvents(); |
| 6210 | mBottomWindow->assertNoEvents(); |
| 6211 | } |
| 6212 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6213 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 6214 | protected: |
| 6215 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6216 | sp<FakeWindowHandle> mWindow; |
| 6217 | sp<FakeWindowHandle> mMirror; |
| 6218 | |
| 6219 | virtual void SetUp() override { |
| 6220 | InputDispatcherTest::SetUp(); |
| 6221 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6222 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 6223 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 6224 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6225 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6226 | mWindow->setFocusable(true); |
| 6227 | mMirror->setFocusable(true); |
| 6228 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6229 | } |
| 6230 | }; |
| 6231 | |
| 6232 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 6233 | // Request focus on a mirrored window |
| 6234 | setFocusedWindow(mMirror); |
| 6235 | |
| 6236 | // window gets focused |
| 6237 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6238 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6239 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6240 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6241 | } |
| 6242 | |
| 6243 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 6244 | // focusable. |
| 6245 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 6246 | setFocusedWindow(mMirror); |
| 6247 | |
| 6248 | // window gets focused |
| 6249 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6250 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6251 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6252 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6253 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6254 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6255 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6256 | |
| 6257 | mMirror->setFocusable(false); |
| 6258 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6259 | |
| 6260 | // window loses focus since one of the windows associated with the token in not focusable |
| 6261 | mWindow->consumeFocusEvent(false); |
| 6262 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6263 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6264 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6265 | mWindow->assertNoEvents(); |
| 6266 | } |
| 6267 | |
| 6268 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 6269 | // invisible. |
| 6270 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 6271 | setFocusedWindow(mMirror); |
| 6272 | |
| 6273 | // window gets focused |
| 6274 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6275 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6276 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6277 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6278 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6279 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6280 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6281 | |
| 6282 | mMirror->setVisible(false); |
| 6283 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6284 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6286 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6287 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6288 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6289 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6290 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6291 | |
| 6292 | mWindow->setVisible(false); |
| 6293 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6294 | |
| 6295 | // window loses focus only after all windows associated with the token become invisible. |
| 6296 | mWindow->consumeFocusEvent(false); |
| 6297 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6298 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6299 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6300 | mWindow->assertNoEvents(); |
| 6301 | } |
| 6302 | |
| 6303 | // A focused & mirrored window remains focused until both windows are removed. |
| 6304 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 6305 | setFocusedWindow(mMirror); |
| 6306 | |
| 6307 | // window gets focused |
| 6308 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6309 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6310 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6311 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6312 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6313 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6314 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6315 | |
| 6316 | // single window is removed but the window token remains focused |
| 6317 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 6318 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6319 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6320 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6321 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6322 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 6323 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6324 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 6325 | |
| 6326 | // Both windows are removed |
| 6327 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 6328 | mWindow->consumeFocusEvent(false); |
| 6329 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6330 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 6331 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6332 | mWindow->assertNoEvents(); |
| 6333 | } |
| 6334 | |
| 6335 | // Focus request can be pending until one window becomes visible. |
| 6336 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 6337 | // Request focus on an invisible mirror. |
| 6338 | mWindow->setVisible(false); |
| 6339 | mMirror->setVisible(false); |
| 6340 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6341 | setFocusedWindow(mMirror); |
| 6342 | |
| 6343 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6344 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6345 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6346 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6347 | |
| 6348 | mMirror->setVisible(true); |
| 6349 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 6350 | |
| 6351 | // window gets focused |
| 6352 | mWindow->consumeFocusEvent(true); |
| 6353 | // window gets the pending key event |
| 6354 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6355 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6356 | |
| 6357 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 6358 | protected: |
| 6359 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6360 | sp<FakeWindowHandle> mWindow; |
| 6361 | sp<FakeWindowHandle> mSecondWindow; |
| 6362 | |
| 6363 | void SetUp() override { |
| 6364 | InputDispatcherTest::SetUp(); |
| 6365 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6366 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6367 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6368 | mSecondWindow = |
| 6369 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6370 | mSecondWindow->setFocusable(true); |
| 6371 | |
| 6372 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 6373 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 6374 | |
| 6375 | setFocusedWindow(mWindow); |
| 6376 | mWindow->consumeFocusEvent(true); |
| 6377 | } |
| 6378 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6379 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 6380 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6381 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 6382 | } |
| 6383 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6384 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 6385 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6386 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6387 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 6388 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6389 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6390 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6391 | } |
| 6392 | }; |
| 6393 | |
| 6394 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 6395 | // Ensure that capture cannot be obtained for unfocused windows. |
| 6396 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 6397 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6398 | mSecondWindow->assertNoEvents(); |
| 6399 | |
| 6400 | // Ensure that capture can be enabled from the focus window. |
| 6401 | requestAndVerifyPointerCapture(mWindow, true); |
| 6402 | |
| 6403 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 6404 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 6405 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6406 | |
| 6407 | // Ensure that capture can be disabled from the window with capture. |
| 6408 | requestAndVerifyPointerCapture(mWindow, false); |
| 6409 | } |
| 6410 | |
| 6411 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6412 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6413 | |
| 6414 | setFocusedWindow(mSecondWindow); |
| 6415 | |
| 6416 | // Ensure that the capture disabled event was sent first. |
| 6417 | mWindow->consumeCaptureEvent(false); |
| 6418 | mWindow->consumeFocusEvent(false); |
| 6419 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6420 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6421 | |
| 6422 | // 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] | 6423 | notifyPointerCaptureChanged({}); |
| 6424 | notifyPointerCaptureChanged(request); |
| 6425 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6426 | mWindow->assertNoEvents(); |
| 6427 | mSecondWindow->assertNoEvents(); |
| 6428 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 6429 | } |
| 6430 | |
| 6431 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6432 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6433 | |
| 6434 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6435 | notifyPointerCaptureChanged({}); |
| 6436 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6437 | |
| 6438 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6439 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 6440 | mWindow->consumeCaptureEvent(false); |
| 6441 | mWindow->assertNoEvents(); |
| 6442 | } |
| 6443 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6444 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 6445 | requestAndVerifyPointerCapture(mWindow, true); |
| 6446 | |
| 6447 | // The first window loses focus. |
| 6448 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6449 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6450 | mWindow->consumeCaptureEvent(false); |
| 6451 | |
| 6452 | // Request Pointer Capture from the second window before the notification from InputReader |
| 6453 | // arrives. |
| 6454 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6455 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6456 | |
| 6457 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6458 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6459 | |
| 6460 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6461 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 6462 | |
| 6463 | mSecondWindow->consumeFocusEvent(true); |
| 6464 | mSecondWindow->consumeCaptureEvent(true); |
| 6465 | } |
| 6466 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 6467 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 6468 | // App repeatedly enables and disables capture. |
| 6469 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6470 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6471 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6472 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6473 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6474 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6475 | |
| 6476 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 6477 | // first request is now stale, this should do nothing. |
| 6478 | notifyPointerCaptureChanged(firstRequest); |
| 6479 | mWindow->assertNoEvents(); |
| 6480 | |
| 6481 | // InputReader notifies that the second request was enabled. |
| 6482 | notifyPointerCaptureChanged(secondRequest); |
| 6483 | mWindow->consumeCaptureEvent(true); |
| 6484 | } |
| 6485 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 6486 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 6487 | requestAndVerifyPointerCapture(mWindow, true); |
| 6488 | |
| 6489 | // App toggles pointer capture off and on. |
| 6490 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 6491 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 6492 | |
| 6493 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 6494 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 6495 | |
| 6496 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 6497 | // preceding "disable" request. |
| 6498 | notifyPointerCaptureChanged(enableRequest); |
| 6499 | |
| 6500 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 6501 | // any notifications. |
| 6502 | mWindow->assertNoEvents(); |
| 6503 | } |
| 6504 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6505 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 6506 | protected: |
| 6507 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6508 | |
| 6509 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 6510 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 6511 | |
| 6512 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 6513 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6514 | |
| 6515 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 6516 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 6517 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 6518 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 6519 | MAXIMUM_OBSCURING_OPACITY); |
| 6520 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6521 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6522 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6523 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6524 | |
| 6525 | sp<FakeWindowHandle> mTouchWindow; |
| 6526 | |
| 6527 | virtual void SetUp() override { |
| 6528 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6529 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6530 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 6531 | } |
| 6532 | |
| 6533 | virtual void TearDown() override { |
| 6534 | InputDispatcherTest::TearDown(); |
| 6535 | mTouchWindow.clear(); |
| 6536 | } |
| 6537 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 6538 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 6539 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6540 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6541 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6542 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6543 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6544 | return window; |
| 6545 | } |
| 6546 | |
| 6547 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 6548 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 6549 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6550 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6551 | // Generate an arbitrary PID based on the UID |
| 6552 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 6553 | return window; |
| 6554 | } |
| 6555 | |
| 6556 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 6557 | NotifyMotionArgs args = |
| 6558 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6559 | ADISPLAY_ID_DEFAULT, points); |
| 6560 | mDispatcher->notifyMotion(&args); |
| 6561 | } |
| 6562 | }; |
| 6563 | |
| 6564 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6565 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6566 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6567 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6568 | |
| 6569 | touch(); |
| 6570 | |
| 6571 | mTouchWindow->assertNoEvents(); |
| 6572 | } |
| 6573 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6574 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6575 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 6576 | const sp<FakeWindowHandle>& w = |
| 6577 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 6578 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6579 | |
| 6580 | touch(); |
| 6581 | |
| 6582 | mTouchWindow->assertNoEvents(); |
| 6583 | } |
| 6584 | |
| 6585 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6586 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 6587 | const sp<FakeWindowHandle>& w = |
| 6588 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6589 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6590 | |
| 6591 | touch(); |
| 6592 | |
| 6593 | w->assertNoEvents(); |
| 6594 | } |
| 6595 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6596 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6597 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 6598 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6599 | |
| 6600 | touch(); |
| 6601 | |
| 6602 | mTouchWindow->consumeAnyMotionDown(); |
| 6603 | } |
| 6604 | |
| 6605 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6606 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6607 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6608 | w->setFrame(Rect(0, 0, 50, 50)); |
| 6609 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6610 | |
| 6611 | touch({PointF{100, 100}}); |
| 6612 | |
| 6613 | mTouchWindow->consumeAnyMotionDown(); |
| 6614 | } |
| 6615 | |
| 6616 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6617 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6618 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6619 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6620 | |
| 6621 | touch(); |
| 6622 | |
| 6623 | mTouchWindow->consumeAnyMotionDown(); |
| 6624 | } |
| 6625 | |
| 6626 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 6627 | const sp<FakeWindowHandle>& w = |
| 6628 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6629 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6630 | |
| 6631 | touch(); |
| 6632 | |
| 6633 | mTouchWindow->consumeAnyMotionDown(); |
| 6634 | } |
| 6635 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6636 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 6637 | const sp<FakeWindowHandle>& w = |
| 6638 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 6639 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6640 | |
| 6641 | touch(); |
| 6642 | |
| 6643 | w->assertNoEvents(); |
| 6644 | } |
| 6645 | |
| 6646 | /** |
| 6647 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 6648 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 6649 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 6650 | */ |
| 6651 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6652 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 6653 | const sp<FakeWindowHandle>& w = |
| 6654 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6655 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6656 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6657 | |
| 6658 | touch(); |
| 6659 | |
| 6660 | w->consumeMotionOutside(); |
| 6661 | } |
| 6662 | |
| 6663 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 6664 | const sp<FakeWindowHandle>& w = |
| 6665 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6666 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6667 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6668 | |
| 6669 | touch(); |
| 6670 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 6671 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 6672 | } |
| 6673 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6674 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6675 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6676 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6677 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6678 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6679 | |
| 6680 | touch(); |
| 6681 | |
| 6682 | mTouchWindow->consumeAnyMotionDown(); |
| 6683 | } |
| 6684 | |
| 6685 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 6686 | const sp<FakeWindowHandle>& w = |
| 6687 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6688 | MAXIMUM_OBSCURING_OPACITY); |
| 6689 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6690 | |
| 6691 | touch(); |
| 6692 | |
| 6693 | mTouchWindow->consumeAnyMotionDown(); |
| 6694 | } |
| 6695 | |
| 6696 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6697 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6698 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6699 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6700 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6701 | |
| 6702 | touch(); |
| 6703 | |
| 6704 | mTouchWindow->assertNoEvents(); |
| 6705 | } |
| 6706 | |
| 6707 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 6708 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 6709 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6710 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6711 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6712 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6713 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6714 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6716 | |
| 6717 | touch(); |
| 6718 | |
| 6719 | mTouchWindow->assertNoEvents(); |
| 6720 | } |
| 6721 | |
| 6722 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 6723 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 6724 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6725 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 6726 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6727 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6728 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 6729 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6730 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6731 | |
| 6732 | touch(); |
| 6733 | |
| 6734 | mTouchWindow->consumeAnyMotionDown(); |
| 6735 | } |
| 6736 | |
| 6737 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6738 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 6739 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6740 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6741 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6742 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6743 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6744 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6745 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6746 | |
| 6747 | touch(); |
| 6748 | |
| 6749 | mTouchWindow->consumeAnyMotionDown(); |
| 6750 | } |
| 6751 | |
| 6752 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 6753 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6754 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6755 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6756 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6757 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6758 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 6759 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 6760 | |
| 6761 | touch(); |
| 6762 | |
| 6763 | mTouchWindow->assertNoEvents(); |
| 6764 | } |
| 6765 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6766 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6767 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 6768 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6769 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6770 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6771 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6772 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6773 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6774 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6775 | |
| 6776 | touch(); |
| 6777 | |
| 6778 | mTouchWindow->assertNoEvents(); |
| 6779 | } |
| 6780 | |
| 6781 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6782 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 6783 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6784 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6785 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6786 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6787 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6788 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6789 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 6790 | |
| 6791 | touch(); |
| 6792 | |
| 6793 | mTouchWindow->consumeAnyMotionDown(); |
| 6794 | } |
| 6795 | |
| 6796 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 6797 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6798 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 6799 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 6800 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6801 | |
| 6802 | touch(); |
| 6803 | |
| 6804 | mTouchWindow->consumeAnyMotionDown(); |
| 6805 | } |
| 6806 | |
| 6807 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 6808 | const sp<FakeWindowHandle>& w = |
| 6809 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6810 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6811 | |
| 6812 | touch(); |
| 6813 | |
| 6814 | mTouchWindow->consumeAnyMotionDown(); |
| 6815 | } |
| 6816 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6817 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6818 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 6819 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6820 | const sp<FakeWindowHandle>& w = |
| 6821 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 6822 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6823 | |
| 6824 | touch(); |
| 6825 | |
| 6826 | mTouchWindow->assertNoEvents(); |
| 6827 | } |
| 6828 | |
| 6829 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 6830 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 6831 | const sp<FakeWindowHandle>& w = |
| 6832 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 6833 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6834 | |
| 6835 | touch(); |
| 6836 | |
| 6837 | mTouchWindow->consumeAnyMotionDown(); |
| 6838 | } |
| 6839 | |
| 6840 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6841 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 6842 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 6843 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 6844 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6845 | OPACITY_ABOVE_THRESHOLD); |
| 6846 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6847 | |
| 6848 | touch(); |
| 6849 | |
| 6850 | mTouchWindow->consumeAnyMotionDown(); |
| 6851 | } |
| 6852 | |
| 6853 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6854 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 6855 | const sp<FakeWindowHandle>& w1 = |
| 6856 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6857 | OPACITY_BELOW_THRESHOLD); |
| 6858 | const sp<FakeWindowHandle>& w2 = |
| 6859 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 6860 | OPACITY_BELOW_THRESHOLD); |
| 6861 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 6862 | |
| 6863 | touch(); |
| 6864 | |
| 6865 | mTouchWindow->assertNoEvents(); |
| 6866 | } |
| 6867 | |
| 6868 | /** |
| 6869 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 6870 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 6871 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 6872 | */ |
| 6873 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6874 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 6875 | const sp<FakeWindowHandle>& wB = |
| 6876 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 6877 | OPACITY_BELOW_THRESHOLD); |
| 6878 | const sp<FakeWindowHandle>& wC = |
| 6879 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 6880 | OPACITY_BELOW_THRESHOLD); |
| 6881 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 6882 | |
| 6883 | touch(); |
| 6884 | |
| 6885 | mTouchWindow->assertNoEvents(); |
| 6886 | } |
| 6887 | |
| 6888 | /** |
| 6889 | * This test is testing that a window from a different UID but with same application token doesn't |
| 6890 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 6891 | */ |
| 6892 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 6893 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 6894 | const sp<FakeWindowHandle>& w = |
| 6895 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6896 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 6897 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 6898 | |
| 6899 | touch(); |
| 6900 | |
| 6901 | mTouchWindow->consumeAnyMotionDown(); |
| 6902 | } |
| 6903 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6904 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 6905 | protected: |
| 6906 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6907 | sp<FakeWindowHandle> mWindow; |
| 6908 | sp<FakeWindowHandle> mSecondWindow; |
| 6909 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6910 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6911 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 6912 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6913 | |
| 6914 | void SetUp() override { |
| 6915 | InputDispatcherTest::SetUp(); |
| 6916 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6917 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6918 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6919 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6920 | mSecondWindow = |
| 6921 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6922 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6923 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6924 | mSpyWindow = |
| 6925 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6926 | mSpyWindow->setSpy(true); |
| 6927 | mSpyWindow->setTrustedOverlay(true); |
| 6928 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 6929 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6930 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6931 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6932 | } |
| 6933 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6934 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 6935 | switch (fromSource) { |
| 6936 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 6937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6938 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6939 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 6940 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6941 | break; |
| 6942 | case AINPUT_SOURCE_STYLUS: |
| 6943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6944 | injectMotionEvent( |
| 6945 | mDispatcher, |
| 6946 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 6947 | AINPUT_SOURCE_STYLUS) |
| 6948 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 6949 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 6950 | .x(50) |
| 6951 | .y(50)) |
| 6952 | .build())); |
| 6953 | break; |
| 6954 | case AINPUT_SOURCE_MOUSE: |
| 6955 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6956 | injectMotionEvent( |
| 6957 | mDispatcher, |
| 6958 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6959 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 6960 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 6961 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 6962 | .x(50) |
| 6963 | .y(50)) |
| 6964 | .build())); |
| 6965 | break; |
| 6966 | default: |
| 6967 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 6968 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6969 | |
| 6970 | // Window should receive motion event. |
| 6971 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6972 | // Spy window should also receive motion event |
| 6973 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6974 | } |
| 6975 | |
| 6976 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 6977 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 6978 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6979 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6980 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 6981 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6982 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6983 | |
| 6984 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6985 | mDragWindow = |
| 6986 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6987 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6988 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 6989 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 6990 | |
| 6991 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 6992 | bool transferred = |
| 6993 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
| 6994 | true /* isDragDrop */); |
| 6995 | if (transferred) { |
| 6996 | mWindow->consumeMotionCancel(); |
| 6997 | mDragWindow->consumeMotionDown(); |
| 6998 | } |
| 6999 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7000 | } |
| 7001 | }; |
| 7002 | |
| 7003 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7004 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7005 | |
| 7006 | // Move on window. |
| 7007 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7008 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7009 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7010 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7011 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7012 | mWindow->consumeDragEvent(false, 50, 50); |
| 7013 | mSecondWindow->assertNoEvents(); |
| 7014 | |
| 7015 | // Move to another window. |
| 7016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7017 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7018 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7019 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7020 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7021 | mWindow->consumeDragEvent(true, 150, 50); |
| 7022 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7023 | |
| 7024 | // Move back to original window. |
| 7025 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7026 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7027 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7028 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7029 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7030 | mWindow->consumeDragEvent(false, 50, 50); |
| 7031 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 7032 | |
| 7033 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7034 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7035 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7036 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7037 | mWindow->assertNoEvents(); |
| 7038 | mSecondWindow->assertNoEvents(); |
| 7039 | } |
| 7040 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7041 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7042 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7043 | |
| 7044 | // No cancel event after drag start |
| 7045 | mSpyWindow->assertNoEvents(); |
| 7046 | |
| 7047 | const MotionEvent secondFingerDownEvent = |
| 7048 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7049 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7050 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7051 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60)) |
| 7052 | .build(); |
| 7053 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7054 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7055 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7056 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7057 | |
| 7058 | // Receives cancel for first pointer after next pointer down |
| 7059 | mSpyWindow->consumeMotionCancel(); |
| 7060 | mSpyWindow->consumeMotionDown(); |
| 7061 | |
| 7062 | mSpyWindow->assertNoEvents(); |
| 7063 | } |
| 7064 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7065 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7066 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7067 | |
| 7068 | // Move on window. |
| 7069 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7070 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7071 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7072 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7073 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7074 | mWindow->consumeDragEvent(false, 50, 50); |
| 7075 | mSecondWindow->assertNoEvents(); |
| 7076 | |
| 7077 | // Move to another window. |
| 7078 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7079 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7080 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7081 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7082 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7083 | mWindow->consumeDragEvent(true, 150, 50); |
| 7084 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7085 | |
| 7086 | // drop to another window. |
| 7087 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7088 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7089 | {150, 50})) |
| 7090 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7091 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7092 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7093 | mWindow->assertNoEvents(); |
| 7094 | mSecondWindow->assertNoEvents(); |
| 7095 | } |
| 7096 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7097 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7098 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7099 | |
| 7100 | // Move on window and keep button pressed. |
| 7101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7102 | injectMotionEvent(mDispatcher, |
| 7103 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7104 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 7105 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7106 | .x(50) |
| 7107 | .y(50)) |
| 7108 | .build())) |
| 7109 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7110 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7111 | mWindow->consumeDragEvent(false, 50, 50); |
| 7112 | mSecondWindow->assertNoEvents(); |
| 7113 | |
| 7114 | // Move to another window and release button, expect to drop item. |
| 7115 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7116 | injectMotionEvent(mDispatcher, |
| 7117 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7118 | .buttonState(0) |
| 7119 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7120 | .x(150) |
| 7121 | .y(50)) |
| 7122 | .build())) |
| 7123 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7124 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7125 | mWindow->assertNoEvents(); |
| 7126 | mSecondWindow->assertNoEvents(); |
| 7127 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7128 | |
| 7129 | // nothing to the window. |
| 7130 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7131 | injectMotionEvent(mDispatcher, |
| 7132 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 7133 | .buttonState(0) |
| 7134 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7135 | .x(150) |
| 7136 | .y(50)) |
| 7137 | .build())) |
| 7138 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7139 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7140 | mWindow->assertNoEvents(); |
| 7141 | mSecondWindow->assertNoEvents(); |
| 7142 | } |
| 7143 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7144 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7145 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 7146 | |
| 7147 | // Set second window invisible. |
| 7148 | mSecondWindow->setVisible(false); |
| 7149 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 7150 | |
| 7151 | // Move on window. |
| 7152 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7153 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7154 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7155 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7156 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7157 | mWindow->consumeDragEvent(false, 50, 50); |
| 7158 | mSecondWindow->assertNoEvents(); |
| 7159 | |
| 7160 | // Move to another window. |
| 7161 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7162 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7163 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7164 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7165 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7166 | mWindow->consumeDragEvent(true, 150, 50); |
| 7167 | mSecondWindow->assertNoEvents(); |
| 7168 | |
| 7169 | // drop to another window. |
| 7170 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7171 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7172 | {150, 50})) |
| 7173 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7174 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7175 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 7176 | mWindow->assertNoEvents(); |
| 7177 | mSecondWindow->assertNoEvents(); |
| 7178 | } |
| 7179 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7180 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7181 | // Ensure window could track pointerIds if it didn't support split touch. |
| 7182 | mWindow->setPreventSplitting(true); |
| 7183 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7184 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7185 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7186 | {50, 50})) |
| 7187 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7188 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7189 | |
| 7190 | const MotionEvent secondFingerDownEvent = |
| 7191 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7192 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7193 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7194 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7195 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50)) |
| 7196 | .build(); |
| 7197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7198 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7199 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7200 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7201 | mWindow->consumeMotionPointerDown(1 /* pointerIndex */); |
| 7202 | |
| 7203 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7204 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7205 | } |
| 7206 | |
| 7207 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 7208 | // First down on second window. |
| 7209 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7210 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7211 | {150, 50})) |
| 7212 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7213 | |
| 7214 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7215 | |
| 7216 | // Second down on first window. |
| 7217 | const MotionEvent secondFingerDownEvent = |
| 7218 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7219 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7220 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7221 | .pointer( |
| 7222 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7223 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7224 | .build(); |
| 7225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7226 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7227 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7228 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7229 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7230 | |
| 7231 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7232 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7233 | |
| 7234 | // Move on window. |
| 7235 | const MotionEvent secondFingerMoveEvent = |
| 7236 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7237 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7238 | .pointer( |
| 7239 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7240 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7241 | .build(); |
| 7242 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7243 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 7244 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 7245 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7246 | mWindow->consumeDragEvent(false, 50, 50); |
| 7247 | mSecondWindow->consumeMotionMove(); |
| 7248 | |
| 7249 | // Release the drag pointer should perform drop. |
| 7250 | const MotionEvent secondFingerUpEvent = |
| 7251 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7252 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7253 | .pointer( |
| 7254 | PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7255 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7256 | .build(); |
| 7257 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7258 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 7259 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 7260 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7261 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 7262 | mWindow->assertNoEvents(); |
| 7263 | mSecondWindow->consumeMotionMove(); |
| 7264 | } |
| 7265 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7266 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7267 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7268 | |
| 7269 | // Update window of second display. |
| 7270 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7271 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 7272 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 7273 | |
| 7274 | // Let second display has a touch state. |
| 7275 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7276 | injectMotionEvent(mDispatcher, |
| 7277 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7278 | AINPUT_SOURCE_TOUCHSCREEN) |
| 7279 | .displayId(SECOND_DISPLAY_ID) |
| 7280 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7281 | .x(100) |
| 7282 | .y(100)) |
| 7283 | .build())); |
| 7284 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 7285 | SECOND_DISPLAY_ID, 0 /* expectedFlag */); |
| 7286 | // Update window again. |
| 7287 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 7288 | |
| 7289 | // Move on window. |
| 7290 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7291 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7292 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7293 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7294 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7295 | mWindow->consumeDragEvent(false, 50, 50); |
| 7296 | mSecondWindow->assertNoEvents(); |
| 7297 | |
| 7298 | // Move to another window. |
| 7299 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7300 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7301 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7302 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7303 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7304 | mWindow->consumeDragEvent(true, 150, 50); |
| 7305 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7306 | |
| 7307 | // drop to another window. |
| 7308 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7309 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7310 | {150, 50})) |
| 7311 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7312 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7313 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7314 | mWindow->assertNoEvents(); |
| 7315 | mSecondWindow->assertNoEvents(); |
| 7316 | } |
| 7317 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7318 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 7319 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 7320 | // Move on window. |
| 7321 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7322 | injectMotionEvent(mDispatcher, |
| 7323 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 7324 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7325 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7326 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7327 | .x(50) |
| 7328 | .y(50)) |
| 7329 | .build())) |
| 7330 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7331 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7332 | mWindow->consumeDragEvent(false, 50, 50); |
| 7333 | mSecondWindow->assertNoEvents(); |
| 7334 | |
| 7335 | // Move to another window. |
| 7336 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7337 | injectMotionEvent(mDispatcher, |
| 7338 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 7339 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7340 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7341 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7342 | .x(150) |
| 7343 | .y(50)) |
| 7344 | .build())) |
| 7345 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7346 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7347 | mWindow->consumeDragEvent(true, 150, 50); |
| 7348 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7349 | |
| 7350 | // drop to another window. |
| 7351 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7352 | injectMotionEvent(mDispatcher, |
| 7353 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 7354 | .buttonState(0) |
| 7355 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7356 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7357 | .x(150) |
| 7358 | .y(50)) |
| 7359 | .build())) |
| 7360 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7361 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7362 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7363 | mWindow->assertNoEvents(); |
| 7364 | mSecondWindow->assertNoEvents(); |
| 7365 | } |
| 7366 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7367 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 7368 | |
| 7369 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 7370 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7371 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7372 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7373 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7374 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7375 | window->setFocusable(true); |
| 7376 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7377 | setFocusedWindow(window); |
| 7378 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7379 | |
| 7380 | // With the flag set, window should not get any input |
| 7381 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7382 | mDispatcher->notifyKey(&keyArgs); |
| 7383 | window->assertNoEvents(); |
| 7384 | |
| 7385 | NotifyMotionArgs motionArgs = |
| 7386 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7387 | ADISPLAY_ID_DEFAULT); |
| 7388 | mDispatcher->notifyMotion(&motionArgs); |
| 7389 | window->assertNoEvents(); |
| 7390 | |
| 7391 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7392 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7393 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 7394 | |
| 7395 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7396 | mDispatcher->notifyKey(&keyArgs); |
| 7397 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7398 | |
| 7399 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7400 | ADISPLAY_ID_DEFAULT); |
| 7401 | mDispatcher->notifyMotion(&motionArgs); |
| 7402 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7403 | window->assertNoEvents(); |
| 7404 | } |
| 7405 | |
| 7406 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 7407 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7408 | std::make_shared<FakeApplicationHandle>(); |
| 7409 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7410 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7411 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7412 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7413 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7414 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7415 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7416 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7417 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7418 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7419 | window->setOwnerInfo(222, 222); |
| 7420 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7421 | window->setFocusable(true); |
| 7422 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7423 | setFocusedWindow(window); |
| 7424 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7425 | |
| 7426 | // With the flag set, window should not get any input |
| 7427 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7428 | mDispatcher->notifyKey(&keyArgs); |
| 7429 | window->assertNoEvents(); |
| 7430 | |
| 7431 | NotifyMotionArgs motionArgs = |
| 7432 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7433 | ADISPLAY_ID_DEFAULT); |
| 7434 | mDispatcher->notifyMotion(&motionArgs); |
| 7435 | window->assertNoEvents(); |
| 7436 | |
| 7437 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7438 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7439 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7440 | |
| 7441 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7442 | mDispatcher->notifyKey(&keyArgs); |
| 7443 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7444 | |
| 7445 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7446 | ADISPLAY_ID_DEFAULT); |
| 7447 | mDispatcher->notifyMotion(&motionArgs); |
| 7448 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7449 | window->assertNoEvents(); |
| 7450 | } |
| 7451 | |
| 7452 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 7453 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 7454 | std::make_shared<FakeApplicationHandle>(); |
| 7455 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7456 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 7457 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7458 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 7459 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7460 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7461 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7462 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7463 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7464 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 7465 | window->setOwnerInfo(222, 222); |
| 7466 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7467 | window->setFocusable(true); |
| 7468 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 7469 | setFocusedWindow(window); |
| 7470 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 7471 | |
| 7472 | // With the flag set, window should not get any input |
| 7473 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 7474 | mDispatcher->notifyKey(&keyArgs); |
| 7475 | window->assertNoEvents(); |
| 7476 | |
| 7477 | NotifyMotionArgs motionArgs = |
| 7478 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7479 | ADISPLAY_ID_DEFAULT); |
| 7480 | mDispatcher->notifyMotion(&motionArgs); |
| 7481 | window->assertNoEvents(); |
| 7482 | |
| 7483 | // When the window is no longer obscured because it went on top, it should get input |
| 7484 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 7485 | |
| 7486 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 7487 | mDispatcher->notifyKey(&keyArgs); |
| 7488 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 7489 | |
| 7490 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7491 | ADISPLAY_ID_DEFAULT); |
| 7492 | mDispatcher->notifyMotion(&motionArgs); |
| 7493 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7494 | window->assertNoEvents(); |
| 7495 | } |
| 7496 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7497 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 7498 | protected: |
| 7499 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7500 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7501 | sp<FakeWindowHandle> mWindow; |
| 7502 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7503 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7504 | |
| 7505 | void SetUp() override { |
| 7506 | InputDispatcherTest::SetUp(); |
| 7507 | |
| 7508 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7509 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7510 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7511 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7512 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7513 | mSecondWindow = |
| 7514 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7515 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7516 | mThirdWindow = |
| 7517 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 7518 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 7519 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7520 | |
| 7521 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7522 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 7523 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 7524 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7525 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7526 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7527 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 7528 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7529 | WINDOW_UID, true /* hasPermission */, |
| 7530 | ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7531 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 7532 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7533 | mThirdWindow->assertNoEvents(); |
| 7534 | } |
| 7535 | |
| 7536 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 7537 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
| 7538 | SECONDARY_WINDOW_UID, true /* hasPermission */, |
| 7539 | SECOND_DISPLAY_ID)) { |
| 7540 | mWindow->assertNoEvents(); |
| 7541 | mSecondWindow->assertNoEvents(); |
| 7542 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7543 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7544 | } |
| 7545 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7546 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 7547 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7548 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 7549 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7550 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 7551 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7552 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7553 | } |
| 7554 | }; |
| 7555 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7556 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7557 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7558 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 7559 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 7560 | false /* hasPermission */); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7561 | } |
| 7562 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7563 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 7564 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7565 | int32_t ownerPid = windowInfo.ownerPid; |
| 7566 | int32_t ownerUid = windowInfo.ownerUid; |
| 7567 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 7568 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7569 | ownerUid, false /*hasPermission*/, |
| 7570 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7571 | mWindow->assertNoEvents(); |
| 7572 | mSecondWindow->assertNoEvents(); |
| 7573 | } |
| 7574 | |
| 7575 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 7576 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7577 | int32_t ownerPid = windowInfo.ownerPid; |
| 7578 | int32_t ownerUid = windowInfo.ownerUid; |
| 7579 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7580 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
| 7581 | ownerUid, true /*hasPermission*/); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7582 | } |
| 7583 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7584 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7585 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 7586 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 7587 | windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7588 | true /*hasPermission*/, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7589 | mWindow->assertNoEvents(); |
| 7590 | mSecondWindow->assertNoEvents(); |
| 7591 | } |
| 7592 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 7593 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 7594 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 7595 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7596 | windowInfo.ownerPid, windowInfo.ownerUid, |
| 7597 | true /*hasPermission*/, SECOND_DISPLAY_ID)); |
| 7598 | mWindow->assertNoEvents(); |
| 7599 | mSecondWindow->assertNoEvents(); |
| 7600 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 7601 | } |
| 7602 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7603 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 7604 | // Interact with the window first. |
| 7605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 7606 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7607 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7608 | |
| 7609 | // Then remove focus. |
| 7610 | mWindow->setFocusable(false); |
| 7611 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7612 | |
| 7613 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 7614 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 7615 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 7616 | windowInfo.ownerPid, windowInfo.ownerUid, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 7617 | false /*hasPermission*/, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 7618 | } |
| 7619 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7620 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 7621 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7622 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7623 | std::shared_ptr<FakeApplicationHandle> application = |
| 7624 | std::make_shared<FakeApplicationHandle>(); |
| 7625 | std::string name = "Fake Spy "; |
| 7626 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7627 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7628 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7629 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7630 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7631 | return spy; |
| 7632 | } |
| 7633 | |
| 7634 | sp<FakeWindowHandle> createForeground() { |
| 7635 | std::shared_ptr<FakeApplicationHandle> application = |
| 7636 | std::make_shared<FakeApplicationHandle>(); |
| 7637 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7638 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7639 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7640 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7641 | return window; |
| 7642 | } |
| 7643 | |
| 7644 | private: |
| 7645 | int mSpyCount{0}; |
| 7646 | }; |
| 7647 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7648 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7649 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7650 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 7651 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 7652 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 7653 | ScopedSilentDeath _silentDeath; |
| 7654 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7655 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 7656 | spy->setTrustedOverlay(false); |
| 7657 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 7658 | ".* not a trusted overlay"); |
| 7659 | } |
| 7660 | |
| 7661 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7662 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 7663 | */ |
| 7664 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7665 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7666 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 7667 | |
| 7668 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7669 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7670 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7671 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7672 | } |
| 7673 | |
| 7674 | /** |
| 7675 | * Verify the order in which different input windows receive events. The touched foreground window |
| 7676 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 7677 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 7678 | * receive events before ones belows it. |
| 7679 | * |
| 7680 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 7681 | * spy1, spy2, window, spy3. |
| 7682 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 7683 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 7684 | * window. |
| 7685 | */ |
| 7686 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 7687 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7688 | auto spy1 = createSpy(); |
| 7689 | auto spy2 = createSpy(); |
| 7690 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7691 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 7692 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 7693 | const size_t numChannels = channels.size(); |
| 7694 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 7695 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7696 | if (!epollFd.ok()) { |
| 7697 | FAIL() << "Failed to create epoll fd"; |
| 7698 | } |
| 7699 | |
| 7700 | for (size_t i = 0; i < numChannels; i++) { |
| 7701 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 7702 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 7703 | FAIL() << "Failed to add fd to epoll"; |
| 7704 | } |
| 7705 | } |
| 7706 | |
| 7707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7708 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7709 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7710 | |
| 7711 | std::vector<size_t> eventOrder; |
| 7712 | std::vector<struct epoll_event> events(numChannels); |
| 7713 | for (;;) { |
| 7714 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 7715 | (100ms).count()); |
| 7716 | if (nFds < 0) { |
| 7717 | FAIL() << "Failed to call epoll_wait"; |
| 7718 | } |
| 7719 | if (nFds == 0) { |
| 7720 | break; // epoll_wait timed out |
| 7721 | } |
| 7722 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 7723 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 7724 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7725 | channels[i]->consumeMotionDown(); |
| 7726 | } |
| 7727 | } |
| 7728 | |
| 7729 | // Verify the order in which the events were received. |
| 7730 | EXPECT_EQ(3u, eventOrder.size()); |
| 7731 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 7732 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 7733 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 7734 | } |
| 7735 | |
| 7736 | /** |
| 7737 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 7738 | */ |
| 7739 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 7740 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7741 | auto spy = createSpy(); |
| 7742 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7743 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7744 | |
| 7745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7746 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7747 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7748 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7749 | spy->assertNoEvents(); |
| 7750 | } |
| 7751 | |
| 7752 | /** |
| 7753 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 7754 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 7755 | * to the window. |
| 7756 | */ |
| 7757 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 7758 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7759 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7760 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 7761 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7762 | |
| 7763 | // Inject an event outside the spy window's touchable region. |
| 7764 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7765 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7767 | window->consumeMotionDown(); |
| 7768 | spy->assertNoEvents(); |
| 7769 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7770 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7771 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7772 | window->consumeMotionUp(); |
| 7773 | spy->assertNoEvents(); |
| 7774 | |
| 7775 | // Inject an event inside the spy window's touchable region. |
| 7776 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7777 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7778 | {5, 10})) |
| 7779 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7780 | window->consumeMotionDown(); |
| 7781 | spy->consumeMotionDown(); |
| 7782 | } |
| 7783 | |
| 7784 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7785 | * 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] | 7786 | * 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] | 7787 | */ |
| 7788 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 7789 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7790 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7791 | auto spy = createSpy(); |
| 7792 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7793 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7794 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 7795 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7796 | |
| 7797 | // Inject an event outside the spy window's frame and touchable region. |
| 7798 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7799 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7800 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7801 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7802 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7803 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7804 | } |
| 7805 | |
| 7806 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7807 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 7808 | * pointers that are down within its bounds. |
| 7809 | */ |
| 7810 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 7811 | auto windowLeft = createForeground(); |
| 7812 | windowLeft->setFrame({0, 0, 100, 200}); |
| 7813 | auto windowRight = createForeground(); |
| 7814 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7815 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7816 | spy->setFrame({0, 0, 200, 200}); |
| 7817 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 7818 | |
| 7819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7820 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7821 | {50, 50})) |
| 7822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7823 | windowLeft->consumeMotionDown(); |
| 7824 | spy->consumeMotionDown(); |
| 7825 | |
| 7826 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7827 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7828 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7829 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7830 | .pointer( |
| 7831 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7832 | .build(); |
| 7833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7834 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7835 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7836 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7837 | windowRight->consumeMotionDown(); |
| 7838 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7839 | } |
| 7840 | |
| 7841 | /** |
| 7842 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 7843 | * the spy should receive the second pointer with ACTION_DOWN. |
| 7844 | */ |
| 7845 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 7846 | auto window = createForeground(); |
| 7847 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7848 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7849 | spyRight->setFrame({100, 0, 200, 200}); |
| 7850 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 7851 | |
| 7852 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7853 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7854 | {50, 50})) |
| 7855 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7856 | window->consumeMotionDown(); |
| 7857 | spyRight->assertNoEvents(); |
| 7858 | |
| 7859 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7860 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 7861 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7862 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7863 | .pointer( |
| 7864 | PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 7865 | .build(); |
| 7866 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7867 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7868 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7869 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7870 | window->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 7871 | spyRight->consumeMotionDown(); |
| 7872 | } |
| 7873 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7874 | /** |
| 7875 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 7876 | * windows should be allowed to control split touch. |
| 7877 | */ |
| 7878 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7879 | // 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] | 7880 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7881 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7882 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7883 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7884 | auto window = createForeground(); |
| 7885 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7886 | |
| 7887 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7888 | |
| 7889 | // First finger down, no window touched. |
| 7890 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7891 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7892 | {100, 200})) |
| 7893 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7894 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7895 | window->assertNoEvents(); |
| 7896 | |
| 7897 | // Second finger down on window, the window should receive touch down. |
| 7898 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 7899 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7900 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7901 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 7902 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 7903 | .x(100) |
| 7904 | .y(200)) |
| 7905 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7906 | .build(); |
| 7907 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7908 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7909 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7910 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7911 | |
| 7912 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7913 | spy->consumeMotionPointerDown(1 /* pointerIndex */); |
| 7914 | } |
| 7915 | |
| 7916 | /** |
| 7917 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 7918 | * do not receive key events. |
| 7919 | */ |
| 7920 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7921 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7922 | spy->setFocusable(false); |
| 7923 | |
| 7924 | auto window = createForeground(); |
| 7925 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7926 | setFocusedWindow(window); |
| 7927 | window->consumeFocusEvent(true); |
| 7928 | |
| 7929 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7930 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7931 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7932 | |
| 7933 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7934 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7935 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7936 | |
| 7937 | spy->assertNoEvents(); |
| 7938 | } |
| 7939 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 7940 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 7941 | |
| 7942 | /** |
| 7943 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 7944 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 7945 | */ |
| 7946 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 7947 | auto window = createForeground(); |
| 7948 | auto spy1 = createSpy(); |
| 7949 | auto spy2 = createSpy(); |
| 7950 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 7951 | |
| 7952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7953 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7954 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7955 | window->consumeMotionDown(); |
| 7956 | spy1->consumeMotionDown(); |
| 7957 | spy2->consumeMotionDown(); |
| 7958 | |
| 7959 | // Pilfer pointers from the second spy window. |
| 7960 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 7961 | spy2->assertNoEvents(); |
| 7962 | spy1->consumeMotionCancel(); |
| 7963 | window->consumeMotionCancel(); |
| 7964 | |
| 7965 | // The rest of the gesture should only be sent to the second spy window. |
| 7966 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7967 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7968 | ADISPLAY_ID_DEFAULT)) |
| 7969 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7970 | spy2->consumeMotionMove(); |
| 7971 | spy1->assertNoEvents(); |
| 7972 | window->assertNoEvents(); |
| 7973 | } |
| 7974 | |
| 7975 | /** |
| 7976 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 7977 | * in the middle of the gesture. |
| 7978 | */ |
| 7979 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 7980 | auto window = createForeground(); |
| 7981 | auto spy = createSpy(); |
| 7982 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 7983 | |
| 7984 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7985 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7986 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7987 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7988 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7989 | |
| 7990 | window->releaseChannel(); |
| 7991 | |
| 7992 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 7993 | |
| 7994 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7995 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 7996 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7997 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7998 | } |
| 7999 | |
| 8000 | /** |
| 8001 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 8002 | * the spy, but not to any other windows. |
| 8003 | */ |
| 8004 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 8005 | auto spy = createSpy(); |
| 8006 | auto window = createForeground(); |
| 8007 | |
| 8008 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8009 | |
| 8010 | // First finger down on the window and the spy. |
| 8011 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8012 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8013 | {100, 200})) |
| 8014 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8015 | spy->consumeMotionDown(); |
| 8016 | window->consumeMotionDown(); |
| 8017 | |
| 8018 | // Spy window pilfers the pointers. |
| 8019 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8020 | window->consumeMotionCancel(); |
| 8021 | |
| 8022 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 8023 | const MotionEvent secondFingerDownEvent = |
| 8024 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8025 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8026 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 8027 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 8028 | .x(100) |
| 8029 | .y(200)) |
| 8030 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8031 | .build(); |
| 8032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8033 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8034 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8035 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8036 | |
| 8037 | spy->consumeMotionPointerDown(1 /*pointerIndex*/); |
| 8038 | |
| 8039 | // Third finger goes down outside all windows, so injection should fail. |
| 8040 | const MotionEvent thirdFingerDownEvent = |
| 8041 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8042 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8043 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 8044 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 8045 | .x(100) |
| 8046 | .y(200)) |
| 8047 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8048 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) |
| 8049 | .build(); |
| 8050 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 8051 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8052 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8053 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8054 | |
| 8055 | spy->assertNoEvents(); |
| 8056 | window->assertNoEvents(); |
| 8057 | } |
| 8058 | |
| 8059 | /** |
| 8060 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 8061 | */ |
| 8062 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 8063 | auto spy = createSpy(); |
| 8064 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8065 | auto window = createForeground(); |
| 8066 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8067 | |
| 8068 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8069 | |
| 8070 | // First finger down on the window only |
| 8071 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8072 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8073 | {150, 150})) |
| 8074 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8075 | window->consumeMotionDown(); |
| 8076 | |
| 8077 | // Second finger down on the spy and window |
| 8078 | const MotionEvent secondFingerDownEvent = |
| 8079 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8080 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8081 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 8082 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 8083 | .x(150) |
| 8084 | .y(150)) |
| 8085 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8086 | .build(); |
| 8087 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8088 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8089 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8090 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8091 | spy->consumeMotionDown(); |
| 8092 | window->consumeMotionPointerDown(1); |
| 8093 | |
| 8094 | // Third finger down on the spy and window |
| 8095 | const MotionEvent thirdFingerDownEvent = |
| 8096 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8097 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8098 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 8099 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 8100 | .x(150) |
| 8101 | .y(150)) |
| 8102 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8103 | .pointer(PointerBuilder(/* id */ 2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8104 | .build(); |
| 8105 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8106 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8107 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8108 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8109 | spy->consumeMotionPointerDown(1); |
| 8110 | window->consumeMotionPointerDown(2); |
| 8111 | |
| 8112 | // Spy window pilfers the pointers. |
| 8113 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8114 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8115 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8116 | |
| 8117 | spy->assertNoEvents(); |
| 8118 | window->assertNoEvents(); |
| 8119 | } |
| 8120 | |
| 8121 | /** |
| 8122 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 8123 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 8124 | * window, then that window should receive ACTION_CANCEL. |
| 8125 | */ |
| 8126 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 8127 | auto spy = createSpy(); |
| 8128 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8129 | auto window = createForeground(); |
| 8130 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8131 | |
| 8132 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8133 | |
| 8134 | // First finger down on both spy and window |
| 8135 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8136 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8137 | {10, 10})) |
| 8138 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8139 | window->consumeMotionDown(); |
| 8140 | spy->consumeMotionDown(); |
| 8141 | |
| 8142 | // Second finger down on the spy and window |
| 8143 | const MotionEvent secondFingerDownEvent = |
| 8144 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8145 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8146 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 8147 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8148 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8149 | .build(); |
| 8150 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8151 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8152 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8153 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8154 | spy->consumeMotionPointerDown(1); |
| 8155 | window->consumeMotionPointerDown(1); |
| 8156 | |
| 8157 | // Spy window pilfers the pointers. |
| 8158 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8159 | window->consumeMotionCancel(); |
| 8160 | |
| 8161 | spy->assertNoEvents(); |
| 8162 | window->assertNoEvents(); |
| 8163 | } |
| 8164 | |
| 8165 | /** |
| 8166 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 8167 | * be sent to other windows |
| 8168 | */ |
| 8169 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 8170 | auto spy = createSpy(); |
| 8171 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8172 | auto window = createForeground(); |
| 8173 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8174 | |
| 8175 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8176 | |
| 8177 | // First finger down on both window and spy |
| 8178 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8179 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8180 | {10, 10})) |
| 8181 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8182 | window->consumeMotionDown(); |
| 8183 | spy->consumeMotionDown(); |
| 8184 | |
| 8185 | // Spy window pilfers the pointers. |
| 8186 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8187 | window->consumeMotionCancel(); |
| 8188 | |
| 8189 | // Second finger down on the window only |
| 8190 | const MotionEvent secondFingerDownEvent = |
| 8191 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8192 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8193 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 8194 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8195 | .pointer(PointerBuilder(/* id */ 1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 8196 | .x(150) |
| 8197 | .y(150)) |
| 8198 | .build(); |
| 8199 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8200 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8201 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8202 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8203 | window->consumeMotionDown(); |
| 8204 | window->assertNoEvents(); |
| 8205 | |
| 8206 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 8207 | spy->consumeMotionMove(); |
| 8208 | spy->assertNoEvents(); |
| 8209 | } |
| 8210 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8211 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 8212 | public: |
| 8213 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 8214 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8215 | std::make_shared<FakeApplicationHandle>(); |
| 8216 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8217 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 8218 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8219 | overlay->setFocusable(false); |
| 8220 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8221 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8222 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8223 | overlay->setTrustedOverlay(true); |
| 8224 | |
| 8225 | std::shared_ptr<FakeApplicationHandle> application = |
| 8226 | std::make_shared<FakeApplicationHandle>(); |
| 8227 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8228 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 8229 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8230 | window->setFocusable(true); |
| 8231 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8232 | |
| 8233 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8234 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8235 | setFocusedWindow(window); |
| 8236 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 8237 | return {std::move(overlay), std::move(window)}; |
| 8238 | } |
| 8239 | |
| 8240 | void sendFingerEvent(int32_t action) { |
| 8241 | NotifyMotionArgs motionArgs = |
| 8242 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 8243 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 8244 | mDispatcher->notifyMotion(&motionArgs); |
| 8245 | } |
| 8246 | |
| 8247 | void sendStylusEvent(int32_t action) { |
| 8248 | NotifyMotionArgs motionArgs = |
| 8249 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 8250 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 8251 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 8252 | mDispatcher->notifyMotion(&motionArgs); |
| 8253 | } |
| 8254 | }; |
| 8255 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8256 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 8257 | |
| 8258 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 8259 | ScopedSilentDeath _silentDeath; |
| 8260 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8261 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8262 | overlay->setTrustedOverlay(false); |
| 8263 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 8264 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 8265 | ".* not a trusted overlay"); |
| 8266 | } |
| 8267 | |
| 8268 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 8269 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8270 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8271 | |
| 8272 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8273 | overlay->consumeMotionDown(); |
| 8274 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8275 | overlay->consumeMotionUp(); |
| 8276 | |
| 8277 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8278 | window->consumeMotionDown(); |
| 8279 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 8280 | window->consumeMotionUp(); |
| 8281 | |
| 8282 | overlay->assertNoEvents(); |
| 8283 | window->assertNoEvents(); |
| 8284 | } |
| 8285 | |
| 8286 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 8287 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8288 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8289 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8290 | |
| 8291 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8292 | overlay->consumeMotionDown(); |
| 8293 | window->consumeMotionDown(); |
| 8294 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8295 | overlay->consumeMotionUp(); |
| 8296 | window->consumeMotionUp(); |
| 8297 | |
| 8298 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8299 | window->consumeMotionDown(); |
| 8300 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 8301 | window->consumeMotionUp(); |
| 8302 | |
| 8303 | overlay->assertNoEvents(); |
| 8304 | window->assertNoEvents(); |
| 8305 | } |
| 8306 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 8307 | /** |
| 8308 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 8309 | * The scenario is as follows: |
| 8310 | * - The stylus interceptor overlay is configured as a spy window. |
| 8311 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 8312 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 8313 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 8314 | */ |
| 8315 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 8316 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 8317 | overlay->setSpy(true); |
| 8318 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8319 | |
| 8320 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 8321 | overlay->consumeMotionDown(); |
| 8322 | window->consumeMotionDown(); |
| 8323 | |
| 8324 | // The interceptor pilfers the pointers. |
| 8325 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 8326 | window->consumeMotionCancel(); |
| 8327 | |
| 8328 | // The interceptor configures itself so that it is no longer a spy. |
| 8329 | overlay->setSpy(false); |
| 8330 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 8331 | |
| 8332 | // It continues to receive the rest of the stylus gesture. |
| 8333 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 8334 | overlay->consumeMotionMove(); |
| 8335 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 8336 | overlay->consumeMotionUp(); |
| 8337 | |
| 8338 | window->assertNoEvents(); |
| 8339 | } |
| 8340 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8341 | struct User { |
| 8342 | int32_t mPid; |
| 8343 | int32_t mUid; |
| 8344 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 8345 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 8346 | |
| 8347 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 8348 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 8349 | |
| 8350 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 8351 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 8352 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 8353 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8354 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 8355 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8356 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 8357 | } |
| 8358 | |
| 8359 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
| 8360 | return inputdispatcher::injectKey(mDispatcher, action, 0 /* repeatCount*/, ADISPLAY_ID_NONE, |
| 8361 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8362 | INJECT_EVENT_TIMEOUT, false /*allowKeyRepeat*/, {mUid}, |
| 8363 | mPolicyFlags); |
| 8364 | } |
| 8365 | |
| 8366 | sp<FakeWindowHandle> createWindow() const { |
| 8367 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8368 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8369 | sp<FakeWindowHandle> window = |
| 8370 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 8371 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8372 | window->setOwnerInfo(mPid, mUid); |
| 8373 | return window; |
| 8374 | } |
| 8375 | }; |
| 8376 | |
| 8377 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 8378 | |
| 8379 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 8380 | auto owner = User(mDispatcher, 10, 11); |
| 8381 | auto window = owner.createWindow(); |
| 8382 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8383 | |
| 8384 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8385 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8386 | window->consumeMotionDown(); |
| 8387 | |
| 8388 | setFocusedWindow(window); |
| 8389 | window->consumeFocusEvent(true); |
| 8390 | |
| 8391 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8392 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8393 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8394 | } |
| 8395 | |
| 8396 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 8397 | auto owner = User(mDispatcher, 10, 11); |
| 8398 | auto window = owner.createWindow(); |
| 8399 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8400 | |
| 8401 | auto rando = User(mDispatcher, 20, 21); |
| 8402 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8403 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8404 | |
| 8405 | setFocusedWindow(window); |
| 8406 | window->consumeFocusEvent(true); |
| 8407 | |
| 8408 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 8409 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 8410 | window->assertNoEvents(); |
| 8411 | } |
| 8412 | |
| 8413 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 8414 | auto owner = User(mDispatcher, 10, 11); |
| 8415 | auto window = owner.createWindow(); |
| 8416 | auto spy = owner.createWindow(); |
| 8417 | spy->setSpy(true); |
| 8418 | spy->setTrustedOverlay(true); |
| 8419 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8420 | |
| 8421 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8422 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8423 | spy->consumeMotionDown(); |
| 8424 | window->consumeMotionDown(); |
| 8425 | } |
| 8426 | |
| 8427 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 8428 | auto owner = User(mDispatcher, 10, 11); |
| 8429 | auto window = owner.createWindow(); |
| 8430 | |
| 8431 | auto rando = User(mDispatcher, 20, 21); |
| 8432 | auto randosSpy = rando.createWindow(); |
| 8433 | randosSpy->setSpy(true); |
| 8434 | randosSpy->setTrustedOverlay(true); |
| 8435 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8436 | |
| 8437 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 8438 | // not receive the event. |
| 8439 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8440 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8441 | randosSpy->assertNoEvents(); |
| 8442 | window->consumeMotionDown(); |
| 8443 | } |
| 8444 | |
| 8445 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 8446 | auto owner = User(mDispatcher, 10, 11); |
| 8447 | auto window = owner.createWindow(); |
| 8448 | |
| 8449 | auto rando = User(mDispatcher, 20, 21); |
| 8450 | auto randosSpy = rando.createWindow(); |
| 8451 | randosSpy->setSpy(true); |
| 8452 | randosSpy->setTrustedOverlay(true); |
| 8453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 8454 | |
| 8455 | // A user that has injection permission can inject into any window. |
| 8456 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8457 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8458 | ADISPLAY_ID_DEFAULT)); |
| 8459 | randosSpy->consumeMotionDown(); |
| 8460 | window->consumeMotionDown(); |
| 8461 | |
| 8462 | setFocusedWindow(randosSpy); |
| 8463 | randosSpy->consumeFocusEvent(true); |
| 8464 | |
| 8465 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 8466 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8467 | window->assertNoEvents(); |
| 8468 | } |
| 8469 | |
| 8470 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 8471 | auto owner = User(mDispatcher, 10, 11); |
| 8472 | auto window = owner.createWindow(); |
| 8473 | |
| 8474 | auto rando = User(mDispatcher, 20, 21); |
| 8475 | auto randosWindow = rando.createWindow(); |
| 8476 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 8477 | randosWindow->setWatchOutsideTouch(true); |
| 8478 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 8479 | |
| 8480 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 8481 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8482 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 8483 | window->consumeMotionDown(); |
| 8484 | randosWindow->consumeMotionOutside(); |
| 8485 | } |
| 8486 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 8487 | } // namespace android::inputdispatcher |