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 | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 61 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 62 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 63 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 64 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 65 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 66 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 67 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 68 | /** |
| 69 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 70 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 71 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 72 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 73 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 74 | * pointer id=0 leaves but the pointer id=1 remains. |
| 75 | */ |
| 76 | static constexpr int32_t POINTER_0_DOWN = |
| 77 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 78 | static constexpr int32_t POINTER_1_DOWN = |
| 79 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 80 | static constexpr int32_t POINTER_2_DOWN = |
| 81 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 82 | static constexpr int32_t POINTER_3_DOWN = |
| 83 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 84 | static constexpr int32_t POINTER_0_UP = |
| 85 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 86 | static constexpr int32_t POINTER_1_UP = |
| 87 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 88 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 89 | // 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] | 90 | static constexpr int32_t WINDOW_PID = 999; |
| 91 | static constexpr int32_t WINDOW_UID = 1001; |
| 92 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 93 | // The default pid and uid for the windows created on the secondary display by the test. |
| 94 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; |
| 95 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; |
| 96 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 97 | // The default policy flags to use for event injection by tests. |
| 98 | 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] | 99 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 100 | // An arbitrary pid of the gesture monitor window |
| 101 | static constexpr int32_t MONITOR_PID = 2001; |
| 102 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 103 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 104 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 105 | static constexpr int expectedWallpaperFlags = |
| 106 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 107 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 108 | struct PointF { |
| 109 | float x; |
| 110 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 111 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 112 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 113 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 114 | /** |
| 115 | * Return a DOWN key event with KEYCODE_A. |
| 116 | */ |
| 117 | static KeyEvent getTestKeyEvent() { |
| 118 | KeyEvent event; |
| 119 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 120 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 121 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 122 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 123 | return event; |
| 124 | } |
| 125 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 126 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 127 | ASSERT_EQ(expectedAction, receivedAction) |
| 128 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 129 | << MotionEvent::actionToString(receivedAction); |
| 130 | } |
| 131 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 132 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 133 | bool matches = action == arg.getAction(); |
| 134 | if (!matches) { |
| 135 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 136 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 137 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 138 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 139 | if (!matches) { |
| 140 | *result_listener << "; "; |
| 141 | } |
| 142 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 143 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 144 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 145 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 146 | if (!matches) { |
| 147 | *result_listener << "; "; |
| 148 | } |
| 149 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 150 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 151 | } |
| 152 | return matches; |
| 153 | } |
| 154 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 155 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 156 | return arg.getDownTime() == downTime; |
| 157 | } |
| 158 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 159 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { |
| 160 | return arg.getDisplayId() == displayId; |
| 161 | } |
| 162 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 163 | MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") { |
| 164 | return arg.getDeviceId() == deviceId; |
| 165 | } |
| 166 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 167 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 168 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 169 | << inputEventSourceToString(arg.getSource()); |
| 170 | return arg.getSource() == source; |
| 171 | } |
| 172 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 173 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { |
| 174 | return arg.getFlags() == flags; |
| 175 | } |
| 176 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 177 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 178 | if (arg.getPointerCount() != 1) { |
| 179 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 180 | return false; |
| 181 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 182 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 185 | MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") { |
| 186 | return arg.getPointerCount() == pointerCount; |
| 187 | } |
| 188 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 189 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 190 | // Build a map for the received pointers, by pointer id |
| 191 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 192 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 193 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 194 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 195 | } |
| 196 | return pointers == actualPointers; |
| 197 | } |
| 198 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 199 | // --- FakeInputDispatcherPolicy --- |
| 200 | |
| 201 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 202 | InputDispatcherConfiguration mConfig; |
| 203 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 204 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 205 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 207 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | |
| 209 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 210 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 211 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 212 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 213 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
| 214 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_KEY); |
| 215 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 216 | |
| 217 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 218 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 219 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 220 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 221 | } |
| 222 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 223 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 224 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
| 225 | ASSERT_EQ(event.getType(), AINPUT_EVENT_TYPE_MOTION); |
| 226 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 227 | |
| 228 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 229 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 230 | EXPECT_EQ(motionEvent.getAction(), args.action); |
| 231 | EXPECT_EQ(motionEvent.getX(0), point.x); |
| 232 | EXPECT_EQ(motionEvent.getY(0), point.y); |
| 233 | EXPECT_EQ(motionEvent.getRawX(0), point.x); |
| 234 | EXPECT_EQ(motionEvent.getRawY(0), point.y); |
| 235 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 236 | } |
| 237 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 238 | void assertFilterInputEventWasNotCalled() { |
| 239 | std::scoped_lock lock(mLock); |
| 240 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 241 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 242 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 243 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 244 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 245 | ASSERT_TRUE(mConfigurationChangedTime) |
| 246 | << "Timed out waiting for configuration changed call"; |
| 247 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 248 | mConfigurationChangedTime = std::nullopt; |
| 249 | } |
| 250 | |
| 251 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 252 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 253 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 254 | // 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] | 255 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 256 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 257 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 258 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 259 | mLastNotifySwitch = std::nullopt; |
| 260 | } |
| 261 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 262 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 263 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 264 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 265 | mOnPointerDownToken.clear(); |
| 266 | } |
| 267 | |
| 268 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 269 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 270 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 271 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 274 | // This function must be called soon after the expected ANR timer starts, |
| 275 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 276 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 277 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 278 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 279 | std::unique_lock lock(mLock); |
| 280 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 281 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 282 | ASSERT_NO_FATAL_FAILURE( |
| 283 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 284 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 287 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 288 | const sp<WindowInfoHandle>& window) { |
| 289 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 290 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 291 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 294 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 295 | const sp<IBinder>& expectedToken, |
| 296 | int32_t expectedPid) { |
| 297 | std::unique_lock lock(mLock); |
| 298 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 299 | AnrResult result; |
| 300 | ASSERT_NO_FATAL_FAILURE(result = |
| 301 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 302 | const auto& [token, pid] = result; |
| 303 | ASSERT_EQ(expectedToken, token); |
| 304 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 307 | /** 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] | 308 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 309 | std::unique_lock lock(mLock); |
| 310 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 311 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 312 | const auto& [token, _] = result; |
| 313 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 316 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 317 | int32_t expectedPid) { |
| 318 | std::unique_lock lock(mLock); |
| 319 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 320 | AnrResult result; |
| 321 | ASSERT_NO_FATAL_FAILURE( |
| 322 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 323 | const auto& [token, pid] = result; |
| 324 | ASSERT_EQ(expectedToken, token); |
| 325 | ASSERT_EQ(expectedPid, pid); |
| 326 | } |
| 327 | |
| 328 | /** 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] | 329 | sp<IBinder> getResponsiveWindowToken() { |
| 330 | std::unique_lock lock(mLock); |
| 331 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 332 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 333 | const auto& [token, _] = result; |
| 334 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void assertNotifyAnrWasNotCalled() { |
| 338 | std::scoped_lock lock(mLock); |
| 339 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 340 | ASSERT_TRUE(mAnrWindows.empty()); |
| 341 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 342 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 343 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 346 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 347 | mConfig.keyRepeatTimeout = timeout; |
| 348 | mConfig.keyRepeatDelay = delay; |
| 349 | } |
| 350 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 351 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 352 | std::unique_lock lock(mLock); |
| 353 | base::ScopedLockAssertion assumeLocked(mLock); |
| 354 | |
| 355 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 356 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 357 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 358 | enabled; |
| 359 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 360 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 361 | << ") to be called."; |
| 362 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 363 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 364 | auto request = *mPointerCaptureRequest; |
| 365 | mPointerCaptureRequest.reset(); |
| 366 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void assertSetPointerCaptureNotCalled() { |
| 370 | std::unique_lock lock(mLock); |
| 371 | base::ScopedLockAssertion assumeLocked(mLock); |
| 372 | |
| 373 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 374 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 375 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 376 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 377 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 378 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 379 | } |
| 380 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 381 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 382 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 383 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 384 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 385 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 386 | } |
| 387 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 388 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 389 | std::unique_lock lock(mLock); |
| 390 | base::ScopedLockAssertion assumeLocked(mLock); |
| 391 | std::optional<sp<IBinder>> receivedToken = |
| 392 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 393 | mNotifyInputChannelBroken); |
| 394 | ASSERT_TRUE(receivedToken.has_value()); |
| 395 | ASSERT_EQ(token, *receivedToken); |
| 396 | } |
| 397 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 398 | /** |
| 399 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 400 | */ |
| 401 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 402 | mInterceptKeyTimeout = timeout; |
| 403 | } |
| 404 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 405 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 406 | std::mutex mLock; |
| 407 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 408 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 409 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 410 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 411 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 412 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 413 | |
| 414 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 415 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 416 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 417 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 418 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 419 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 420 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 421 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 422 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 423 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 424 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 425 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 426 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 427 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 428 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 429 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 430 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 431 | // first entry from the container and erase it. |
| 432 | template <class T> |
| 433 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 434 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 435 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 436 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 437 | // before checking if ANR was called. |
| 438 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 439 | // to provide it some time to act. 100ms seems reasonable. |
| 440 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 441 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 442 | std::optional<T> token = |
| 443 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 444 | if (!token.has_value()) { |
| 445 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 446 | return {}; |
| 447 | } |
| 448 | |
| 449 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 450 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 451 | // the dispatcher started counting before this function was called |
| 452 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 453 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 454 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 455 | << "ms, but waited " |
| 456 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 457 | << "ms instead"; |
| 458 | } |
| 459 | return *token; |
| 460 | } |
| 461 | |
| 462 | template <class T> |
| 463 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 464 | std::queue<T>& storage, |
| 465 | std::unique_lock<std::mutex>& lock, |
| 466 | std::condition_variable& condition) |
| 467 | REQUIRES(mLock) { |
| 468 | condition.wait_for(lock, timeout, |
| 469 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 470 | if (storage.empty()) { |
| 471 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 472 | return std::nullopt; |
| 473 | } |
| 474 | T item = storage.front(); |
| 475 | storage.pop(); |
| 476 | return std::make_optional(item); |
| 477 | } |
| 478 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 479 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 480 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 481 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 484 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 485 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 486 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 487 | ASSERT_TRUE(pid.has_value()); |
| 488 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 489 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 492 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 493 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 494 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 495 | ASSERT_TRUE(pid.has_value()); |
| 496 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 497 | mNotifyAnr.notify_all(); |
| 498 | } |
| 499 | |
| 500 | void notifyNoFocusedWindowAnr( |
| 501 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 502 | std::scoped_lock lock(mLock); |
| 503 | mAnrApplications.push(applicationHandle); |
| 504 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 507 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 508 | std::scoped_lock lock(mLock); |
| 509 | mBrokenInputChannels.push(connectionToken); |
| 510 | mNotifyInputChannelBroken.notify_all(); |
| 511 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 512 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 513 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 514 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 515 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 516 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 517 | const std::vector<float>& values) override {} |
| 518 | |
| 519 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 520 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 521 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 522 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 523 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 524 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 525 | *outConfig = mConfig; |
| 526 | } |
| 527 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 528 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 529 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 530 | switch (inputEvent->getType()) { |
| 531 | case AINPUT_EVENT_TYPE_KEY: { |
| 532 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 533 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 534 | break; |
| 535 | } |
| 536 | |
| 537 | case AINPUT_EVENT_TYPE_MOTION: { |
| 538 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 539 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 540 | break; |
| 541 | } |
| 542 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 543 | return true; |
| 544 | } |
| 545 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 546 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { |
| 547 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { |
| 548 | // Clear intercept state when we handled the event. |
| 549 | mInterceptKeyTimeout = 0ms; |
| 550 | } |
| 551 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 552 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 553 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 554 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 555 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 556 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 557 | // Clear intercept state so we could dispatch the event in next wake. |
| 558 | mInterceptKeyTimeout = 0ms; |
| 559 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 562 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 | return false; |
| 564 | } |
| 565 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 566 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 567 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 568 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 569 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 570 | * essentially a passthrough for notifySwitch. |
| 571 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 572 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 575 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 577 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 578 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 579 | mOnPointerDownToken = newToken; |
| 580 | } |
| 581 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 582 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 583 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 584 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 585 | mPointerCaptureChangedCondition.notify_all(); |
| 586 | } |
| 587 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 588 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 589 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 590 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 591 | mDropTargetWindowToken = token; |
| 592 | } |
| 593 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 594 | void assertFilterInputEventWasCalledInternal( |
| 595 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 596 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 597 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 598 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 599 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 600 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 601 | }; |
| 602 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 603 | // --- InputDispatcherTest --- |
| 604 | |
| 605 | class InputDispatcherTest : public testing::Test { |
| 606 | protected: |
| 607 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 608 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 609 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 610 | void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 611 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 612 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 613 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 614 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 615 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 618 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 619 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 620 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 621 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 622 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 623 | |
| 624 | /** |
| 625 | * Used for debugging when writing the test |
| 626 | */ |
| 627 | void dumpDispatcherState() { |
| 628 | std::string dump; |
| 629 | mDispatcher->dump(dump); |
| 630 | std::stringstream ss(dump); |
| 631 | std::string to; |
| 632 | |
| 633 | while (std::getline(ss, to, '\n')) { |
| 634 | ALOGE("%s", to.c_str()); |
| 635 | } |
| 636 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 637 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 638 | void setFocusedWindow(const sp<WindowInfoHandle>& window, |
| 639 | const sp<WindowInfoHandle>& focusedWindow = nullptr) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 640 | FocusRequest request; |
| 641 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 642 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 643 | if (focusedWindow) { |
| 644 | request.focusedToken = focusedWindow->getToken(); |
| 645 | } |
| 646 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 647 | request.displayId = window->getInfo()->displayId; |
| 648 | mDispatcher->setFocusedWindow(request); |
| 649 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 650 | }; |
| 651 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 652 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 653 | KeyEvent event; |
| 654 | |
| 655 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 656 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 657 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 658 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 659 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 660 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 661 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 662 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 663 | << "Should reject key events with undefined action."; |
| 664 | |
| 665 | // 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] | 666 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 667 | 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] | 668 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 669 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 670 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 671 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 672 | << "Should reject key events with ACTION_MULTIPLE."; |
| 673 | } |
| 674 | |
| 675 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 676 | MotionEvent event; |
| 677 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 678 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 679 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 680 | pointerProperties[i].clear(); |
| 681 | pointerProperties[i].id = i; |
| 682 | pointerCoords[i].clear(); |
| 683 | } |
| 684 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 685 | // Some constants commonly used below |
| 686 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 687 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 688 | constexpr int32_t metaState = AMETA_NONE; |
| 689 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 690 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 691 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 692 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 693 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 694 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 695 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 696 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 697 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 698 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 699 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 700 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 701 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 702 | << "Should reject motion events with undefined action."; |
| 703 | |
| 704 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 705 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 706 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 707 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 708 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 709 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 710 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 711 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 712 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 713 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 714 | << "Should reject motion events with pointer down index too large."; |
| 715 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 716 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 717 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 718 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 719 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 720 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 721 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 722 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 723 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 724 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 725 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 726 | << "Should reject motion events with pointer down index too small."; |
| 727 | |
| 728 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 729 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 730 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 731 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 732 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 733 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 734 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 735 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 736 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 737 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 738 | << "Should reject motion events with pointer up index too large."; |
| 739 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 740 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 741 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 742 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 743 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 744 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 745 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 746 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 747 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 748 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 749 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 750 | << "Should reject motion events with pointer up index too small."; |
| 751 | |
| 752 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 753 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 754 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 755 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 756 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 757 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 758 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 759 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 760 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 761 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 762 | << "Should reject motion events with 0 pointers."; |
| 763 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 764 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 765 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 766 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 767 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 768 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 769 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 770 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 771 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 772 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 773 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 774 | |
| 775 | // Rejects motion events with invalid pointer ids. |
| 776 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 777 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 778 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 779 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 780 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 781 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 782 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 783 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 784 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 785 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | << "Should reject motion events with pointer ids less than 0."; |
| 787 | |
| 788 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 789 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 790 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 791 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 792 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 793 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 794 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 795 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 796 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 797 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 799 | |
| 800 | // Rejects motion events with duplicate pointer ids. |
| 801 | pointerProperties[0].id = 1; |
| 802 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 803 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 804 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 805 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 806 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 807 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 808 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 809 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 810 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 811 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | << "Should reject motion events with duplicate pointer ids."; |
| 813 | } |
| 814 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 815 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 816 | |
| 817 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 818 | constexpr nsecs_t eventTime = 20; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 819 | NotifyConfigurationChangedArgs args(/*id=*/10, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 820 | mDispatcher->notifyConfigurationChanged(&args); |
| 821 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 822 | |
| 823 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 824 | } |
| 825 | |
| 826 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 827 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 828 | /*switchMask=*/2); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 829 | mDispatcher->notifySwitch(&args); |
| 830 | |
| 831 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 832 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 833 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 834 | } |
| 835 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 836 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 837 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 838 | // Default input dispatching timeout if there is no focused application or paused window |
| 839 | // from which to determine an appropriate dispatching timeout. |
| 840 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 841 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 842 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 843 | |
| 844 | class FakeApplicationHandle : public InputApplicationHandle { |
| 845 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 846 | FakeApplicationHandle() { |
| 847 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 848 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 849 | mInfo.dispatchingTimeoutMillis = |
| 850 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 851 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 852 | virtual ~FakeApplicationHandle() {} |
| 853 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 854 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 855 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 856 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 857 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 858 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 859 | }; |
| 860 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 861 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 862 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 863 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 864 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 865 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 868 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 869 | InputEvent* event; |
| 870 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 871 | if (!consumeSeq) { |
| 872 | return nullptr; |
| 873 | } |
| 874 | finishEvent(*consumeSeq); |
| 875 | return event; |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Receive an event without acknowledging it. |
| 880 | * Return the sequence number that could later be used to send finished signal. |
| 881 | */ |
| 882 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 883 | uint32_t consumeSeq; |
| 884 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 885 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 886 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 887 | status_t status = WOULD_BLOCK; |
| 888 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 889 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 890 | &event); |
| 891 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 892 | if (elapsed > 100ms) { |
| 893 | break; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (status == WOULD_BLOCK) { |
| 898 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 899 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | if (status != OK) { |
| 903 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 904 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 905 | } |
| 906 | if (event == nullptr) { |
| 907 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 908 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 909 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 910 | if (outEvent != nullptr) { |
| 911 | *outEvent = event; |
| 912 | } |
| 913 | return consumeSeq; |
| 914 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 915 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 916 | /** |
| 917 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 918 | */ |
| 919 | void finishEvent(uint32_t consumeSeq) { |
| 920 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 921 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 924 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 925 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 926 | ASSERT_EQ(OK, status); |
| 927 | } |
| 928 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 929 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 930 | std::optional<int32_t> expectedDisplayId, |
| 931 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 932 | InputEvent* event = consume(); |
| 933 | |
| 934 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 935 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 936 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 937 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 938 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 939 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 940 | if (expectedDisplayId.has_value()) { |
| 941 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 942 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 943 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 944 | switch (expectedEventType) { |
| 945 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 946 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 947 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 948 | if (expectedFlags.has_value()) { |
| 949 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 950 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 951 | break; |
| 952 | } |
| 953 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 954 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 955 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 956 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 957 | if (expectedFlags.has_value()) { |
| 958 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 959 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 960 | break; |
| 961 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 962 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 963 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 964 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 965 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 966 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 967 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 968 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { |
| 969 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 970 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 971 | case AINPUT_EVENT_TYPE_DRAG: { |
| 972 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 973 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 974 | default: { |
| 975 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 976 | } |
| 977 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 978 | } |
| 979 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 980 | MotionEvent* consumeMotion() { |
| 981 | InputEvent* event = consume(); |
| 982 | |
| 983 | if (event == nullptr) { |
| 984 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 985 | return nullptr; |
| 986 | } |
| 987 | |
| 988 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 989 | ADD_FAILURE() << mName << " expected a MotionEvent, got " |
| 990 | << inputEventTypeToString(event->getType()) << " event"; |
| 991 | return nullptr; |
| 992 | } |
| 993 | return static_cast<MotionEvent*>(event); |
| 994 | } |
| 995 | |
| 996 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 997 | MotionEvent* motionEvent = consumeMotion(); |
| 998 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 999 | ASSERT_THAT(*motionEvent, matcher); |
| 1000 | } |
| 1001 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1002 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 1003 | InputEvent* event = consume(); |
| 1004 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1005 | << ": consumer should have returned non-NULL event."; |
| 1006 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 1007 | << "Got " << inputEventTypeToString(event->getType()) |
| 1008 | << " event instead of FOCUS event"; |
| 1009 | |
| 1010 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1011 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1012 | |
| 1013 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1014 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1017 | void consumeCaptureEvent(bool hasCapture) { |
| 1018 | const InputEvent* event = consume(); |
| 1019 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1020 | << ": consumer should have returned non-NULL event."; |
| 1021 | ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType()) |
| 1022 | << "Got " << inputEventTypeToString(event->getType()) |
| 1023 | << " event instead of CAPTURE event"; |
| 1024 | |
| 1025 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1026 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1027 | |
| 1028 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1029 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1030 | } |
| 1031 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1032 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1033 | const InputEvent* event = consume(); |
| 1034 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1035 | << ": consumer should have returned non-NULL event."; |
| 1036 | ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType()) |
| 1037 | << "Got " << inputEventTypeToString(event->getType()) |
| 1038 | << " event instead of DRAG event"; |
| 1039 | |
| 1040 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1041 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1042 | |
| 1043 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1044 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1045 | EXPECT_EQ(x, dragEvent.getX()); |
| 1046 | EXPECT_EQ(y, dragEvent.getY()); |
| 1047 | } |
| 1048 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1049 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1050 | const InputEvent* event = consume(); |
| 1051 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1052 | << ": consumer should have returned non-NULL event."; |
| 1053 | ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType()) |
| 1054 | << "Got " << inputEventTypeToString(event->getType()) |
| 1055 | << " event instead of TOUCH_MODE event"; |
| 1056 | |
| 1057 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1058 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1059 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1060 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1061 | } |
| 1062 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1063 | void assertNoEvents() { |
| 1064 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1065 | if (event == nullptr) { |
| 1066 | return; |
| 1067 | } |
| 1068 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 1069 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1070 | ADD_FAILURE() << "Received key event " |
| 1071 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 1072 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 1073 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1074 | ADD_FAILURE() << "Received motion event " |
| 1075 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 1076 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 1077 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1078 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1079 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1080 | } else if (event->getType() == AINPUT_EVENT_TYPE_CAPTURE) { |
| 1081 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1082 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1083 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1084 | } else if (event->getType() == AINPUT_EVENT_TYPE_TOUCH_MODE) { |
| 1085 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1086 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1087 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1088 | } |
| 1089 | FAIL() << mName.c_str() |
| 1090 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1094 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1095 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1096 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1097 | protected: |
| 1098 | std::unique_ptr<InputConsumer> mConsumer; |
| 1099 | PreallocatedInputEventFactory mEventFactory; |
| 1100 | |
| 1101 | std::string mName; |
| 1102 | }; |
| 1103 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1104 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1105 | public: |
| 1106 | static const int32_t WIDTH = 600; |
| 1107 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1108 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1109 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1110 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1111 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1112 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1113 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1114 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1115 | dispatcher->createInputChannel(name); |
| 1116 | token = (*channel)->getConnectionToken(); |
| 1117 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | inputApplicationHandle->updateInfo(); |
| 1121 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1122 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1123 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1124 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1125 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1126 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1127 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1128 | mInfo.frameLeft = 0; |
| 1129 | mInfo.frameTop = 0; |
| 1130 | mInfo.frameRight = WIDTH; |
| 1131 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1132 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1133 | mInfo.globalScaleFactor = 1.0; |
| 1134 | mInfo.touchableRegion.clear(); |
| 1135 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1136 | mInfo.ownerPid = WINDOW_PID; |
| 1137 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1138 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1139 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1142 | sp<FakeWindowHandle> clone( |
| 1143 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1144 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1145 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1146 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1147 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1148 | return handle; |
| 1149 | } |
| 1150 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1151 | void setTouchable(bool touchable) { |
| 1152 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1153 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1154 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1155 | void setFocusable(bool focusable) { |
| 1156 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1157 | } |
| 1158 | |
| 1159 | void setVisible(bool visible) { |
| 1160 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1161 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1162 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1163 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1164 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1167 | void setPaused(bool paused) { |
| 1168 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1169 | } |
| 1170 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1171 | void setPreventSplitting(bool preventSplitting) { |
| 1172 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | void setSlippery(bool slippery) { |
| 1176 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1177 | } |
| 1178 | |
| 1179 | void setWatchOutsideTouch(bool watchOutside) { |
| 1180 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1181 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1182 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1183 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1184 | |
| 1185 | void setInterceptsStylus(bool interceptsStylus) { |
| 1186 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1187 | } |
| 1188 | |
| 1189 | void setDropInput(bool dropInput) { |
| 1190 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1191 | } |
| 1192 | |
| 1193 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1194 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1195 | } |
| 1196 | |
| 1197 | void setNoInputChannel(bool noInputChannel) { |
| 1198 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1199 | } |
| 1200 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1201 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1202 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1203 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1204 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1205 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1206 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1207 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1208 | mInfo.frameLeft = frame.left; |
| 1209 | mInfo.frameTop = frame.top; |
| 1210 | mInfo.frameRight = frame.right; |
| 1211 | mInfo.frameBottom = frame.bottom; |
| 1212 | mInfo.touchableRegion.clear(); |
| 1213 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1214 | |
| 1215 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1216 | ui::Transform translate; |
| 1217 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1218 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1221 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1222 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1223 | void setIsWallpaper(bool isWallpaper) { |
| 1224 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1225 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1226 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1227 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1228 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1229 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1230 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1231 | void setTrustedOverlay(bool trustedOverlay) { |
| 1232 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1233 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1234 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1235 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1236 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1237 | } |
| 1238 | |
| 1239 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1240 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1241 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1242 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1243 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1244 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1245 | expectedFlags); |
| 1246 | } |
| 1247 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1248 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1249 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 1250 | } |
| 1251 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1252 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1253 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1254 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1255 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1259 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1260 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1261 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1265 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1266 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1267 | } |
| 1268 | |
| 1269 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1270 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1271 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1272 | expectedFlags); |
| 1273 | } |
| 1274 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1275 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1276 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1277 | int32_t expectedFlags = 0) { |
| 1278 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1279 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1280 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1281 | } |
| 1282 | |
| 1283 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1284 | int32_t expectedFlags = 0) { |
| 1285 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1286 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1287 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 1288 | } |
| 1289 | |
| 1290 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1291 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1292 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 1293 | expectedFlags); |
| 1294 | } |
| 1295 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1296 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1297 | int32_t expectedFlags = 0) { |
| 1298 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
| 1299 | expectedFlags); |
| 1300 | } |
| 1301 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1302 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1303 | int32_t expectedFlags = 0) { |
| 1304 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1305 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1306 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()); |
| 1307 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1308 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1309 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1310 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1311 | } |
| 1312 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1313 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1314 | ASSERT_NE(mInputReceiver, nullptr) |
| 1315 | << "Cannot consume events from a window with no receiver"; |
| 1316 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1317 | } |
| 1318 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1319 | void consumeCaptureEvent(bool hasCapture) { |
| 1320 | ASSERT_NE(mInputReceiver, nullptr) |
| 1321 | << "Cannot consume events from a window with no receiver"; |
| 1322 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1323 | } |
| 1324 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1325 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1326 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1327 | 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] | 1328 | ASSERT_THAT(*motionEvent, matcher); |
| 1329 | } |
| 1330 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1331 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, |
| 1332 | std::optional<int32_t> expectedDisplayId, |
| 1333 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1334 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1335 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1336 | expectedFlags); |
| 1337 | } |
| 1338 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1339 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1340 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1341 | } |
| 1342 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1343 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1344 | ASSERT_NE(mInputReceiver, nullptr) |
| 1345 | << "Cannot consume events from a window with no receiver"; |
| 1346 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1347 | } |
| 1348 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1349 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1350 | if (mInputReceiver == nullptr) { |
| 1351 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1352 | return std::nullopt; |
| 1353 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1354 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | void finishEvent(uint32_t sequenceNum) { |
| 1358 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1359 | mInputReceiver->finishEvent(sequenceNum); |
| 1360 | } |
| 1361 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1362 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1363 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1364 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1365 | } |
| 1366 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1367 | InputEvent* consume() { |
| 1368 | if (mInputReceiver == nullptr) { |
| 1369 | return nullptr; |
| 1370 | } |
| 1371 | return mInputReceiver->consume(); |
| 1372 | } |
| 1373 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1374 | MotionEvent* consumeMotion() { |
| 1375 | InputEvent* event = consume(); |
| 1376 | if (event == nullptr) { |
| 1377 | ADD_FAILURE() << "Consume failed : no event"; |
| 1378 | return nullptr; |
| 1379 | } |
| 1380 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 1381 | ADD_FAILURE() << "Instead of motion event, got " |
| 1382 | << inputEventTypeToString(event->getType()); |
| 1383 | return nullptr; |
| 1384 | } |
| 1385 | return static_cast<MotionEvent*>(event); |
| 1386 | } |
| 1387 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1388 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1389 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1390 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1391 | return; // Can't receive events if the window does not have input channel |
| 1392 | } |
| 1393 | ASSERT_NE(nullptr, mInputReceiver) |
| 1394 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1395 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1396 | } |
| 1397 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1398 | sp<IBinder> getToken() { return mInfo.token; } |
| 1399 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1400 | const std::string& getName() { return mName; } |
| 1401 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1402 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1403 | mInfo.ownerPid = ownerPid; |
| 1404 | mInfo.ownerUid = ownerUid; |
| 1405 | } |
| 1406 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1407 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1408 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1409 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1410 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1411 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1412 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1413 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1414 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1415 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1416 | 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] | 1417 | }; |
| 1418 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1419 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1420 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1421 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1422 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1423 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1424 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1425 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1426 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1427 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1428 | KeyEvent event; |
| 1429 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1430 | |
| 1431 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1432 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1433 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1434 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1435 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1436 | if (!allowKeyRepeat) { |
| 1437 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1438 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1439 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1440 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1441 | } |
| 1442 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1443 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1444 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1445 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1448 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1449 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1450 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1451 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1452 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1453 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1454 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1455 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1458 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1459 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1460 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1463 | class PointerBuilder { |
| 1464 | public: |
| 1465 | PointerBuilder(int32_t id, int32_t toolType) { |
| 1466 | mProperties.clear(); |
| 1467 | mProperties.id = id; |
| 1468 | mProperties.toolType = toolType; |
| 1469 | mCoords.clear(); |
| 1470 | } |
| 1471 | |
| 1472 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1473 | |
| 1474 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1475 | |
| 1476 | PointerBuilder& axis(int32_t axis, float value) { |
| 1477 | mCoords.setAxisValue(axis, value); |
| 1478 | return *this; |
| 1479 | } |
| 1480 | |
| 1481 | PointerProperties buildProperties() const { return mProperties; } |
| 1482 | |
| 1483 | PointerCoords buildCoords() const { return mCoords; } |
| 1484 | |
| 1485 | private: |
| 1486 | PointerProperties mProperties; |
| 1487 | PointerCoords mCoords; |
| 1488 | }; |
| 1489 | |
| 1490 | class MotionEventBuilder { |
| 1491 | public: |
| 1492 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1493 | mAction = action; |
| 1494 | mSource = source; |
| 1495 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1496 | mDownTime = mEventTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1499 | MotionEventBuilder& deviceId(int32_t deviceId) { |
| 1500 | mDeviceId = deviceId; |
| 1501 | return *this; |
| 1502 | } |
| 1503 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1504 | MotionEventBuilder& downTime(nsecs_t downTime) { |
| 1505 | mDownTime = downTime; |
| 1506 | return *this; |
| 1507 | } |
| 1508 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1509 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1510 | mEventTime = eventTime; |
| 1511 | return *this; |
| 1512 | } |
| 1513 | |
| 1514 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1515 | mDisplayId = displayId; |
| 1516 | return *this; |
| 1517 | } |
| 1518 | |
| 1519 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1520 | mActionButton = actionButton; |
| 1521 | return *this; |
| 1522 | } |
| 1523 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1524 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1525 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1526 | return *this; |
| 1527 | } |
| 1528 | |
| 1529 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1530 | mRawXCursorPosition = rawXCursorPosition; |
| 1531 | return *this; |
| 1532 | } |
| 1533 | |
| 1534 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1535 | mRawYCursorPosition = rawYCursorPosition; |
| 1536 | return *this; |
| 1537 | } |
| 1538 | |
| 1539 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1540 | mPointers.push_back(pointer); |
| 1541 | return *this; |
| 1542 | } |
| 1543 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1544 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1545 | mFlags |= flags; |
| 1546 | return *this; |
| 1547 | } |
| 1548 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1549 | MotionEvent build() { |
| 1550 | std::vector<PointerProperties> pointerProperties; |
| 1551 | std::vector<PointerCoords> pointerCoords; |
| 1552 | for (const PointerBuilder& pointer : mPointers) { |
| 1553 | pointerProperties.push_back(pointer.buildProperties()); |
| 1554 | pointerCoords.push_back(pointer.buildCoords()); |
| 1555 | } |
| 1556 | |
| 1557 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1558 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1559 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1560 | mPointers.size() == 1) { |
| 1561 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1562 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1563 | } |
| 1564 | |
| 1565 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1566 | ui::Transform identityTransform; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1567 | event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1568 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1569 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1570 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1571 | mRawYCursorPosition, identityTransform, mDownTime, mEventTime, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1572 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1573 | |
| 1574 | return event; |
| 1575 | } |
| 1576 | |
| 1577 | private: |
| 1578 | int32_t mAction; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1579 | int32_t mDeviceId = DEVICE_ID; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1580 | int32_t mSource; |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1581 | nsecs_t mDownTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1582 | nsecs_t mEventTime; |
| 1583 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1584 | int32_t mActionButton{0}; |
| 1585 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1586 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1587 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1588 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1589 | |
| 1590 | std::vector<PointerBuilder> mPointers; |
| 1591 | }; |
| 1592 | |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1593 | class MotionArgsBuilder { |
| 1594 | public: |
| 1595 | MotionArgsBuilder(int32_t action, int32_t source) { |
| 1596 | mAction = action; |
| 1597 | mSource = source; |
| 1598 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1599 | mDownTime = mEventTime; |
| 1600 | } |
| 1601 | |
| 1602 | MotionArgsBuilder& deviceId(int32_t deviceId) { |
| 1603 | mDeviceId = deviceId; |
| 1604 | return *this; |
| 1605 | } |
| 1606 | |
| 1607 | MotionArgsBuilder& downTime(nsecs_t downTime) { |
| 1608 | mDownTime = downTime; |
| 1609 | return *this; |
| 1610 | } |
| 1611 | |
| 1612 | MotionArgsBuilder& eventTime(nsecs_t eventTime) { |
| 1613 | mEventTime = eventTime; |
| 1614 | return *this; |
| 1615 | } |
| 1616 | |
| 1617 | MotionArgsBuilder& displayId(int32_t displayId) { |
| 1618 | mDisplayId = displayId; |
| 1619 | return *this; |
| 1620 | } |
| 1621 | |
| 1622 | MotionArgsBuilder& policyFlags(int32_t policyFlags) { |
| 1623 | mPolicyFlags = policyFlags; |
| 1624 | return *this; |
| 1625 | } |
| 1626 | |
| 1627 | MotionArgsBuilder& actionButton(int32_t actionButton) { |
| 1628 | mActionButton = actionButton; |
| 1629 | return *this; |
| 1630 | } |
| 1631 | |
| 1632 | MotionArgsBuilder& buttonState(int32_t buttonState) { |
| 1633 | mButtonState = buttonState; |
| 1634 | return *this; |
| 1635 | } |
| 1636 | |
| 1637 | MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1638 | mRawXCursorPosition = rawXCursorPosition; |
| 1639 | return *this; |
| 1640 | } |
| 1641 | |
| 1642 | MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1643 | mRawYCursorPosition = rawYCursorPosition; |
| 1644 | return *this; |
| 1645 | } |
| 1646 | |
| 1647 | MotionArgsBuilder& pointer(PointerBuilder pointer) { |
| 1648 | mPointers.push_back(pointer); |
| 1649 | return *this; |
| 1650 | } |
| 1651 | |
| 1652 | MotionArgsBuilder& addFlag(uint32_t flags) { |
| 1653 | mFlags |= flags; |
| 1654 | return *this; |
| 1655 | } |
| 1656 | |
| 1657 | NotifyMotionArgs build() { |
| 1658 | std::vector<PointerProperties> pointerProperties; |
| 1659 | std::vector<PointerCoords> pointerCoords; |
| 1660 | for (const PointerBuilder& pointer : mPointers) { |
| 1661 | pointerProperties.push_back(pointer.buildProperties()); |
| 1662 | pointerCoords.push_back(pointer.buildCoords()); |
| 1663 | } |
| 1664 | |
| 1665 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1666 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1667 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1668 | mPointers.size() == 1) { |
| 1669 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1670 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1671 | } |
| 1672 | |
| 1673 | NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId, |
| 1674 | mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags, |
| 1675 | AMETA_NONE, mButtonState, MotionClassification::NONE, /*edgeFlags=*/0, |
| 1676 | mPointers.size(), pointerProperties.data(), pointerCoords.data(), |
| 1677 | /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition, |
| 1678 | mRawYCursorPosition, mDownTime, /*videoFrames=*/{}); |
| 1679 | |
| 1680 | return args; |
| 1681 | } |
| 1682 | |
| 1683 | private: |
| 1684 | int32_t mAction; |
| 1685 | int32_t mDeviceId = DEVICE_ID; |
| 1686 | int32_t mSource; |
| 1687 | nsecs_t mDownTime; |
| 1688 | nsecs_t mEventTime; |
| 1689 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1690 | int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; |
| 1691 | int32_t mActionButton{0}; |
| 1692 | int32_t mButtonState{0}; |
| 1693 | int32_t mFlags{0}; |
| 1694 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1695 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1696 | |
| 1697 | std::vector<PointerBuilder> mPointers; |
| 1698 | }; |
| 1699 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1700 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1701 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1702 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1703 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1704 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1705 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1706 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1709 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1710 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1711 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1712 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1713 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1714 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1715 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1716 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1717 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1718 | MotionEvent event = MotionEventBuilder(action, source) |
| 1719 | .displayId(displayId) |
| 1720 | .eventTime(eventTime) |
| 1721 | .rawXCursorPosition(cursorPosition.x) |
| 1722 | .rawYCursorPosition(cursorPosition.y) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1723 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1724 | .x(position.x) |
| 1725 | .y(position.y)) |
| 1726 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1727 | |
| 1728 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1729 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, |
| 1730 | policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1733 | static InputEventInjectionResult injectMotionDown( |
| 1734 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1735 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1736 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1739 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1740 | int32_t source, int32_t displayId, |
| 1741 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1742 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1745 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1746 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1747 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1748 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1749 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1750 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1751 | |
| 1752 | return args; |
| 1753 | } |
| 1754 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1755 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1756 | const std::vector<PointF>& points) { |
| 1757 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1758 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1759 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1760 | } |
| 1761 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1762 | PointerProperties pointerProperties[pointerCount]; |
| 1763 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1764 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1765 | for (size_t i = 0; i < pointerCount; i++) { |
| 1766 | pointerProperties[i].clear(); |
| 1767 | pointerProperties[i].id = i; |
| 1768 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1769 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1770 | pointerCoords[i].clear(); |
| 1771 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1772 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1773 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1774 | |
| 1775 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1776 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1777 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1778 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1779 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1780 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1781 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1782 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1783 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1784 | |
| 1785 | return args; |
| 1786 | } |
| 1787 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1788 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1789 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1790 | } |
| 1791 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1792 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1793 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1794 | } |
| 1795 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1796 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1797 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1798 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1801 | /** |
| 1802 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1803 | * broken channel. |
| 1804 | */ |
| 1805 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1806 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1807 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1808 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1809 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1810 | |
| 1811 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1812 | |
| 1813 | // Window closes its channel, but the window remains. |
| 1814 | window->destroyReceiver(); |
| 1815 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1816 | } |
| 1817 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1818 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1819 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1820 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1821 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1822 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1823 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1825 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1826 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1827 | |
| 1828 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1829 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1830 | } |
| 1831 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1832 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1833 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1834 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1835 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1836 | |
| 1837 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1838 | // Inject a MotionEvent to an unknown display. |
| 1839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1840 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1841 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1842 | |
| 1843 | // Window should receive motion event. |
| 1844 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1845 | } |
| 1846 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1847 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1848 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1849 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1850 | * called twice. |
| 1851 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1852 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1853 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1854 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1855 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1856 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1857 | |
| 1858 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1860 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1861 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1862 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1863 | |
| 1864 | // Window should receive motion event. |
| 1865 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1866 | } |
| 1867 | |
| 1868 | /** |
| 1869 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1870 | */ |
| 1871 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1872 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1873 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1874 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1875 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1876 | |
| 1877 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1878 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1880 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1881 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1882 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1883 | |
| 1884 | // Window should receive motion event. |
| 1885 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1886 | } |
| 1887 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1888 | // The foreground window should receive the first touch down event. |
| 1889 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1890 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1891 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1892 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1893 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1894 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1895 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1896 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1897 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1898 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1899 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1900 | |
| 1901 | // 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] | 1902 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1903 | windowSecond->assertNoEvents(); |
| 1904 | } |
| 1905 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1906 | /** |
| 1907 | * Two windows: A top window, and a wallpaper behind the window. |
| 1908 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1909 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1910 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1911 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1912 | */ |
| 1913 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1914 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1915 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1916 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1917 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1918 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1919 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1920 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1921 | |
| 1922 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1924 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1925 | {100, 200})) |
| 1926 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1927 | |
| 1928 | // Both foreground window and its wallpaper should receive the touch down |
| 1929 | foregroundWindow->consumeMotionDown(); |
| 1930 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1931 | |
| 1932 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1933 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1934 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1935 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1936 | |
| 1937 | foregroundWindow->consumeMotionMove(); |
| 1938 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1939 | |
| 1940 | // Now the foreground window goes away, but the wallpaper stays |
| 1941 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1942 | foregroundWindow->consumeMotionCancel(); |
| 1943 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1944 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1945 | } |
| 1946 | |
| 1947 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1948 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1949 | * with the following differences: |
| 1950 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1951 | * clean up the connection. |
| 1952 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1953 | * Ensure that there's no crash in the dispatcher. |
| 1954 | */ |
| 1955 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1956 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1957 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1958 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1959 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 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); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1962 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1963 | |
| 1964 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1965 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1966 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1967 | {100, 200})) |
| 1968 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1969 | |
| 1970 | // Both foreground window and its wallpaper should receive the touch down |
| 1971 | foregroundWindow->consumeMotionDown(); |
| 1972 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1973 | |
| 1974 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1975 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1976 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1977 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1978 | |
| 1979 | foregroundWindow->consumeMotionMove(); |
| 1980 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1981 | |
| 1982 | // Wallpaper closes its channel, but the window remains. |
| 1983 | wallpaperWindow->destroyReceiver(); |
| 1984 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1985 | |
| 1986 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1987 | // is no longer valid. |
| 1988 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1989 | foregroundWindow->consumeMotionCancel(); |
| 1990 | } |
| 1991 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1992 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1993 | public ::testing::WithParamInterface<bool> {}; |
| 1994 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1995 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1996 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1997 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1998 | * The top window gets a multitouch gesture. |
| 1999 | * Ensure that wallpaper gets the same gesture. |
| 2000 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2001 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2002 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2003 | sp<FakeWindowHandle> foregroundWindow = |
| 2004 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 2005 | foregroundWindow->setDupTouchToWallpaper(true); |
| 2006 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2007 | |
| 2008 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2009 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2010 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2011 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2012 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2013 | |
| 2014 | // Touch down on top window |
| 2015 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2016 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2017 | {100, 100})) |
| 2018 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2019 | |
| 2020 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2021 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2022 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2023 | |
| 2024 | // Second finger down on the top window |
| 2025 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2026 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2027 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2028 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2029 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2030 | .build(); |
| 2031 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2032 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2033 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2034 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2035 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2036 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 2037 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2038 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2039 | |
| 2040 | const MotionEvent secondFingerUpEvent = |
| 2041 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2042 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2043 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2044 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2045 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2046 | .build(); |
| 2047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2048 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 2049 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2050 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2051 | foregroundWindow->consumeMotionPointerUp(0); |
| 2052 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2053 | |
| 2054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame^] | 2055 | injectMotionEvent(mDispatcher, |
| 2056 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2057 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2058 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2059 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2060 | .pointer(PointerBuilder(/* id */ 1, |
| 2061 | AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2062 | .x(100) |
| 2063 | .y(100)) |
| 2064 | .build(), |
| 2065 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2066 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2067 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2068 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | /** |
| 2072 | * Two windows: a window on the left and window on the right. |
| 2073 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 2074 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 2075 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 2076 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 2077 | * ACTION_POINTER_DOWN(1). |
| 2078 | */ |
| 2079 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 2080 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2081 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2082 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2083 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2084 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2085 | |
| 2086 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2087 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2088 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2089 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2090 | |
| 2091 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2092 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2093 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2094 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2095 | |
| 2096 | mDispatcher->setInputWindows( |
| 2097 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 2098 | |
| 2099 | // Touch down on left window |
| 2100 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2101 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2102 | {100, 100})) |
| 2103 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2104 | |
| 2105 | // Both foreground window and its wallpaper should receive the touch down |
| 2106 | leftWindow->consumeMotionDown(); |
| 2107 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2108 | |
| 2109 | // Second finger down on the right window |
| 2110 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2111 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2112 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2113 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2114 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2115 | .build(); |
| 2116 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2117 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2118 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2119 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2120 | |
| 2121 | leftWindow->consumeMotionMove(); |
| 2122 | // Since the touch is split, right window gets ACTION_DOWN |
| 2123 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2124 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2125 | expectedWallpaperFlags); |
| 2126 | |
| 2127 | // Now, leftWindow, which received the first finger, disappears. |
| 2128 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 2129 | leftWindow->consumeMotionCancel(); |
| 2130 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2131 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2132 | |
| 2133 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2134 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2135 | const MotionEvent secondFingerMoveEvent = |
| 2136 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2137 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2138 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2139 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2140 | .build(); |
| 2141 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2142 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2143 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2144 | rightWindow->consumeMotionMove(); |
| 2145 | |
| 2146 | leftWindow->assertNoEvents(); |
| 2147 | rightWindow->assertNoEvents(); |
| 2148 | wallpaperWindow->assertNoEvents(); |
| 2149 | } |
| 2150 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2151 | /** |
| 2152 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2153 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2154 | * The right window should receive ACTION_DOWN. |
| 2155 | */ |
| 2156 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2157 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2158 | sp<FakeWindowHandle> leftWindow = |
| 2159 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2160 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2161 | leftWindow->setDupTouchToWallpaper(true); |
| 2162 | leftWindow->setSlippery(true); |
| 2163 | |
| 2164 | sp<FakeWindowHandle> rightWindow = |
| 2165 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2166 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2167 | |
| 2168 | sp<FakeWindowHandle> wallpaperWindow = |
| 2169 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2170 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2171 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2172 | mDispatcher->setInputWindows( |
| 2173 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2174 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2175 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2177 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2178 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2179 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2180 | |
| 2181 | // Both foreground window and its wallpaper should receive the touch down |
| 2182 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2183 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2184 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2185 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2186 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2187 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2188 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2189 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2190 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2191 | leftWindow->consumeMotionCancel(); |
| 2192 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2193 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2196 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2197 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2198 | * interactive, it might stop sending this flag. |
| 2199 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2200 | * to have a consistent input stream. |
| 2201 | * |
| 2202 | * Test procedure: |
| 2203 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2204 | * DOWN (new gesture). |
| 2205 | * |
| 2206 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2207 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2208 | * |
| 2209 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2210 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2211 | */ |
| 2212 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2213 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2214 | sp<FakeWindowHandle> spyWindow = |
| 2215 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2216 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2217 | spyWindow->setTrustedOverlay(true); |
| 2218 | spyWindow->setSpy(true); |
| 2219 | |
| 2220 | sp<FakeWindowHandle> window = |
| 2221 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2222 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2223 | |
| 2224 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2225 | const int32_t touchDeviceId = 4; |
| 2226 | NotifyMotionArgs args; |
| 2227 | |
| 2228 | // Two pointers down |
| 2229 | mDispatcher->notifyMotion(&( |
| 2230 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2231 | .deviceId(touchDeviceId) |
| 2232 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2233 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2234 | .build())); |
| 2235 | |
| 2236 | mDispatcher->notifyMotion(&( |
| 2237 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2238 | .deviceId(touchDeviceId) |
| 2239 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2240 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2241 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(120).y(120)) |
| 2242 | .build())); |
| 2243 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2244 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2245 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2246 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2247 | |
| 2248 | // Cancel the current gesture. Send the cancel without the default policy flags. |
| 2249 | mDispatcher->notifyMotion(&( |
| 2250 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2251 | .deviceId(touchDeviceId) |
| 2252 | .policyFlags(0) |
| 2253 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2254 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(120).y(120)) |
| 2255 | .build())); |
| 2256 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2257 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2258 | |
| 2259 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2260 | // follows, so we keep it here to model the actual listener behaviour more closely. |
| 2261 | NotifyDeviceResetArgs resetArgs; |
| 2262 | resetArgs.id = 1; // arbitrary id |
| 2263 | resetArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2264 | resetArgs.deviceId = touchDeviceId; |
| 2265 | mDispatcher->notifyDeviceReset(&resetArgs); |
| 2266 | |
| 2267 | // Start new gesture |
| 2268 | mDispatcher->notifyMotion(&( |
| 2269 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2270 | .deviceId(touchDeviceId) |
| 2271 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2272 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2273 | .build())); |
| 2274 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2275 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2276 | |
| 2277 | // No more events |
| 2278 | spyWindow->assertNoEvents(); |
| 2279 | window->assertNoEvents(); |
| 2280 | } |
| 2281 | |
| 2282 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2283 | * Two windows: a window on the left and a window on the right. |
| 2284 | * Mouse is hovered from the right window into the left window. |
| 2285 | * Next, we tap on the left window, where the cursor was last seen. |
| 2286 | * The second tap is done onto the right window. |
| 2287 | * The mouse and tap are from two different devices. |
| 2288 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2289 | * explicitly helps during debugging. |
| 2290 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2291 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2292 | */ |
| 2293 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2294 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2295 | sp<FakeWindowHandle> leftWindow = |
| 2296 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2297 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2298 | |
| 2299 | sp<FakeWindowHandle> rightWindow = |
| 2300 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2301 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2302 | |
| 2303 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2304 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2305 | // stale. |
| 2306 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2307 | const int32_t mouseDeviceId = 6; |
| 2308 | const int32_t touchDeviceId = 4; |
| 2309 | // Move the cursor from right |
| 2310 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2311 | injectMotionEvent(mDispatcher, |
| 2312 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2313 | AINPUT_SOURCE_MOUSE) |
| 2314 | .deviceId(mouseDeviceId) |
| 2315 | .downTime(baseTime + 10) |
| 2316 | .eventTime(baseTime + 20) |
| 2317 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2318 | .x(300) |
| 2319 | .y(100)) |
| 2320 | .build())); |
| 2321 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2322 | |
| 2323 | // .. to the left window |
| 2324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2325 | injectMotionEvent(mDispatcher, |
| 2326 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2327 | AINPUT_SOURCE_MOUSE) |
| 2328 | .deviceId(mouseDeviceId) |
| 2329 | .downTime(baseTime + 10) |
| 2330 | .eventTime(baseTime + 30) |
| 2331 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2332 | .x(110) |
| 2333 | .y(100)) |
| 2334 | .build())); |
| 2335 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2336 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2337 | // Now tap the left window |
| 2338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2339 | injectMotionEvent(mDispatcher, |
| 2340 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2341 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2342 | .deviceId(touchDeviceId) |
| 2343 | .downTime(baseTime + 40) |
| 2344 | .eventTime(baseTime + 40) |
| 2345 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2346 | .x(100) |
| 2347 | .y(100)) |
| 2348 | .build())); |
| 2349 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2350 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2351 | |
| 2352 | // release tap |
| 2353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2354 | injectMotionEvent(mDispatcher, |
| 2355 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2356 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2357 | .deviceId(touchDeviceId) |
| 2358 | .downTime(baseTime + 40) |
| 2359 | .eventTime(baseTime + 50) |
| 2360 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2361 | .x(100) |
| 2362 | .y(100)) |
| 2363 | .build())); |
| 2364 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2365 | |
| 2366 | // Tap the window on the right |
| 2367 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2368 | injectMotionEvent(mDispatcher, |
| 2369 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2370 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2371 | .deviceId(touchDeviceId) |
| 2372 | .downTime(baseTime + 60) |
| 2373 | .eventTime(baseTime + 60) |
| 2374 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2375 | .x(300) |
| 2376 | .y(100)) |
| 2377 | .build())); |
| 2378 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2379 | |
| 2380 | // release tap |
| 2381 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2382 | injectMotionEvent(mDispatcher, |
| 2383 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2384 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2385 | .deviceId(touchDeviceId) |
| 2386 | .downTime(baseTime + 60) |
| 2387 | .eventTime(baseTime + 70) |
| 2388 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2389 | .x(300) |
| 2390 | .y(100)) |
| 2391 | .build())); |
| 2392 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2393 | |
| 2394 | // No more events |
| 2395 | leftWindow->assertNoEvents(); |
| 2396 | rightWindow->assertNoEvents(); |
| 2397 | } |
| 2398 | |
| 2399 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2400 | * Two windows: a window on the left and a window on the right. |
| 2401 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2402 | * down. Then, on the left window, also place second touch pointer down. |
| 2403 | * This test tries to reproduce a crash. |
| 2404 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2405 | */ |
| 2406 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { |
| 2407 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2408 | sp<FakeWindowHandle> leftWindow = |
| 2409 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2410 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2411 | |
| 2412 | sp<FakeWindowHandle> rightWindow = |
| 2413 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2414 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2415 | |
| 2416 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2417 | |
| 2418 | const int32_t touchDeviceId = 4; |
| 2419 | const int32_t mouseDeviceId = 6; |
| 2420 | NotifyMotionArgs args; |
| 2421 | |
| 2422 | // Start hovering over the left window |
| 2423 | mDispatcher->notifyMotion(&( |
| 2424 | args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2425 | .deviceId(mouseDeviceId) |
| 2426 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(100).y(100)) |
| 2427 | .build())); |
| 2428 | leftWindow->consumeMotionEvent( |
| 2429 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2430 | |
| 2431 | // Mouse down on left window |
| 2432 | mDispatcher->notifyMotion(&( |
| 2433 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2434 | .deviceId(mouseDeviceId) |
| 2435 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2436 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(100).y(100)) |
| 2437 | .build())); |
| 2438 | |
| 2439 | leftWindow->consumeMotionEvent( |
| 2440 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2441 | leftWindow->consumeMotionEvent( |
| 2442 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2443 | |
| 2444 | mDispatcher->notifyMotion(&( |
| 2445 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2446 | .deviceId(mouseDeviceId) |
| 2447 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2448 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2449 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(100).y(100)) |
| 2450 | .build())); |
| 2451 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2452 | |
| 2453 | // First touch pointer down on right window |
| 2454 | mDispatcher->notifyMotion(&( |
| 2455 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2456 | .deviceId(touchDeviceId) |
| 2457 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100)) |
| 2458 | .build())); |
| 2459 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2460 | |
| 2461 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2462 | |
| 2463 | // Second touch pointer down on left window |
| 2464 | mDispatcher->notifyMotion(&( |
| 2465 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2466 | .deviceId(touchDeviceId) |
| 2467 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100)) |
| 2468 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 2469 | .build())); |
| 2470 | leftWindow->consumeMotionEvent( |
| 2471 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2472 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2473 | // current implementation. |
| 2474 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2475 | rightWindow->consumeMotionEvent( |
| 2476 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2477 | |
| 2478 | leftWindow->assertNoEvents(); |
| 2479 | rightWindow->assertNoEvents(); |
| 2480 | } |
| 2481 | |
| 2482 | /** |
| 2483 | * On a single window, use two different devices: mouse and touch. |
| 2484 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2485 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2486 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2487 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2488 | * represent a new gesture. |
| 2489 | */ |
| 2490 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { |
| 2491 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2492 | sp<FakeWindowHandle> window = |
| 2493 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2494 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2495 | |
| 2496 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2497 | |
| 2498 | const int32_t touchDeviceId = 4; |
| 2499 | const int32_t mouseDeviceId = 6; |
| 2500 | NotifyMotionArgs args; |
| 2501 | |
| 2502 | // First touch pointer down on right window |
| 2503 | mDispatcher->notifyMotion(&( |
| 2504 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2505 | .deviceId(touchDeviceId) |
| 2506 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100)) |
| 2507 | .build())); |
| 2508 | // Second touch pointer down |
| 2509 | mDispatcher->notifyMotion(&( |
| 2510 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2511 | .deviceId(touchDeviceId) |
| 2512 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100)) |
| 2513 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(350).y(100)) |
| 2514 | .build())); |
| 2515 | // First touch pointer lifts. The second one remains down |
| 2516 | mDispatcher->notifyMotion(&( |
| 2517 | args = MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2518 | .deviceId(touchDeviceId) |
| 2519 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100)) |
| 2520 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(350).y(100)) |
| 2521 | .build())); |
| 2522 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2523 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2524 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2525 | |
| 2526 | // Mouse down. The touch should be canceled |
| 2527 | mDispatcher->notifyMotion(&( |
| 2528 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2529 | .deviceId(mouseDeviceId) |
| 2530 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2531 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(320).y(100)) |
| 2532 | .build())); |
| 2533 | |
| 2534 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2535 | WithPointerCount(2u))); |
| 2536 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2537 | |
| 2538 | mDispatcher->notifyMotion(&( |
| 2539 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2540 | .deviceId(mouseDeviceId) |
| 2541 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2542 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2543 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(320).y(100)) |
| 2544 | .build())); |
| 2545 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2546 | |
| 2547 | // Second touch pointer down. |
| 2548 | mDispatcher->notifyMotion(&( |
| 2549 | args = MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2550 | .deviceId(touchDeviceId) |
| 2551 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(300).y(100)) |
| 2552 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(350).y(100)) |
| 2553 | .build())); |
| 2554 | // The pointer_down event should be ignored |
| 2555 | window->assertNoEvents(); |
| 2556 | } |
| 2557 | |
| 2558 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2559 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2560 | * |
| 2561 | * Two windows: a window on the left and a window on the right. |
| 2562 | * Mouse is hovered over the left window. |
| 2563 | * Next, we tap on the left window, where the cursor was last seen. |
| 2564 | * |
| 2565 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2566 | * the left window. |
| 2567 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2568 | * window (first), and then another on the left window (second). |
| 2569 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2570 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2571 | */ |
| 2572 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2573 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2574 | sp<FakeWindowHandle> leftWindow = |
| 2575 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2576 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2577 | |
| 2578 | sp<FakeWindowHandle> rightWindow = |
| 2579 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2580 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2581 | |
| 2582 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2583 | |
| 2584 | const int32_t mouseDeviceId = 6; |
| 2585 | const int32_t touchDeviceId = 4; |
| 2586 | // Hover over the left window. Keep the cursor there. |
| 2587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2588 | injectMotionEvent(mDispatcher, |
| 2589 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2590 | AINPUT_SOURCE_MOUSE) |
| 2591 | .deviceId(mouseDeviceId) |
| 2592 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 2593 | .x(50) |
| 2594 | .y(50)) |
| 2595 | .build())); |
| 2596 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2597 | |
| 2598 | // Tap on left window |
| 2599 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2600 | injectMotionEvent(mDispatcher, |
| 2601 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2602 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2603 | .deviceId(touchDeviceId) |
| 2604 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2605 | .x(100) |
| 2606 | .y(100)) |
| 2607 | .build())); |
| 2608 | |
| 2609 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2610 | injectMotionEvent(mDispatcher, |
| 2611 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2612 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2613 | .deviceId(touchDeviceId) |
| 2614 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2615 | .x(100) |
| 2616 | .y(100)) |
| 2617 | .build())); |
| 2618 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2619 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2620 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2621 | |
| 2622 | // First finger down on right window |
| 2623 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2624 | injectMotionEvent(mDispatcher, |
| 2625 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2626 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2627 | .deviceId(touchDeviceId) |
| 2628 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2629 | .x(300) |
| 2630 | .y(100)) |
| 2631 | .build())); |
| 2632 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2633 | |
| 2634 | // Second finger down on the left window |
| 2635 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2636 | injectMotionEvent(mDispatcher, |
| 2637 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2638 | .deviceId(touchDeviceId) |
| 2639 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2640 | .x(300) |
| 2641 | .y(100)) |
| 2642 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2643 | .x(100) |
| 2644 | .y(100)) |
| 2645 | .build())); |
| 2646 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2647 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2648 | |
| 2649 | // No more events |
| 2650 | leftWindow->assertNoEvents(); |
| 2651 | rightWindow->assertNoEvents(); |
| 2652 | } |
| 2653 | |
| 2654 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2655 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2656 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2657 | * touch is gone, stylus hovering should start working again. |
| 2658 | */ |
| 2659 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 2660 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2661 | sp<FakeWindowHandle> window = |
| 2662 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2663 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2664 | |
| 2665 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2666 | |
| 2667 | const int32_t stylusDeviceId = 5; |
| 2668 | const int32_t touchDeviceId = 4; |
| 2669 | // Start hovering with stylus |
| 2670 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2671 | injectMotionEvent(mDispatcher, |
| 2672 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2673 | AINPUT_SOURCE_STYLUS) |
| 2674 | .deviceId(stylusDeviceId) |
| 2675 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 2676 | .x(50) |
| 2677 | .y(50)) |
| 2678 | .build())); |
| 2679 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2680 | |
| 2681 | // Finger down on the window |
| 2682 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2683 | injectMotionEvent(mDispatcher, |
| 2684 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2685 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2686 | .deviceId(touchDeviceId) |
| 2687 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2688 | .x(100) |
| 2689 | .y(100)) |
| 2690 | .build())); |
| 2691 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2692 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2693 | |
| 2694 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2695 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2696 | injectMotionEvent(mDispatcher, |
| 2697 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2698 | AINPUT_SOURCE_STYLUS) |
| 2699 | .deviceId(stylusDeviceId) |
| 2700 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 2701 | .x(50) |
| 2702 | .y(50)) |
| 2703 | .build())); |
| 2704 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2705 | // is already down. |
| 2706 | |
| 2707 | // Lift up the finger |
| 2708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2709 | injectMotionEvent(mDispatcher, |
| 2710 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2711 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2712 | .deviceId(touchDeviceId) |
| 2713 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 2714 | .x(100) |
| 2715 | .y(100)) |
| 2716 | .build())); |
| 2717 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2718 | |
| 2719 | // Now that the touch is gone, stylus hovering should start working again |
| 2720 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2721 | injectMotionEvent(mDispatcher, |
| 2722 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2723 | AINPUT_SOURCE_STYLUS) |
| 2724 | .deviceId(stylusDeviceId) |
| 2725 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 2726 | .x(50) |
| 2727 | .y(50)) |
| 2728 | .build())); |
| 2729 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2730 | // No more events |
| 2731 | window->assertNoEvents(); |
| 2732 | } |
| 2733 | |
| 2734 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2735 | * A spy window above a window with no input channel. |
| 2736 | * Start hovering with a stylus device, and then tap with it. |
| 2737 | * Ensure spy window receives the entire sequence. |
| 2738 | */ |
| 2739 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 2740 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2741 | sp<FakeWindowHandle> spyWindow = |
| 2742 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2743 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2744 | spyWindow->setTrustedOverlay(true); |
| 2745 | spyWindow->setSpy(true); |
| 2746 | sp<FakeWindowHandle> window = |
| 2747 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2748 | window->setNoInputChannel(true); |
| 2749 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2750 | |
| 2751 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2752 | |
| 2753 | NotifyMotionArgs args; |
| 2754 | |
| 2755 | // Start hovering with stylus |
| 2756 | mDispatcher->notifyMotion( |
| 2757 | &(args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2758 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS).x(50).y(50)) |
| 2759 | .build())); |
| 2760 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2761 | // Stop hovering |
| 2762 | mDispatcher->notifyMotion( |
| 2763 | &(args = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2764 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS).x(50).y(50)) |
| 2765 | .build())); |
| 2766 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2767 | |
| 2768 | // Stylus touches down |
| 2769 | mDispatcher->notifyMotion( |
| 2770 | &(args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2771 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS).x(50).y(50)) |
| 2772 | .build())); |
| 2773 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2774 | |
| 2775 | // Stylus goes up |
| 2776 | mDispatcher->notifyMotion( |
| 2777 | &(args = MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 2778 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS).x(50).y(50)) |
| 2779 | .build())); |
| 2780 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 2781 | |
| 2782 | // Again hover |
| 2783 | mDispatcher->notifyMotion( |
| 2784 | &(args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2785 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS).x(50).y(50)) |
| 2786 | .build())); |
| 2787 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2788 | // Stop hovering |
| 2789 | mDispatcher->notifyMotion( |
| 2790 | &(args = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2791 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS).x(50).y(50)) |
| 2792 | .build())); |
| 2793 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2794 | |
| 2795 | // No more events |
| 2796 | spyWindow->assertNoEvents(); |
| 2797 | window->assertNoEvents(); |
| 2798 | } |
| 2799 | |
| 2800 | /** |
| 2801 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 2802 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 2803 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 2804 | * While the mouse is down, new move events from the touch device should be ignored. |
| 2805 | */ |
| 2806 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 2807 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2808 | sp<FakeWindowHandle> spyWindow = |
| 2809 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2810 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2811 | spyWindow->setTrustedOverlay(true); |
| 2812 | spyWindow->setSpy(true); |
| 2813 | sp<FakeWindowHandle> window = |
| 2814 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2815 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2816 | |
| 2817 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2818 | |
| 2819 | const int32_t mouseDeviceId = 7; |
| 2820 | const int32_t touchDeviceId = 4; |
| 2821 | NotifyMotionArgs args; |
| 2822 | |
| 2823 | // Hover a bit with mouse first |
| 2824 | mDispatcher->notifyMotion(&( |
| 2825 | args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2826 | .deviceId(mouseDeviceId) |
| 2827 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(100).y(100)) |
| 2828 | .build())); |
| 2829 | spyWindow->consumeMotionEvent( |
| 2830 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2831 | window->consumeMotionEvent( |
| 2832 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2833 | |
| 2834 | // Start touching |
| 2835 | mDispatcher->notifyMotion( |
| 2836 | &(args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2837 | .deviceId(touchDeviceId) |
| 2838 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 2839 | .build())); |
| 2840 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2841 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2842 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2843 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2844 | |
| 2845 | mDispatcher->notifyMotion( |
| 2846 | &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2847 | .deviceId(touchDeviceId) |
| 2848 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(55).y(55)) |
| 2849 | .build())); |
| 2850 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2851 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2852 | |
| 2853 | // Pilfer the stream |
| 2854 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2855 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2856 | |
| 2857 | mDispatcher->notifyMotion( |
| 2858 | &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2859 | .deviceId(touchDeviceId) |
| 2860 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60)) |
| 2861 | .build())); |
| 2862 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2863 | |
| 2864 | // Mouse down |
| 2865 | mDispatcher->notifyMotion(&( |
| 2866 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2867 | .deviceId(mouseDeviceId) |
| 2868 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2869 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(100).y(100)) |
| 2870 | .build())); |
| 2871 | |
| 2872 | spyWindow->consumeMotionEvent( |
| 2873 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2874 | spyWindow->consumeMotionEvent( |
| 2875 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2876 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2877 | |
| 2878 | mDispatcher->notifyMotion(&( |
| 2879 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2880 | .deviceId(mouseDeviceId) |
| 2881 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2882 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2883 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(100).y(100)) |
| 2884 | .build())); |
| 2885 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2886 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2887 | |
| 2888 | // Mouse move! |
| 2889 | mDispatcher->notifyMotion(&( |
| 2890 | args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 2891 | .deviceId(mouseDeviceId) |
| 2892 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2893 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(110).y(110)) |
| 2894 | .build())); |
| 2895 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2896 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2897 | |
| 2898 | // Touch move! |
| 2899 | mDispatcher->notifyMotion( |
| 2900 | &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2901 | .deviceId(touchDeviceId) |
| 2902 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(65).y(65)) |
| 2903 | .build())); |
| 2904 | |
| 2905 | // No more events |
| 2906 | spyWindow->assertNoEvents(); |
| 2907 | window->assertNoEvents(); |
| 2908 | } |
| 2909 | |
| 2910 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2911 | * On the display, have a single window, and also an area where there's no window. |
| 2912 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2913 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2914 | */ |
| 2915 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2916 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2917 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2918 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2919 | |
| 2920 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 2921 | NotifyMotionArgs args; |
| 2922 | |
| 2923 | // Touch down on the empty space |
| 2924 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 2925 | |
| 2926 | mDispatcher->waitForIdle(); |
| 2927 | window->assertNoEvents(); |
| 2928 | |
| 2929 | // Now touch down on the window with another pointer |
| 2930 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 2931 | mDispatcher->waitForIdle(); |
| 2932 | window->consumeMotionDown(); |
| 2933 | } |
| 2934 | |
| 2935 | /** |
| 2936 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2937 | * non-touchable window. |
| 2938 | */ |
| 2939 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2940 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2941 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2942 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2943 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2944 | window1->setTouchable(false); |
| 2945 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2947 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2948 | |
| 2949 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2950 | |
| 2951 | NotifyMotionArgs args; |
| 2952 | // Touch down on the non-touchable window |
| 2953 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2954 | |
| 2955 | mDispatcher->waitForIdle(); |
| 2956 | window1->assertNoEvents(); |
| 2957 | window2->assertNoEvents(); |
| 2958 | |
| 2959 | // Now touch down on the window with another pointer |
| 2960 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2961 | mDispatcher->waitForIdle(); |
| 2962 | window2->consumeMotionDown(); |
| 2963 | } |
| 2964 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2965 | /** |
| 2966 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2967 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2968 | */ |
| 2969 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2970 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2971 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2972 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2973 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2974 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2975 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2976 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2977 | |
| 2978 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2979 | |
| 2980 | NotifyMotionArgs args; |
| 2981 | // Touch down on the first window |
| 2982 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 2983 | |
| 2984 | mDispatcher->waitForIdle(); |
| 2985 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2986 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2987 | window2->assertNoEvents(); |
| 2988 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2989 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2990 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2991 | |
| 2992 | // Now touch down on the window with another pointer |
| 2993 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 2994 | mDispatcher->waitForIdle(); |
| 2995 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2996 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2997 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2998 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2999 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 3000 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 3001 | |
| 3002 | // Now move the pointer on the second window |
| 3003 | mDispatcher->notifyMotion( |
| 3004 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); |
| 3005 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3006 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3007 | |
| 3008 | // Now add new touch down on the second window |
| 3009 | mDispatcher->notifyMotion( |
| 3010 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); |
| 3011 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3012 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3013 | |
| 3014 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3015 | window1->consumeMotionMove(); |
| 3016 | window1->assertNoEvents(); |
| 3017 | |
| 3018 | // Now move the pointer on the first window |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame^] | 3019 | mDispatcher->notifyMotion(&( |
| 3020 | args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}))); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3021 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3022 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3023 | |
| 3024 | mDispatcher->notifyMotion(&( |
| 3025 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); |
| 3026 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3027 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3030 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3031 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3032 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3033 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3034 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3035 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3036 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3037 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3038 | |
| 3039 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3040 | |
| 3041 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 3042 | |
| 3043 | // 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] | 3044 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3045 | injectMotionEvent(mDispatcher, |
| 3046 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3047 | AINPUT_SOURCE_MOUSE) |
| 3048 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3049 | .x(900) |
| 3050 | .y(400)) |
| 3051 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3052 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3053 | |
| 3054 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3055 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3056 | injectMotionEvent(mDispatcher, |
| 3057 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3058 | AINPUT_SOURCE_MOUSE) |
| 3059 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3060 | .x(300) |
| 3061 | .y(400)) |
| 3062 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3063 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3064 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3065 | |
| 3066 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3067 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3068 | injectMotionEvent(mDispatcher, |
| 3069 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3070 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3071 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3072 | .x(300) |
| 3073 | .y(400)) |
| 3074 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3075 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3076 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3077 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3078 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3079 | injectMotionEvent(mDispatcher, |
| 3080 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3081 | AINPUT_SOURCE_MOUSE) |
| 3082 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3083 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3084 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3085 | .x(300) |
| 3086 | .y(400)) |
| 3087 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3088 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3089 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3090 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3091 | injectMotionEvent(mDispatcher, |
| 3092 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3093 | AINPUT_SOURCE_MOUSE) |
| 3094 | .buttonState(0) |
| 3095 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3096 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3097 | .x(300) |
| 3098 | .y(400)) |
| 3099 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3100 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3101 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3102 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3103 | injectMotionEvent(mDispatcher, |
| 3104 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3105 | .buttonState(0) |
| 3106 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3107 | .x(300) |
| 3108 | .y(400)) |
| 3109 | .build())); |
| 3110 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3111 | |
| 3112 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3113 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3114 | injectMotionEvent(mDispatcher, |
| 3115 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3116 | AINPUT_SOURCE_MOUSE) |
| 3117 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3118 | .x(900) |
| 3119 | .y(400)) |
| 3120 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3121 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3122 | |
| 3123 | // No more events |
| 3124 | windowLeft->assertNoEvents(); |
| 3125 | windowRight->assertNoEvents(); |
| 3126 | } |
| 3127 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3128 | /** |
| 3129 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3130 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3131 | * currently active gesture should be canceled, and the new one should proceed. |
| 3132 | */ |
| 3133 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3134 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3135 | sp<FakeWindowHandle> window = |
| 3136 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3137 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3138 | |
| 3139 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3140 | |
| 3141 | const int32_t touchDeviceId = 4; |
| 3142 | const int32_t mouseDeviceId = 6; |
| 3143 | NotifyMotionArgs args; |
| 3144 | |
| 3145 | // Two pointers down |
| 3146 | mDispatcher->notifyMotion(&( |
| 3147 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3148 | .deviceId(touchDeviceId) |
| 3149 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 3150 | .build())); |
| 3151 | |
| 3152 | mDispatcher->notifyMotion(&( |
| 3153 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3154 | .deviceId(touchDeviceId) |
| 3155 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(100)) |
| 3156 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(120).y(120)) |
| 3157 | .build())); |
| 3158 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3159 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3160 | |
| 3161 | // Inject a series of mouse events for a mouse click |
| 3162 | mDispatcher->notifyMotion(&( |
| 3163 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3164 | .deviceId(mouseDeviceId) |
| 3165 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3166 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(300).y(400)) |
| 3167 | .build())); |
| 3168 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3169 | WithPointerCount(2u))); |
| 3170 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3171 | |
| 3172 | mDispatcher->notifyMotion(&( |
| 3173 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3174 | .deviceId(mouseDeviceId) |
| 3175 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3176 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3177 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE).x(300).y(400)) |
| 3178 | .build())); |
| 3179 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3180 | |
| 3181 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3182 | // already canceled gesture, it should be ignored. |
| 3183 | mDispatcher->notifyMotion(&( |
| 3184 | args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3185 | .deviceId(touchDeviceId) |
| 3186 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(101).y(101)) |
| 3187 | .pointer(PointerBuilder(1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(121).y(121)) |
| 3188 | .build())); |
| 3189 | window->assertNoEvents(); |
| 3190 | } |
| 3191 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3192 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3193 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3194 | |
| 3195 | sp<FakeWindowHandle> spyWindow = |
| 3196 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3197 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3198 | spyWindow->setTrustedOverlay(true); |
| 3199 | spyWindow->setSpy(true); |
| 3200 | sp<FakeWindowHandle> window = |
| 3201 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3202 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3203 | |
| 3204 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3205 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3206 | |
| 3207 | // Send mouse cursor to the window |
| 3208 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3209 | injectMotionEvent(mDispatcher, |
| 3210 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3211 | AINPUT_SOURCE_MOUSE) |
| 3212 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3213 | .x(100) |
| 3214 | .y(100)) |
| 3215 | .build())); |
| 3216 | |
| 3217 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3218 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3219 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3220 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3221 | |
| 3222 | window->assertNoEvents(); |
| 3223 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3224 | } |
| 3225 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3226 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3227 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3228 | |
| 3229 | sp<FakeWindowHandle> spyWindow = |
| 3230 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3231 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3232 | spyWindow->setTrustedOverlay(true); |
| 3233 | spyWindow->setSpy(true); |
| 3234 | sp<FakeWindowHandle> window = |
| 3235 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3236 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3237 | |
| 3238 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3239 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3240 | |
| 3241 | // Send mouse cursor to the window |
| 3242 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3243 | injectMotionEvent(mDispatcher, |
| 3244 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3245 | AINPUT_SOURCE_MOUSE) |
| 3246 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3247 | .x(100) |
| 3248 | .y(100)) |
| 3249 | .build())); |
| 3250 | |
| 3251 | // Move mouse cursor |
| 3252 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3253 | injectMotionEvent(mDispatcher, |
| 3254 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3255 | AINPUT_SOURCE_MOUSE) |
| 3256 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3257 | .x(110) |
| 3258 | .y(110)) |
| 3259 | .build())); |
| 3260 | |
| 3261 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3262 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3263 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3264 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3265 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3266 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3267 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3268 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3269 | // Touch down on the window |
| 3270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3271 | injectMotionEvent(mDispatcher, |
| 3272 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3273 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3274 | .deviceId(SECOND_DEVICE_ID) |
| 3275 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 3276 | .x(200) |
| 3277 | .y(200)) |
| 3278 | .build())); |
| 3279 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3280 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3281 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3282 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3283 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3284 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3285 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3286 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3287 | |
| 3288 | // pilfer the motion, retaining the gesture on the spy window. |
| 3289 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3290 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3291 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3292 | |
| 3293 | // Touch UP on the window |
| 3294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3295 | injectMotionEvent(mDispatcher, |
| 3296 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3297 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3298 | .deviceId(SECOND_DEVICE_ID) |
| 3299 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 3300 | .x(200) |
| 3301 | .y(200)) |
| 3302 | .build())); |
| 3303 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3304 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3305 | |
| 3306 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3307 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3308 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3309 | |
| 3310 | // One more tap - DOWN |
| 3311 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3312 | injectMotionEvent(mDispatcher, |
| 3313 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3314 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3315 | .deviceId(SECOND_DEVICE_ID) |
| 3316 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 3317 | .x(250) |
| 3318 | .y(250)) |
| 3319 | .build())); |
| 3320 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3321 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3322 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3323 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3324 | |
| 3325 | // Touch UP on the window |
| 3326 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3327 | injectMotionEvent(mDispatcher, |
| 3328 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3329 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3330 | .deviceId(SECOND_DEVICE_ID) |
| 3331 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 3332 | .x(250) |
| 3333 | .y(250)) |
| 3334 | .build())); |
| 3335 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3336 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3337 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3338 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3339 | |
| 3340 | window->assertNoEvents(); |
| 3341 | spyWindow->assertNoEvents(); |
| 3342 | } |
| 3343 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3344 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3345 | // directly in this test. |
| 3346 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3347 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3348 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3349 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3350 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3351 | |
| 3352 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3353 | |
| 3354 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3355 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3356 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3357 | injectMotionEvent(mDispatcher, |
| 3358 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3359 | AINPUT_SOURCE_MOUSE) |
| 3360 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3361 | .x(300) |
| 3362 | .y(400)) |
| 3363 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3364 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3365 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3367 | injectMotionEvent(mDispatcher, |
| 3368 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3369 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3370 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3371 | .x(300) |
| 3372 | .y(400)) |
| 3373 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3374 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3375 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3376 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3377 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3378 | injectMotionEvent(mDispatcher, |
| 3379 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3380 | AINPUT_SOURCE_MOUSE) |
| 3381 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3382 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3383 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3384 | .x(300) |
| 3385 | .y(400)) |
| 3386 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3387 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3388 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3389 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3390 | injectMotionEvent(mDispatcher, |
| 3391 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3392 | AINPUT_SOURCE_MOUSE) |
| 3393 | .buttonState(0) |
| 3394 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3395 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3396 | .x(300) |
| 3397 | .y(400)) |
| 3398 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3399 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3400 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3401 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3402 | injectMotionEvent(mDispatcher, |
| 3403 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3404 | .buttonState(0) |
| 3405 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3406 | .x(300) |
| 3407 | .y(400)) |
| 3408 | .build())); |
| 3409 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3410 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3412 | injectMotionEvent(mDispatcher, |
| 3413 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3414 | AINPUT_SOURCE_MOUSE) |
| 3415 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3416 | .x(300) |
| 3417 | .y(400)) |
| 3418 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3419 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3420 | } |
| 3421 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3422 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3423 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3424 | * is generated. |
| 3425 | */ |
| 3426 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3427 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3428 | sp<FakeWindowHandle> window = |
| 3429 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3430 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3431 | |
| 3432 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3433 | |
| 3434 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3435 | |
| 3436 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3437 | injectMotionEvent(mDispatcher, |
| 3438 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3439 | AINPUT_SOURCE_MOUSE) |
| 3440 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3441 | .x(300) |
| 3442 | .y(400)) |
| 3443 | .build())); |
| 3444 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3445 | |
| 3446 | // Remove the window, but keep the channel. |
| 3447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3448 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3449 | } |
| 3450 | |
| 3451 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3452 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3453 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3454 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3455 | */ |
| 3456 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3457 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3458 | sp<FakeWindowHandle> window = |
| 3459 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3460 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3461 | |
| 3462 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3463 | |
| 3464 | // Inject a hover_move from mouse. |
| 3465 | NotifyMotionArgs motionArgs = |
| 3466 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 3467 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3468 | motionArgs.xCursorPosition = 50; |
| 3469 | motionArgs.yCursorPosition = 50; |
| 3470 | mDispatcher->notifyMotion(&motionArgs); |
| 3471 | ASSERT_NO_FATAL_FAILURE( |
| 3472 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3473 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3474 | |
| 3475 | // Tap on the window |
| 3476 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3477 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 3478 | mDispatcher->notifyMotion(&motionArgs); |
| 3479 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3480 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3481 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3482 | |
| 3483 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3484 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3485 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3486 | |
| 3487 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3488 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 3489 | mDispatcher->notifyMotion(&motionArgs); |
| 3490 | ASSERT_NO_FATAL_FAILURE( |
| 3491 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3492 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3493 | } |
| 3494 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3495 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3496 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3497 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3498 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3499 | ADISPLAY_ID_DEFAULT); |
| 3500 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3501 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3502 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3503 | SECOND_DISPLAY_ID); |
| 3504 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3505 | |
| 3506 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3507 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3508 | |
| 3509 | // Set cursor position in window in default display and check that hover enter and move |
| 3510 | // events are generated. |
| 3511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3512 | injectMotionEvent(mDispatcher, |
| 3513 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3514 | AINPUT_SOURCE_MOUSE) |
| 3515 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3516 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3517 | .x(300) |
| 3518 | .y(600)) |
| 3519 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3520 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3521 | |
| 3522 | // Remove all windows in secondary display and check that no event happens on window in |
| 3523 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3524 | mDispatcher->setInputWindows( |
| 3525 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3526 | windowDefaultDisplay->assertNoEvents(); |
| 3527 | |
| 3528 | // Move cursor position in window in default display and check that only hover move |
| 3529 | // event is generated and not hover enter event. |
| 3530 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3531 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3532 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3533 | injectMotionEvent(mDispatcher, |
| 3534 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3535 | AINPUT_SOURCE_MOUSE) |
| 3536 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3537 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 3538 | .x(400) |
| 3539 | .y(700)) |
| 3540 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3541 | windowDefaultDisplay->consumeMotionEvent( |
| 3542 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3543 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3544 | windowDefaultDisplay->assertNoEvents(); |
| 3545 | } |
| 3546 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3547 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3548 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3549 | |
| 3550 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3551 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3552 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3553 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3554 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3555 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3556 | |
| 3557 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3558 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3559 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3560 | |
| 3561 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3562 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3563 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3564 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3565 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3566 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3567 | windowRight->assertNoEvents(); |
| 3568 | } |
| 3569 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3570 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3571 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3572 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3573 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3574 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3575 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3576 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3577 | setFocusedWindow(window); |
| 3578 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3579 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3580 | |
| 3581 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3582 | mDispatcher->notifyKey(&keyArgs); |
| 3583 | |
| 3584 | // Window should receive key down event. |
| 3585 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3586 | |
| 3587 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3588 | // on the app side. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3589 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3590 | mDispatcher->notifyDeviceReset(&args); |
| 3591 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 3592 | AKEY_EVENT_FLAG_CANCELED); |
| 3593 | } |
| 3594 | |
| 3595 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3596 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3597 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3598 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3599 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3600 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3601 | |
| 3602 | NotifyMotionArgs motionArgs = |
| 3603 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3604 | ADISPLAY_ID_DEFAULT); |
| 3605 | mDispatcher->notifyMotion(&motionArgs); |
| 3606 | |
| 3607 | // Window should receive motion down event. |
| 3608 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3609 | |
| 3610 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 3611 | // on the app side. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3612 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3613 | mDispatcher->notifyDeviceReset(&args); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3614 | window->consumeMotionEvent( |
| 3615 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3616 | } |
| 3617 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3618 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 3619 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3620 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3621 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3622 | window->setFocusable(true); |
| 3623 | |
| 3624 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3625 | setFocusedWindow(window); |
| 3626 | |
| 3627 | window->consumeFocusEvent(true); |
| 3628 | |
| 3629 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3630 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3631 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3632 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 3633 | mDispatcher->notifyKey(&keyArgs); |
| 3634 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3635 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3636 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3637 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3638 | } |
| 3639 | |
| 3640 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3641 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3642 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3643 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3644 | window->setFocusable(true); |
| 3645 | |
| 3646 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3647 | setFocusedWindow(window); |
| 3648 | |
| 3649 | window->consumeFocusEvent(true); |
| 3650 | |
| 3651 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3652 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3653 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 3654 | mDispatcher->notifyKey(&keyDown); |
| 3655 | mDispatcher->notifyKey(&keyUp); |
| 3656 | |
| 3657 | // Window should receive key event immediately when same key up. |
| 3658 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3659 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3660 | } |
| 3661 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3662 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3663 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3664 | * WATCH_OUTSIDE_TOUCH. |
| 3665 | * Both windows are owned by the same UID. |
| 3666 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3667 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3668 | */ |
| 3669 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3670 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3671 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3672 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3673 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3674 | |
| 3675 | sp<FakeWindowHandle> outsideWindow = |
| 3676 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3677 | ADISPLAY_ID_DEFAULT); |
| 3678 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3679 | outsideWindow->setWatchOutsideTouch(true); |
| 3680 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3682 | |
| 3683 | // Tap on first window. |
| 3684 | NotifyMotionArgs motionArgs = |
| 3685 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3686 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 3687 | mDispatcher->notifyMotion(&motionArgs); |
| 3688 | window->consumeMotionDown(); |
| 3689 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3690 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3691 | outsideWindow->consumeMotionEvent( |
| 3692 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3693 | } |
| 3694 | |
| 3695 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3696 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3697 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3698 | * ACTION_OUTSIDE event is sent per gesture. |
| 3699 | */ |
| 3700 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3701 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 3702 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3703 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3704 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3705 | window->setWatchOutsideTouch(true); |
| 3706 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3707 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3708 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3709 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3710 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3711 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3712 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3713 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3714 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3716 | |
| 3717 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 3718 | NotifyMotionArgs motionArgs = |
| 3719 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3720 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 3721 | mDispatcher->notifyMotion(&motionArgs); |
| 3722 | window->assertNoEvents(); |
| 3723 | secondWindow->assertNoEvents(); |
| 3724 | |
| 3725 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3726 | // Now, `window` should get ACTION_OUTSIDE. |
| 3727 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3728 | {PointF{-10, -10}, PointF{105, 105}}); |
| 3729 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3730 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3731 | window->consumeMotionEvent( |
| 3732 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3733 | secondWindow->consumeMotionDown(); |
| 3734 | thirdWindow->assertNoEvents(); |
| 3735 | |
| 3736 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3737 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 3738 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3739 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 3740 | mDispatcher->notifyMotion(&motionArgs); |
| 3741 | window->assertNoEvents(); |
| 3742 | secondWindow->consumeMotionMove(); |
| 3743 | thirdWindow->consumeMotionDown(); |
| 3744 | } |
| 3745 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3746 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3747 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3748 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3749 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3750 | window->setFocusable(true); |
| 3751 | |
| 3752 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 3753 | setFocusedWindow(window); |
| 3754 | |
| 3755 | window->consumeFocusEvent(true); |
| 3756 | |
| 3757 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3758 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3759 | mDispatcher->notifyKey(&keyDown); |
| 3760 | mDispatcher->notifyKey(&keyUp); |
| 3761 | |
| 3762 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3763 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3764 | |
| 3765 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
| 3766 | mDispatcher->onWindowInfosChanged({}, {}); |
| 3767 | |
| 3768 | window->consumeFocusEvent(false); |
| 3769 | |
| 3770 | mDispatcher->notifyKey(&keyDown); |
| 3771 | mDispatcher->notifyKey(&keyUp); |
| 3772 | window->assertNoEvents(); |
| 3773 | } |
| 3774 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3775 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3776 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3777 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3778 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3779 | // Ensure window is non-split and have some transform. |
| 3780 | window->setPreventSplitting(true); |
| 3781 | window->setWindowOffset(20, 40); |
| 3782 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 3783 | |
| 3784 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3785 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3786 | {50, 50})) |
| 3787 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3788 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3789 | |
| 3790 | const MotionEvent secondFingerDownEvent = |
| 3791 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3792 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3793 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3794 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 3795 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3796 | .build(); |
| 3797 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3798 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3799 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3800 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3801 | |
| 3802 | const MotionEvent* event = window->consumeMotion(); |
| 3803 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3804 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3805 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3806 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3807 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3808 | } |
| 3809 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3810 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3811 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 3812 | * |
| 3813 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 3814 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 3815 | * space. |
| 3816 | */ |
| 3817 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 3818 | public: |
| 3819 | void SetUp() override { |
| 3820 | InputDispatcherTest::SetUp(); |
| 3821 | mDisplayInfos.clear(); |
| 3822 | mWindowInfos.clear(); |
| 3823 | } |
| 3824 | |
| 3825 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 3826 | gui::DisplayInfo info; |
| 3827 | info.displayId = displayId; |
| 3828 | info.transform = transform; |
| 3829 | mDisplayInfos.push_back(std::move(info)); |
| 3830 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3831 | } |
| 3832 | |
| 3833 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 3834 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 3835 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3836 | } |
| 3837 | |
| 3838 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 3839 | // on the display. |
| 3840 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 3841 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 3842 | // respectively. |
| 3843 | ui::Transform displayTransform; |
| 3844 | displayTransform.set(2, 0, 0, 4); |
| 3845 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 3846 | |
| 3847 | std::shared_ptr<FakeApplicationHandle> application = |
| 3848 | std::make_shared<FakeApplicationHandle>(); |
| 3849 | |
| 3850 | // Add two windows to the display. Their frames are represented in the display space. |
| 3851 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3852 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3853 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3854 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 3855 | addWindow(firstWindow); |
| 3856 | |
| 3857 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3858 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3859 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3860 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 3861 | addWindow(secondWindow); |
| 3862 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 3863 | } |
| 3864 | |
| 3865 | private: |
| 3866 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 3867 | std::vector<gui::WindowInfo> mWindowInfos; |
| 3868 | }; |
| 3869 | |
| 3870 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestsInDisplaySpace) { |
| 3871 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3872 | // Send down to the first window. The point is represented in the display space. The point is |
| 3873 | // selected so that if the hit test was done with the transform applied to it, then it would |
| 3874 | // end up in the incorrect window. |
| 3875 | NotifyMotionArgs downMotionArgs = |
| 3876 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3877 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 3878 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3879 | |
| 3880 | firstWindow->consumeMotionDown(); |
| 3881 | secondWindow->assertNoEvents(); |
| 3882 | } |
| 3883 | |
| 3884 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 3885 | // the event should be treated as being in the logical display space. |
| 3886 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 3887 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3888 | // Send down to the first window. The point is represented in the logical display space. The |
| 3889 | // point is selected so that if the hit test was done in logical display space, then it would |
| 3890 | // end up in the incorrect window. |
| 3891 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3892 | PointF{75 * 2, 55 * 4}); |
| 3893 | |
| 3894 | firstWindow->consumeMotionDown(); |
| 3895 | secondWindow->assertNoEvents(); |
| 3896 | } |
| 3897 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3898 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 3899 | // event should be treated as being in the logical display space. |
| 3900 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 3901 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3902 | |
| 3903 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 3904 | ui::Transform injectedEventTransform; |
| 3905 | injectedEventTransform.set(matrix); |
| 3906 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 3907 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 3908 | |
| 3909 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3910 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3911 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3912 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3913 | .x(untransformedPoint.x) |
| 3914 | .y(untransformedPoint.y)) |
| 3915 | .build(); |
| 3916 | event.transform(matrix); |
| 3917 | |
| 3918 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 3919 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 3920 | |
| 3921 | firstWindow->consumeMotionDown(); |
| 3922 | secondWindow->assertNoEvents(); |
| 3923 | } |
| 3924 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3925 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 3926 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3927 | |
| 3928 | // Send down to the second window. |
| 3929 | NotifyMotionArgs downMotionArgs = |
| 3930 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3931 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 3932 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3933 | |
| 3934 | firstWindow->assertNoEvents(); |
| 3935 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 3936 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 3937 | |
| 3938 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 3939 | EXPECT_EQ(300, event->getRawX(0)); |
| 3940 | EXPECT_EQ(880, event->getRawY(0)); |
| 3941 | |
| 3942 | // Ensure that the x and y values are in the window's coordinate space. |
| 3943 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 3944 | // the logical display space. This will be the origin of the window space. |
| 3945 | EXPECT_EQ(100, event->getX(0)); |
| 3946 | EXPECT_EQ(80, event->getY(0)); |
| 3947 | } |
| 3948 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 3949 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 3950 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3951 | |
| 3952 | class TransferTouchFixture : public InputDispatcherTest, |
| 3953 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 3954 | |
| 3955 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3956 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3957 | |
| 3958 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3959 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3960 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3961 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3962 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3963 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3964 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3965 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3966 | sp<FakeWindowHandle> wallpaper = |
| 3967 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 3968 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3969 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3970 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3971 | |
| 3972 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3973 | NotifyMotionArgs downMotionArgs = |
| 3974 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3975 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3976 | mDispatcher->notifyMotion(&downMotionArgs); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3977 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3978 | // Only the first window should get the down event |
| 3979 | firstWindow->consumeMotionDown(); |
| 3980 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3981 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3982 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 3983 | // Transfer touch to the second window |
| 3984 | TransferFunction f = GetParam(); |
| 3985 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 3986 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3987 | // The first window gets cancel and the second gets down |
| 3988 | firstWindow->consumeMotionCancel(); |
| 3989 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 3990 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3991 | |
| 3992 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 3993 | NotifyMotionArgs upMotionArgs = |
| 3994 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3995 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 3996 | mDispatcher->notifyMotion(&upMotionArgs); |
| 3997 | // The first window gets no events and the second gets up |
| 3998 | firstWindow->assertNoEvents(); |
| 3999 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4000 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4001 | } |
| 4002 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4003 | /** |
| 4004 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 4005 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 4006 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 4007 | * natural to the user. |
| 4008 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 4009 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 4010 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 4011 | * the first window to the second. |
| 4012 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 4013 | * the other API, as well. |
| 4014 | */ |
| 4015 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 4016 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4017 | |
| 4018 | // Create a couple of windows + a spy window |
| 4019 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4020 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4021 | spyWindow->setTrustedOverlay(true); |
| 4022 | spyWindow->setSpy(true); |
| 4023 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4024 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4025 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4026 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4027 | |
| 4028 | // Add the windows to the dispatcher |
| 4029 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 4030 | |
| 4031 | // Send down to the first window |
| 4032 | NotifyMotionArgs downMotionArgs = |
| 4033 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4034 | ADISPLAY_ID_DEFAULT); |
| 4035 | mDispatcher->notifyMotion(&downMotionArgs); |
| 4036 | // Only the first window and spy should get the down event |
| 4037 | spyWindow->consumeMotionDown(); |
| 4038 | firstWindow->consumeMotionDown(); |
| 4039 | |
| 4040 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 4041 | // if f === 'transferTouch'. |
| 4042 | TransferFunction f = GetParam(); |
| 4043 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4044 | ASSERT_TRUE(success); |
| 4045 | // The first window gets cancel and the second gets down |
| 4046 | firstWindow->consumeMotionCancel(); |
| 4047 | secondWindow->consumeMotionDown(); |
| 4048 | |
| 4049 | // Send up event to the second window |
| 4050 | NotifyMotionArgs upMotionArgs = |
| 4051 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4052 | ADISPLAY_ID_DEFAULT); |
| 4053 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4054 | // The first window gets no events and the second+spy get up |
| 4055 | firstWindow->assertNoEvents(); |
| 4056 | spyWindow->consumeMotionUp(); |
| 4057 | secondWindow->consumeMotionUp(); |
| 4058 | } |
| 4059 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4060 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4061 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4062 | |
| 4063 | PointF touchPoint = {10, 10}; |
| 4064 | |
| 4065 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4066 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4067 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4068 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4069 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4070 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4071 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4072 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4073 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4074 | |
| 4075 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4076 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4077 | |
| 4078 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4079 | NotifyMotionArgs downMotionArgs = |
| 4080 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4081 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4082 | mDispatcher->notifyMotion(&downMotionArgs); |
| 4083 | // Only the first window should get the down event |
| 4084 | firstWindow->consumeMotionDown(); |
| 4085 | secondWindow->assertNoEvents(); |
| 4086 | |
| 4087 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4088 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4089 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4090 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4091 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 4092 | // Only the first window should get the pointer down event |
| 4093 | firstWindow->consumeMotionPointerDown(1); |
| 4094 | secondWindow->assertNoEvents(); |
| 4095 | |
| 4096 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4097 | TransferFunction f = GetParam(); |
| 4098 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4099 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4100 | // The first window gets cancel and the second gets down and pointer down |
| 4101 | firstWindow->consumeMotionCancel(); |
| 4102 | secondWindow->consumeMotionDown(); |
| 4103 | secondWindow->consumeMotionPointerDown(1); |
| 4104 | |
| 4105 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4106 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4107 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4108 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4109 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 4110 | // The first window gets nothing and the second gets pointer up |
| 4111 | firstWindow->assertNoEvents(); |
| 4112 | secondWindow->consumeMotionPointerUp(1); |
| 4113 | |
| 4114 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4115 | NotifyMotionArgs upMotionArgs = |
| 4116 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4117 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4118 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4119 | // The first window gets nothing and the second gets up |
| 4120 | firstWindow->assertNoEvents(); |
| 4121 | secondWindow->consumeMotionUp(); |
| 4122 | } |
| 4123 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4124 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 4125 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4126 | |
| 4127 | // Create a couple of windows |
| 4128 | sp<FakeWindowHandle> firstWindow = |
| 4129 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4130 | ADISPLAY_ID_DEFAULT); |
| 4131 | firstWindow->setDupTouchToWallpaper(true); |
| 4132 | sp<FakeWindowHandle> secondWindow = |
| 4133 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4134 | ADISPLAY_ID_DEFAULT); |
| 4135 | secondWindow->setDupTouchToWallpaper(true); |
| 4136 | |
| 4137 | sp<FakeWindowHandle> wallpaper1 = |
| 4138 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 4139 | wallpaper1->setIsWallpaper(true); |
| 4140 | |
| 4141 | sp<FakeWindowHandle> wallpaper2 = |
| 4142 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 4143 | wallpaper2->setIsWallpaper(true); |
| 4144 | // Add the windows to the dispatcher |
| 4145 | mDispatcher->setInputWindows( |
| 4146 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 4147 | |
| 4148 | // Send down to the first window |
| 4149 | NotifyMotionArgs downMotionArgs = |
| 4150 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4151 | ADISPLAY_ID_DEFAULT); |
| 4152 | mDispatcher->notifyMotion(&downMotionArgs); |
| 4153 | |
| 4154 | // Only the first window should get the down event |
| 4155 | firstWindow->consumeMotionDown(); |
| 4156 | secondWindow->assertNoEvents(); |
| 4157 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4158 | wallpaper2->assertNoEvents(); |
| 4159 | |
| 4160 | // Transfer touch focus to the second window |
| 4161 | TransferFunction f = GetParam(); |
| 4162 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4163 | ASSERT_TRUE(success); |
| 4164 | |
| 4165 | // The first window gets cancel and the second gets down |
| 4166 | firstWindow->consumeMotionCancel(); |
| 4167 | secondWindow->consumeMotionDown(); |
| 4168 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4169 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4170 | |
| 4171 | // Send up event to the second window |
| 4172 | NotifyMotionArgs upMotionArgs = |
| 4173 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4174 | ADISPLAY_ID_DEFAULT); |
| 4175 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4176 | // The first window gets no events and the second gets up |
| 4177 | firstWindow->assertNoEvents(); |
| 4178 | secondWindow->consumeMotionUp(); |
| 4179 | wallpaper1->assertNoEvents(); |
| 4180 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4181 | } |
| 4182 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4183 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 4184 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 4185 | // for the case where there are multiple pointers split across several windows. |
| 4186 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 4187 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4188 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4189 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4190 | return dispatcher->transferTouch(destChannelToken, |
| 4191 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4192 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4193 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4194 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4195 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4196 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4197 | })); |
| 4198 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4199 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4200 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4201 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4202 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4203 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4204 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4205 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4206 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4207 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4208 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4209 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4210 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4211 | |
| 4212 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4213 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4214 | |
| 4215 | PointF pointInFirst = {300, 200}; |
| 4216 | PointF pointInSecond = {300, 600}; |
| 4217 | |
| 4218 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4219 | NotifyMotionArgs firstDownMotionArgs = |
| 4220 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4221 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4222 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 4223 | // Only the first window should get the down event |
| 4224 | firstWindow->consumeMotionDown(); |
| 4225 | secondWindow->assertNoEvents(); |
| 4226 | |
| 4227 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4228 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4229 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4230 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4231 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 4232 | // The first window gets a move and the second a down |
| 4233 | firstWindow->consumeMotionMove(); |
| 4234 | secondWindow->consumeMotionDown(); |
| 4235 | |
| 4236 | // Transfer touch focus to the second window |
| 4237 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4238 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 4239 | firstWindow->consumeMotionCancel(); |
| 4240 | secondWindow->consumeMotionPointerDown(1); |
| 4241 | |
| 4242 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4243 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4244 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4245 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4246 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 4247 | // The first window gets nothing and the second gets pointer up |
| 4248 | firstWindow->assertNoEvents(); |
| 4249 | secondWindow->consumeMotionPointerUp(1); |
| 4250 | |
| 4251 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4252 | NotifyMotionArgs upMotionArgs = |
| 4253 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4254 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4255 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4256 | // The first window gets nothing and the second gets up |
| 4257 | firstWindow->assertNoEvents(); |
| 4258 | secondWindow->consumeMotionUp(); |
| 4259 | } |
| 4260 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4261 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 4262 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 4263 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 4264 | // window should get nothing. |
| 4265 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 4266 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4267 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4268 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4269 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4270 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4271 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4272 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4273 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4274 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4275 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4276 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4277 | |
| 4278 | // Add the windows to the dispatcher |
| 4279 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4280 | |
| 4281 | PointF pointInFirst = {300, 200}; |
| 4282 | PointF pointInSecond = {300, 600}; |
| 4283 | |
| 4284 | // Send down to the first window |
| 4285 | NotifyMotionArgs firstDownMotionArgs = |
| 4286 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4287 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 4288 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 4289 | // Only the first window should get the down event |
| 4290 | firstWindow->consumeMotionDown(); |
| 4291 | secondWindow->assertNoEvents(); |
| 4292 | |
| 4293 | // Send down to the second window |
| 4294 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4295 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4296 | {pointInFirst, pointInSecond}); |
| 4297 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 4298 | // The first window gets a move and the second a down |
| 4299 | firstWindow->consumeMotionMove(); |
| 4300 | secondWindow->consumeMotionDown(); |
| 4301 | |
| 4302 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4303 | const bool transferred = |
| 4304 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4305 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 4306 | ASSERT_FALSE(transferred); |
| 4307 | firstWindow->assertNoEvents(); |
| 4308 | secondWindow->assertNoEvents(); |
| 4309 | |
| 4310 | // The rest of the dispatch should proceed as normal |
| 4311 | // Send pointer up to the second window |
| 4312 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4313 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4314 | {pointInFirst, pointInSecond}); |
| 4315 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 4316 | // The first window gets MOVE and the second gets pointer up |
| 4317 | firstWindow->consumeMotionMove(); |
| 4318 | secondWindow->consumeMotionUp(); |
| 4319 | |
| 4320 | // Send up event to the first window |
| 4321 | NotifyMotionArgs upMotionArgs = |
| 4322 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4323 | ADISPLAY_ID_DEFAULT); |
| 4324 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4325 | // The first window gets nothing and the second gets up |
| 4326 | firstWindow->consumeMotionUp(); |
| 4327 | secondWindow->assertNoEvents(); |
| 4328 | } |
| 4329 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4330 | // This case will create two windows and one mirrored window on the default display and mirror |
| 4331 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 4332 | // the windows info of second display before default display. |
| 4333 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 4334 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4335 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4336 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4337 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4338 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4339 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4340 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4341 | |
| 4342 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4343 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4344 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4345 | |
| 4346 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4347 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4348 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4349 | |
| 4350 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4351 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4352 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4353 | |
| 4354 | // Update window info, let it find window handle of second display first. |
| 4355 | mDispatcher->setInputWindows( |
| 4356 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4357 | {ADISPLAY_ID_DEFAULT, |
| 4358 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4359 | |
| 4360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4361 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4362 | {50, 50})) |
| 4363 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4364 | |
| 4365 | // Window should receive motion event. |
| 4366 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4367 | |
| 4368 | // Transfer touch focus |
| 4369 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 4370 | secondWindowInPrimary->getToken())); |
| 4371 | // The first window gets cancel. |
| 4372 | firstWindowInPrimary->consumeMotionCancel(); |
| 4373 | secondWindowInPrimary->consumeMotionDown(); |
| 4374 | |
| 4375 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4376 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4377 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4378 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4379 | firstWindowInPrimary->assertNoEvents(); |
| 4380 | secondWindowInPrimary->consumeMotionMove(); |
| 4381 | |
| 4382 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4383 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4384 | {150, 50})) |
| 4385 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4386 | firstWindowInPrimary->assertNoEvents(); |
| 4387 | secondWindowInPrimary->consumeMotionUp(); |
| 4388 | } |
| 4389 | |
| 4390 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 4391 | // 'transferTouch' api. |
| 4392 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 4393 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4394 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4395 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4396 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4397 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4398 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4399 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4400 | |
| 4401 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4402 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4403 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4404 | |
| 4405 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4406 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4407 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4408 | |
| 4409 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4410 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4411 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4412 | |
| 4413 | // Update window info, let it find window handle of second display first. |
| 4414 | mDispatcher->setInputWindows( |
| 4415 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4416 | {ADISPLAY_ID_DEFAULT, |
| 4417 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4418 | |
| 4419 | // Touch on second display. |
| 4420 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4421 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 4422 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4423 | |
| 4424 | // Window should receive motion event. |
| 4425 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4426 | |
| 4427 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4428 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4429 | |
| 4430 | // The first window gets cancel. |
| 4431 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4432 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4433 | |
| 4434 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4435 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4436 | SECOND_DISPLAY_ID, {150, 50})) |
| 4437 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4438 | firstWindowInPrimary->assertNoEvents(); |
| 4439 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 4440 | |
| 4441 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4442 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 4443 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4444 | firstWindowInPrimary->assertNoEvents(); |
| 4445 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 4446 | } |
| 4447 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4448 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4449 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4450 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4451 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4452 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4453 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4454 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4455 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4456 | |
| 4457 | window->consumeFocusEvent(true); |
| 4458 | |
| 4459 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4460 | mDispatcher->notifyKey(&keyArgs); |
| 4461 | |
| 4462 | // Window should receive key down event. |
| 4463 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4464 | } |
| 4465 | |
| 4466 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4467 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4468 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4469 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4470 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4471 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4472 | |
| 4473 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4474 | mDispatcher->notifyKey(&keyArgs); |
| 4475 | mDispatcher->waitForIdle(); |
| 4476 | |
| 4477 | window->assertNoEvents(); |
| 4478 | } |
| 4479 | |
| 4480 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4481 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4482 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4483 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4484 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4485 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4486 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4487 | |
| 4488 | // Send key |
| 4489 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4490 | mDispatcher->notifyKey(&keyArgs); |
| 4491 | // Send motion |
| 4492 | NotifyMotionArgs motionArgs = |
| 4493 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4494 | ADISPLAY_ID_DEFAULT); |
| 4495 | mDispatcher->notifyMotion(&motionArgs); |
| 4496 | |
| 4497 | // Window should receive only the motion event |
| 4498 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4499 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4500 | } |
| 4501 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4502 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4503 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4504 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4505 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4506 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4507 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4508 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4509 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4510 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4511 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4512 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4513 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4514 | |
| 4515 | // Add the windows to the dispatcher |
| 4516 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4517 | |
| 4518 | PointF pointInFirst = {300, 200}; |
| 4519 | PointF pointInSecond = {300, 600}; |
| 4520 | |
| 4521 | // Send down to the first window |
| 4522 | NotifyMotionArgs firstDownMotionArgs = |
| 4523 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4524 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 4525 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 4526 | // Only the first window should get the down event |
| 4527 | firstWindow->consumeMotionDown(); |
| 4528 | secondWindow->assertNoEvents(); |
| 4529 | |
| 4530 | // Send down to the second window |
| 4531 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4532 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4533 | {pointInFirst, pointInSecond}); |
| 4534 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 4535 | // The first window gets a move and the second a down |
| 4536 | firstWindow->consumeMotionMove(); |
| 4537 | secondWindow->consumeMotionDown(); |
| 4538 | |
| 4539 | // Send pointer cancel to the second window |
| 4540 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4541 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4542 | {pointInFirst, pointInSecond}); |
| 4543 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 4544 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 4545 | // The first window gets move and the second gets cancel. |
| 4546 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4547 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4548 | |
| 4549 | // Send up event. |
| 4550 | NotifyMotionArgs upMotionArgs = |
| 4551 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4552 | ADISPLAY_ID_DEFAULT); |
| 4553 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4554 | // The first window gets up and the second gets nothing. |
| 4555 | firstWindow->consumeMotionUp(); |
| 4556 | secondWindow->assertNoEvents(); |
| 4557 | } |
| 4558 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4559 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 4560 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4561 | |
| 4562 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4563 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4564 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4565 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 4566 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 4567 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 4568 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4569 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4570 | window->assertNoEvents(); |
| 4571 | mDispatcher->waitForIdle(); |
| 4572 | } |
| 4573 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4574 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4575 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4576 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4577 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4578 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4579 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4580 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4583 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 4584 | |
| 4585 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 4586 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 4587 | expectedDisplayId, expectedFlags); |
| 4588 | } |
| 4589 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4590 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 4591 | |
| 4592 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 4593 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4594 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 4595 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 4596 | expectedDisplayId, expectedFlags); |
| 4597 | } |
| 4598 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4599 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 4600 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 4601 | expectedDisplayId, expectedFlags); |
| 4602 | } |
| 4603 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4604 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 4605 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 4606 | expectedDisplayId, expectedFlags); |
| 4607 | } |
| 4608 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4609 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4610 | mInputReceiver->consumeMotionEvent( |
| 4611 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4612 | WithDisplayId(expectedDisplayId), |
| 4613 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4616 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 4617 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 4618 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 4619 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4620 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4623 | MotionEvent* consumeMotion() { |
| 4624 | InputEvent* event = mInputReceiver->consume(); |
| 4625 | if (!event) { |
| 4626 | ADD_FAILURE() << "No event was produced"; |
| 4627 | return nullptr; |
| 4628 | } |
| 4629 | if (event->getType() != AINPUT_EVENT_TYPE_MOTION) { |
| 4630 | ADD_FAILURE() << "Received event of type " << event->getType() << " instead of motion"; |
| 4631 | return nullptr; |
| 4632 | } |
| 4633 | return static_cast<MotionEvent*>(event); |
| 4634 | } |
| 4635 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4636 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 4637 | |
| 4638 | private: |
| 4639 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4640 | }; |
| 4641 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4642 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 4643 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4644 | /** |
| 4645 | * Two entities that receive touch: A window, and a global monitor. |
| 4646 | * The touch goes to the window, and then the window disappears. |
| 4647 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 4648 | * for the monitor, as well. |
| 4649 | * 1. foregroundWindow |
| 4650 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 4651 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4652 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4653 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4654 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4655 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4656 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4657 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4658 | |
| 4659 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4661 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4662 | {100, 200})) |
| 4663 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4664 | |
| 4665 | // Both the foreground window and the global monitor should receive the touch down |
| 4666 | window->consumeMotionDown(); |
| 4667 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4668 | |
| 4669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4670 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4671 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4672 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4673 | |
| 4674 | window->consumeMotionMove(); |
| 4675 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4676 | |
| 4677 | // Now the foreground window goes away |
| 4678 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4679 | window->consumeMotionCancel(); |
| 4680 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 4681 | |
| 4682 | // If more events come in, there will be no more foreground window to send them to. This will |
| 4683 | // cause a cancel for the monitor, as well. |
| 4684 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4685 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4686 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 4687 | << "Injection should fail because the window was removed"; |
| 4688 | window->assertNoEvents(); |
| 4689 | // Global monitor now gets the cancel |
| 4690 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4691 | } |
| 4692 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4693 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4694 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4695 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4696 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4697 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4698 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4699 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4700 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4702 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4703 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4704 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4705 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4708 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 4709 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4710 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4711 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4712 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4713 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4714 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4715 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4716 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4717 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4718 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4719 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4720 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4721 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4722 | // Pilfer pointers from the monitor. |
| 4723 | // This should not do anything and the window should continue to receive events. |
| 4724 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4725 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4727 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4728 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4729 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4730 | |
| 4731 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4732 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4735 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4736 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4737 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4738 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4739 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4740 | window->setWindowOffset(20, 40); |
| 4741 | window->setWindowTransform(0, 1, -1, 0); |
| 4742 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4743 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4744 | |
| 4745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4746 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4747 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4748 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4749 | MotionEvent* event = monitor.consumeMotion(); |
| 4750 | // Even though window has transform, gesture monitor must not. |
| 4751 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 4752 | } |
| 4753 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4754 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4755 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4756 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4757 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4758 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4759 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4760 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 4761 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4764 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4765 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4766 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4767 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4768 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4769 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4770 | |
| 4771 | NotifyMotionArgs motionArgs = |
| 4772 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4773 | ADISPLAY_ID_DEFAULT); |
| 4774 | |
| 4775 | mDispatcher->notifyMotion(&motionArgs); |
| 4776 | // Window should receive motion down event. |
| 4777 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4778 | |
| 4779 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4780 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4781 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4782 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4783 | motionArgs.pointerCoords[0].getX() - 10); |
| 4784 | |
| 4785 | mDispatcher->notifyMotion(&motionArgs); |
| 4786 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4787 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4788 | } |
| 4789 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4790 | /** |
| 4791 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 4792 | * the device default right away. In the test scenario, we check both the default value, |
| 4793 | * and the action of enabling / disabling. |
| 4794 | */ |
| 4795 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4796 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4797 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4798 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4799 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4800 | |
| 4801 | // Set focused application. |
| 4802 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4803 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4804 | |
| 4805 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4806 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4807 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4808 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4809 | |
| 4810 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4811 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4812 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4813 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4814 | |
| 4815 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4816 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4817 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4818 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4819 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4820 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4821 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4822 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4823 | |
| 4824 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4825 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4826 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4827 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4828 | |
| 4829 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4830 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4831 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4832 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4833 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4834 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4835 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4836 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4837 | |
| 4838 | window->assertNoEvents(); |
| 4839 | } |
| 4840 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4841 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4842 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4843 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4844 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4845 | |
| 4846 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4847 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4848 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4849 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4850 | setFocusedWindow(window); |
| 4851 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4852 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4853 | |
| 4854 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4855 | mDispatcher->notifyKey(&keyArgs); |
| 4856 | |
| 4857 | InputEvent* event = window->consume(); |
| 4858 | ASSERT_NE(event, nullptr); |
| 4859 | |
| 4860 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4861 | ASSERT_NE(verified, nullptr); |
| 4862 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 4863 | |
| 4864 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 4865 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 4866 | ASSERT_EQ(keyArgs.source, verified->source); |
| 4867 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 4868 | |
| 4869 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 4870 | |
| 4871 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4872 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4873 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4874 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 4875 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 4876 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 4877 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 4878 | } |
| 4879 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4880 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4881 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4882 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4883 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4884 | |
| 4885 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4886 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4887 | ui::Transform transform; |
| 4888 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4889 | |
| 4890 | gui::DisplayInfo displayInfo; |
| 4891 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 4892 | displayInfo.transform = transform; |
| 4893 | |
| 4894 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4895 | |
| 4896 | NotifyMotionArgs motionArgs = |
| 4897 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4898 | ADISPLAY_ID_DEFAULT); |
| 4899 | mDispatcher->notifyMotion(&motionArgs); |
| 4900 | |
| 4901 | InputEvent* event = window->consume(); |
| 4902 | ASSERT_NE(event, nullptr); |
| 4903 | |
| 4904 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4905 | ASSERT_NE(verified, nullptr); |
| 4906 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 4907 | |
| 4908 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 4909 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 4910 | EXPECT_EQ(motionArgs.source, verified->source); |
| 4911 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 4912 | |
| 4913 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 4914 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4915 | const vec2 rawXY = |
| 4916 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 4917 | motionArgs.pointerCoords[0].getXYValue()); |
| 4918 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 4919 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4920 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4921 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4922 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4923 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 4924 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 4925 | } |
| 4926 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4927 | /** |
| 4928 | * Ensure that separate calls to sign the same data are generating the same key. |
| 4929 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 4930 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 4931 | * tests. |
| 4932 | */ |
| 4933 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 4934 | KeyEvent event = getTestKeyEvent(); |
| 4935 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4936 | |
| 4937 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 4938 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 4939 | ASSERT_EQ(hmac1, hmac2); |
| 4940 | } |
| 4941 | |
| 4942 | /** |
| 4943 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 4944 | */ |
| 4945 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 4946 | KeyEvent event = getTestKeyEvent(); |
| 4947 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4948 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 4949 | |
| 4950 | verifiedEvent.deviceId += 1; |
| 4951 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4952 | |
| 4953 | verifiedEvent.source += 1; |
| 4954 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4955 | |
| 4956 | verifiedEvent.eventTimeNanos += 1; |
| 4957 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4958 | |
| 4959 | verifiedEvent.displayId += 1; |
| 4960 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4961 | |
| 4962 | verifiedEvent.action += 1; |
| 4963 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4964 | |
| 4965 | verifiedEvent.downTimeNanos += 1; |
| 4966 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4967 | |
| 4968 | verifiedEvent.flags += 1; |
| 4969 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4970 | |
| 4971 | verifiedEvent.keyCode += 1; |
| 4972 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4973 | |
| 4974 | verifiedEvent.scanCode += 1; |
| 4975 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4976 | |
| 4977 | verifiedEvent.metaState += 1; |
| 4978 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4979 | |
| 4980 | verifiedEvent.repeatCount += 1; |
| 4981 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 4982 | } |
| 4983 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4984 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 4985 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4986 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4987 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4988 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4989 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4990 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4991 | |
| 4992 | // Top window is also focusable but is not granted focus. |
| 4993 | windowTop->setFocusable(true); |
| 4994 | windowSecond->setFocusable(true); |
| 4995 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 4996 | setFocusedWindow(windowSecond); |
| 4997 | |
| 4998 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5000 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5001 | |
| 5002 | // Focused window should receive event. |
| 5003 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5004 | windowTop->assertNoEvents(); |
| 5005 | } |
| 5006 | |
| 5007 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5008 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5009 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5010 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5011 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5012 | |
| 5013 | window->setFocusable(true); |
| 5014 | // Release channel for window is no longer valid. |
| 5015 | window->releaseChannel(); |
| 5016 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5017 | setFocusedWindow(window); |
| 5018 | |
| 5019 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5020 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5021 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5022 | |
| 5023 | // window channel is invalid, so it should not receive any input event. |
| 5024 | window->assertNoEvents(); |
| 5025 | } |
| 5026 | |
| 5027 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5028 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5029 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5030 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5031 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5032 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5033 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5034 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5035 | setFocusedWindow(window); |
| 5036 | |
| 5037 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5038 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5039 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5040 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5041 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5042 | window->assertNoEvents(); |
| 5043 | } |
| 5044 | |
| 5045 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5046 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5047 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5048 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5049 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5050 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5051 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5052 | |
| 5053 | windowTop->setFocusable(true); |
| 5054 | windowSecond->setFocusable(true); |
| 5055 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5056 | setFocusedWindow(windowTop); |
| 5057 | windowTop->consumeFocusEvent(true); |
| 5058 | |
| 5059 | setFocusedWindow(windowSecond, windowTop); |
| 5060 | windowSecond->consumeFocusEvent(true); |
| 5061 | windowTop->consumeFocusEvent(false); |
| 5062 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5063 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5064 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5065 | |
| 5066 | // Focused window should receive event. |
| 5067 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5068 | } |
| 5069 | |
| 5070 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 5071 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5072 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5073 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5074 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5075 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5076 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5077 | |
| 5078 | windowTop->setFocusable(true); |
| 5079 | windowSecond->setFocusable(true); |
| 5080 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5081 | setFocusedWindow(windowSecond, windowTop); |
| 5082 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5083 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5084 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5085 | |
| 5086 | // Event should be dropped. |
| 5087 | windowTop->assertNoEvents(); |
| 5088 | windowSecond->assertNoEvents(); |
| 5089 | } |
| 5090 | |
| 5091 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5092 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5093 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5094 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5095 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5096 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5097 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5098 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5099 | |
| 5100 | window->setFocusable(true); |
| 5101 | previousFocusedWindow->setFocusable(true); |
| 5102 | window->setVisible(false); |
| 5103 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5104 | setFocusedWindow(previousFocusedWindow); |
| 5105 | previousFocusedWindow->consumeFocusEvent(true); |
| 5106 | |
| 5107 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5108 | setFocusedWindow(window); |
| 5109 | previousFocusedWindow->consumeFocusEvent(false); |
| 5110 | |
| 5111 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5113 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5114 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5115 | |
| 5116 | // Window does not get focus event or key down. |
| 5117 | window->assertNoEvents(); |
| 5118 | |
| 5119 | // Window becomes visible. |
| 5120 | window->setVisible(true); |
| 5121 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5122 | |
| 5123 | // Window receives focus event. |
| 5124 | window->consumeFocusEvent(true); |
| 5125 | // Focused window receives key down. |
| 5126 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5127 | } |
| 5128 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5129 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5130 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5131 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5132 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5133 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5134 | |
| 5135 | // window is granted focus. |
| 5136 | window->setFocusable(true); |
| 5137 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5138 | setFocusedWindow(window); |
| 5139 | window->consumeFocusEvent(true); |
| 5140 | |
| 5141 | // When a display is removed window loses focus. |
| 5142 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5143 | window->consumeFocusEvent(false); |
| 5144 | } |
| 5145 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5146 | /** |
| 5147 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5148 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5149 | * of the 'slipperyEnterWindow'. |
| 5150 | * |
| 5151 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5152 | * a way so that the touched location is no longer covered by the top window. |
| 5153 | * |
| 5154 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5155 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5156 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5157 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5158 | * with ACTION_DOWN). |
| 5159 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5160 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5161 | * |
| 5162 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5163 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5164 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5165 | * |
| 5166 | * In this test, we ensure that the event received by the bottom window has |
| 5167 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5168 | */ |
| 5169 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5170 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 5171 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5172 | |
| 5173 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5174 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5175 | |
| 5176 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5177 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5178 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5179 | // Make sure this one overlaps the bottom window |
| 5180 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5181 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5182 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5183 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5184 | |
| 5185 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5186 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5187 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5188 | |
| 5189 | mDispatcher->setInputWindows( |
| 5190 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5191 | |
| 5192 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 5193 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5194 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 5195 | mDispatcher->notifyMotion(&args); |
| 5196 | slipperyExitWindow->consumeMotionDown(); |
| 5197 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5198 | mDispatcher->setInputWindows( |
| 5199 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5200 | |
| 5201 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5202 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 5203 | mDispatcher->notifyMotion(&args); |
| 5204 | |
| 5205 | slipperyExitWindow->consumeMotionCancel(); |
| 5206 | |
| 5207 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5208 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5209 | } |
| 5210 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5211 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5212 | protected: |
| 5213 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5214 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5215 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5216 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5217 | sp<FakeWindowHandle> mWindow; |
| 5218 | |
| 5219 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5220 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5221 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5222 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5223 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 5224 | ASSERT_EQ(OK, mDispatcher->start()); |
| 5225 | |
| 5226 | setUpWindow(); |
| 5227 | } |
| 5228 | |
| 5229 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5230 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5231 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5232 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5233 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5234 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5235 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5236 | mWindow->consumeFocusEvent(true); |
| 5237 | } |
| 5238 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5239 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5240 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5241 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5242 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 5243 | mDispatcher->notifyKey(&keyArgs); |
| 5244 | |
| 5245 | // Window should receive key down event. |
| 5246 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5247 | } |
| 5248 | |
| 5249 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 5250 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 5251 | InputEvent* repeatEvent = mWindow->consume(); |
| 5252 | ASSERT_NE(nullptr, repeatEvent); |
| 5253 | |
| 5254 | uint32_t eventType = repeatEvent->getType(); |
| 5255 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 5256 | |
| 5257 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 5258 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 5259 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 5260 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 5261 | } |
| 5262 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5263 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5264 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5265 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5266 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 5267 | mDispatcher->notifyKey(&keyArgs); |
| 5268 | |
| 5269 | // Window should receive key down event. |
| 5270 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5271 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5272 | } |
| 5273 | }; |
| 5274 | |
| 5275 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5276 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5277 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5278 | expectKeyRepeatOnce(repeatCount); |
| 5279 | } |
| 5280 | } |
| 5281 | |
| 5282 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5283 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5284 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5285 | expectKeyRepeatOnce(repeatCount); |
| 5286 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5287 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5288 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5289 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5290 | expectKeyRepeatOnce(repeatCount); |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5295 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5296 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5297 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5298 | mWindow->assertNoEvents(); |
| 5299 | } |
| 5300 | |
| 5301 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5302 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5303 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5304 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5305 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5306 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5307 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5308 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5309 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 5310 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5311 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5312 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5313 | mWindow->assertNoEvents(); |
| 5314 | } |
| 5315 | |
| 5316 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5317 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5318 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5319 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5320 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5321 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5322 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5323 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5324 | mWindow->assertNoEvents(); |
| 5325 | } |
| 5326 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5327 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 5328 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5329 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5330 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5331 | mDispatcher->notifyDeviceReset(&args); |
| 5332 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 5333 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 5334 | mWindow->assertNoEvents(); |
| 5335 | } |
| 5336 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5337 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5338 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5339 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5340 | InputEvent* repeatEvent = mWindow->consume(); |
| 5341 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5342 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 5343 | IdGenerator::getSource(repeatEvent->getId())); |
| 5344 | } |
| 5345 | } |
| 5346 | |
| 5347 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5348 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5349 | |
| 5350 | std::unordered_set<int32_t> idSet; |
| 5351 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5352 | InputEvent* repeatEvent = mWindow->consume(); |
| 5353 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5354 | int32_t id = repeatEvent->getId(); |
| 5355 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 5356 | idSet.insert(id); |
| 5357 | } |
| 5358 | } |
| 5359 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5360 | /* Test InputDispatcher for MultiDisplay */ |
| 5361 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 5362 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5363 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5364 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5365 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5366 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5367 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5368 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5369 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5370 | // Set focus window for primary display, but focused display would be second one. |
| 5371 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5372 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5373 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5374 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5375 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5376 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5377 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5378 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5379 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5380 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5381 | // Set focus display to second one. |
| 5382 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 5383 | // Set focus window for second display. |
| 5384 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5385 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5386 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5387 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5388 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5389 | } |
| 5390 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5391 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5392 | InputDispatcherTest::TearDown(); |
| 5393 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5394 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5395 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5396 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5397 | windowInSecondary.clear(); |
| 5398 | } |
| 5399 | |
| 5400 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5401 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5402 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5403 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5404 | sp<FakeWindowHandle> windowInSecondary; |
| 5405 | }; |
| 5406 | |
| 5407 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 5408 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5409 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5410 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5411 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5412 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5413 | windowInSecondary->assertNoEvents(); |
| 5414 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5415 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5416 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5417 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5418 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5419 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5420 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5421 | } |
| 5422 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5423 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5424 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5425 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5426 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5427 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5428 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5429 | windowInSecondary->assertNoEvents(); |
| 5430 | |
| 5431 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5432 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5433 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5434 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5435 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5436 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5437 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5438 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5439 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5440 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5441 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 5442 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5443 | |
| 5444 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5445 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5446 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5447 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5448 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5449 | windowInSecondary->assertNoEvents(); |
| 5450 | } |
| 5451 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5452 | // Test per-display input monitors for motion event. |
| 5453 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5454 | FakeMonitorReceiver monitorInPrimary = |
| 5455 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5456 | FakeMonitorReceiver monitorInSecondary = |
| 5457 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5458 | |
| 5459 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5461 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5462 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5463 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5464 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5465 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5466 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5467 | |
| 5468 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5469 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5470 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5471 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5472 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5473 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5474 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5475 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5476 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame^] | 5477 | // Lift up the touch from the second display |
| 5478 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5479 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5480 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5481 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5482 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 5483 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5484 | // Test inject a non-pointer motion event. |
| 5485 | // If specific a display, it will dispatch to the focused window of particular display, |
| 5486 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5488 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 5489 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5490 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5491 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5492 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5493 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | // Test per-display input monitors for key event. |
| 5497 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5498 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5499 | FakeMonitorReceiver monitorInPrimary = |
| 5500 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5501 | FakeMonitorReceiver monitorInSecondary = |
| 5502 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5503 | |
| 5504 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5505 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5506 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5507 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5508 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5509 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5510 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5511 | } |
| 5512 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5513 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 5514 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5515 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5516 | secondWindowInPrimary->setFocusable(true); |
| 5517 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 5518 | setFocusedWindow(secondWindowInPrimary); |
| 5519 | windowInPrimary->consumeFocusEvent(false); |
| 5520 | secondWindowInPrimary->consumeFocusEvent(true); |
| 5521 | |
| 5522 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5523 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 5524 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5525 | windowInPrimary->assertNoEvents(); |
| 5526 | windowInSecondary->assertNoEvents(); |
| 5527 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5528 | } |
| 5529 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 5530 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 5531 | FakeMonitorReceiver monitorInPrimary = |
| 5532 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5533 | FakeMonitorReceiver monitorInSecondary = |
| 5534 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 5535 | |
| 5536 | // Test touch down on primary display. |
| 5537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5538 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5539 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5540 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5541 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5542 | |
| 5543 | // Test touch down on second display. |
| 5544 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5545 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5546 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5547 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5548 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 5549 | |
| 5550 | // Trigger cancel touch. |
| 5551 | mDispatcher->cancelCurrentTouch(); |
| 5552 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5553 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5554 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5555 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5556 | |
| 5557 | // Test inject a move motion event, no window/monitor should receive the event. |
| 5558 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5559 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5560 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5561 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5562 | windowInPrimary->assertNoEvents(); |
| 5563 | monitorInPrimary.assertNoEvents(); |
| 5564 | |
| 5565 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5566 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5567 | SECOND_DISPLAY_ID, {110, 200})) |
| 5568 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5569 | windowInSecondary->assertNoEvents(); |
| 5570 | monitorInSecondary.assertNoEvents(); |
| 5571 | } |
| 5572 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5573 | class InputFilterTest : public InputDispatcherTest { |
| 5574 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5575 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 5576 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5577 | NotifyMotionArgs motionArgs; |
| 5578 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5579 | motionArgs = |
| 5580 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5581 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5582 | motionArgs = |
| 5583 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5584 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5585 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5586 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5587 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 5588 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5589 | } else { |
| 5590 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | void testNotifyKey(bool expectToBeFiltered) { |
| 5595 | NotifyKeyArgs keyArgs; |
| 5596 | |
| 5597 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5598 | mDispatcher->notifyKey(&keyArgs); |
| 5599 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 5600 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5601 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5602 | |
| 5603 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 5604 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5605 | } else { |
| 5606 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5607 | } |
| 5608 | } |
| 5609 | }; |
| 5610 | |
| 5611 | // Test InputFilter for MotionEvent |
| 5612 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 5613 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 5614 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5615 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5616 | |
| 5617 | // Enable InputFilter |
| 5618 | mDispatcher->setInputFilterEnabled(true); |
| 5619 | // Test touch on both primary and second display, and check if both events are filtered. |
| 5620 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 5621 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 5622 | |
| 5623 | // Disable InputFilter |
| 5624 | mDispatcher->setInputFilterEnabled(false); |
| 5625 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 5626 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5627 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5628 | } |
| 5629 | |
| 5630 | // Test InputFilter for KeyEvent |
| 5631 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 5632 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 5633 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5634 | |
| 5635 | // Enable InputFilter |
| 5636 | mDispatcher->setInputFilterEnabled(true); |
| 5637 | // Send a key event, and check if it is filtered. |
| 5638 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 5639 | |
| 5640 | // Disable InputFilter |
| 5641 | mDispatcher->setInputFilterEnabled(false); |
| 5642 | // Send a key event, and check if it isn't filtered. |
| 5643 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5644 | } |
| 5645 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5646 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 5647 | // logical display coordinate space. |
| 5648 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 5649 | ui::Transform firstDisplayTransform; |
| 5650 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5651 | ui::Transform secondDisplayTransform; |
| 5652 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 5653 | |
| 5654 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 5655 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 5656 | displayInfos[0].transform = firstDisplayTransform; |
| 5657 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 5658 | displayInfos[1].transform = secondDisplayTransform; |
| 5659 | |
| 5660 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 5661 | |
| 5662 | // Enable InputFilter |
| 5663 | mDispatcher->setInputFilterEnabled(true); |
| 5664 | |
| 5665 | // Ensure the correct transforms are used for the displays. |
| 5666 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 5667 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 5668 | } |
| 5669 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5670 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 5671 | protected: |
| 5672 | virtual void SetUp() override { |
| 5673 | InputDispatcherTest::SetUp(); |
| 5674 | |
| 5675 | /** |
| 5676 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 5677 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 5678 | * on. |
| 5679 | */ |
| 5680 | mDispatcher->setInputFilterEnabled(true); |
| 5681 | |
| 5682 | std::shared_ptr<InputApplicationHandle> application = |
| 5683 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5684 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 5685 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5686 | |
| 5687 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5688 | mWindow->setFocusable(true); |
| 5689 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5690 | setFocusedWindow(mWindow); |
| 5691 | mWindow->consumeFocusEvent(true); |
| 5692 | } |
| 5693 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5694 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5695 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5696 | KeyEvent event; |
| 5697 | |
| 5698 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5699 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 5700 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5701 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5702 | const int32_t additionalPolicyFlags = |
| 5703 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 5704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5705 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5706 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5707 | policyFlags | additionalPolicyFlags)); |
| 5708 | |
| 5709 | InputEvent* received = mWindow->consume(); |
| 5710 | ASSERT_NE(nullptr, received); |
| 5711 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5712 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_KEY); |
| 5713 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 5714 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 5715 | } |
| 5716 | |
| 5717 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5718 | int32_t flags) { |
| 5719 | MotionEvent event; |
| 5720 | PointerProperties pointerProperties[1]; |
| 5721 | PointerCoords pointerCoords[1]; |
| 5722 | pointerProperties[0].clear(); |
| 5723 | pointerProperties[0].id = 0; |
| 5724 | pointerCoords[0].clear(); |
| 5725 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 5726 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 5727 | |
| 5728 | ui::Transform identityTransform; |
| 5729 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5730 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 5731 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 5732 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 5733 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 5734 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 5735 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5736 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 5737 | |
| 5738 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 5739 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5740 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5741 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5742 | policyFlags | additionalPolicyFlags)); |
| 5743 | |
| 5744 | InputEvent* received = mWindow->consume(); |
| 5745 | ASSERT_NE(nullptr, received); |
| 5746 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
| 5747 | ASSERT_EQ(received->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 5748 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 5749 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5750 | } |
| 5751 | |
| 5752 | private: |
| 5753 | sp<FakeWindowHandle> mWindow; |
| 5754 | }; |
| 5755 | |
| 5756 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5757 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 5758 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 5759 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5760 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 5761 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5764 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5765 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5766 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5767 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 5768 | } |
| 5769 | |
| 5770 | TEST_F(InputFilterInjectionPolicyTest, |
| 5771 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 5772 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5773 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5774 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5778 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 5779 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5782 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5783 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5784 | InputDispatcherTest::SetUp(); |
| 5785 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5786 | std::shared_ptr<FakeApplicationHandle> application = |
| 5787 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5788 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5789 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5790 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5791 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5792 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5793 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5794 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5795 | |
| 5796 | // Set focused application. |
| 5797 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5798 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5799 | |
| 5800 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5801 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5802 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5803 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5804 | } |
| 5805 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5806 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5807 | InputDispatcherTest::TearDown(); |
| 5808 | |
| 5809 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5810 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5811 | } |
| 5812 | |
| 5813 | protected: |
| 5814 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5815 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5816 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5817 | }; |
| 5818 | |
| 5819 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5820 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 5821 | // the onPointerDownOutsideFocus callback. |
| 5822 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5823 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5824 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5825 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5826 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5827 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5828 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5829 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5830 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 5831 | } |
| 5832 | |
| 5833 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 5834 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 5835 | // onPointerDownOutsideFocus callback. |
| 5836 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5837 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5838 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5839 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5840 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5841 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5842 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5843 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5844 | } |
| 5845 | |
| 5846 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 5847 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 5848 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5850 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5851 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5852 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5853 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5854 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5855 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5859 | // DOWN on the window that already has focus. Ensure no window received the |
| 5860 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5861 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5862 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5863 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5864 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5865 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5866 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5867 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5868 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5869 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5870 | } |
| 5871 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 5872 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 5873 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 5874 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 5875 | const MotionEvent event = |
| 5876 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5877 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5878 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 5879 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 5880 | .build(); |
| 5881 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 5882 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5883 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 5884 | |
| 5885 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5886 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 5887 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 5888 | mUnfocusedWindow->assertNoEvents(); |
| 5889 | } |
| 5890 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5891 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 5892 | // windows that point to the same client token. |
| 5893 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 5894 | virtual void SetUp() override { |
| 5895 | InputDispatcherTest::SetUp(); |
| 5896 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5897 | std::shared_ptr<FakeApplicationHandle> application = |
| 5898 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5899 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 5900 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5901 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 5902 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5903 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 5904 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5905 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 5906 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5907 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5908 | } |
| 5909 | |
| 5910 | protected: |
| 5911 | sp<FakeWindowHandle> mWindow1; |
| 5912 | sp<FakeWindowHandle> mWindow2; |
| 5913 | |
| 5914 | // 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] | 5915 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5916 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 5917 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5918 | } |
| 5919 | |
| 5920 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 5921 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5922 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5923 | InputEvent* event = window->consume(); |
| 5924 | |
| 5925 | ASSERT_NE(nullptr, event) << name.c_str() |
| 5926 | << ": consumer should have returned non-NULL event."; |
| 5927 | |
| 5928 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 5929 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 5930 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 5931 | |
| 5932 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5933 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 5934 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5935 | |
| 5936 | for (size_t i = 0; i < points.size(); i++) { |
| 5937 | float expectedX = points[i].x; |
| 5938 | float expectedY = points[i].y; |
| 5939 | |
| 5940 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 5941 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 5942 | << ", got " << motionEvent.getX(i); |
| 5943 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 5944 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 5945 | << ", got " << motionEvent.getY(i); |
| 5946 | } |
| 5947 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5948 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5949 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5950 | std::vector<PointF> expectedPoints) { |
| 5951 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 5952 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 5953 | mDispatcher->notifyMotion(&motionArgs); |
| 5954 | |
| 5955 | // Always consume from window1 since it's the window that has the InputReceiver |
| 5956 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 5957 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5958 | }; |
| 5959 | |
| 5960 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 5961 | // Touch Window 1 |
| 5962 | PointF touchedPoint = {10, 10}; |
| 5963 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5964 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5965 | |
| 5966 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5967 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5968 | |
| 5969 | // Touch Window 2 |
| 5970 | touchedPoint = {150, 150}; |
| 5971 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5972 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5973 | } |
| 5974 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5975 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 5976 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5977 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 5978 | |
| 5979 | // Touch Window 1 |
| 5980 | PointF touchedPoint = {10, 10}; |
| 5981 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5982 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5983 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5984 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5985 | |
| 5986 | // Touch Window 2 |
| 5987 | touchedPoint = {150, 150}; |
| 5988 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5989 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 5990 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5991 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5992 | // Update the transform so rotation is set |
| 5993 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 5994 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 5995 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5996 | } |
| 5997 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 5998 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 5999 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6000 | |
| 6001 | // Touch Window 1 |
| 6002 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6003 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6004 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6005 | |
| 6006 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6007 | touchedPoints.push_back(PointF{150, 150}); |
| 6008 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6009 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6010 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6011 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6012 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6013 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6014 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6015 | // Update the transform so rotation is set for Window 2 |
| 6016 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6017 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6018 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6019 | } |
| 6020 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6021 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6022 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6023 | |
| 6024 | // Touch Window 1 |
| 6025 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6026 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6027 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6028 | |
| 6029 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6030 | touchedPoints.push_back(PointF{150, 150}); |
| 6031 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6032 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6033 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6034 | |
| 6035 | // Move both windows |
| 6036 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6037 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6038 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6039 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6040 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6041 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6042 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6043 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6044 | expectedPoints.pop_back(); |
| 6045 | |
| 6046 | // Touch Window 2 |
| 6047 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6048 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6049 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6050 | |
| 6051 | // Move both windows |
| 6052 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6053 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6054 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6055 | |
| 6056 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
| 6059 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 6060 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 6061 | |
| 6062 | // Touch Window 1 |
| 6063 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6064 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6065 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6066 | |
| 6067 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6068 | touchedPoints.push_back(PointF{150, 150}); |
| 6069 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6070 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6071 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6072 | |
| 6073 | // Move both windows |
| 6074 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6075 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6076 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6077 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6078 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6079 | } |
| 6080 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6081 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 6082 | virtual void SetUp() override { |
| 6083 | InputDispatcherTest::SetUp(); |
| 6084 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6085 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6086 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6087 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 6088 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6089 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6090 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6091 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6092 | |
| 6093 | // Set focused application. |
| 6094 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 6095 | |
| 6096 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6097 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6098 | mWindow->consumeFocusEvent(true); |
| 6099 | } |
| 6100 | |
| 6101 | virtual void TearDown() override { |
| 6102 | InputDispatcherTest::TearDown(); |
| 6103 | mWindow.clear(); |
| 6104 | } |
| 6105 | |
| 6106 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6107 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6108 | sp<FakeWindowHandle> mWindow; |
| 6109 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 6110 | |
| 6111 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6113 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6114 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6115 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6116 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6117 | WINDOW_LOCATION)); |
| 6118 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6119 | |
| 6120 | sp<FakeWindowHandle> addSpyWindow() { |
| 6121 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6122 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6123 | spy->setTrustedOverlay(true); |
| 6124 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6125 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6126 | spy->setDispatchingTimeout(30ms); |
| 6127 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 6128 | return spy; |
| 6129 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6130 | }; |
| 6131 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6132 | // Send a tap and respond, which should not cause an ANR. |
| 6133 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 6134 | tapOnWindow(); |
| 6135 | mWindow->consumeMotionDown(); |
| 6136 | mWindow->consumeMotionUp(); |
| 6137 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6138 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6139 | } |
| 6140 | |
| 6141 | // Send a regular key and respond, which should not cause an ANR. |
| 6142 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6143 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6144 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6145 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6146 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6147 | } |
| 6148 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6149 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 6150 | mWindow->setFocusable(false); |
| 6151 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6152 | mWindow->consumeFocusEvent(false); |
| 6153 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6154 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6155 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6156 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6157 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6158 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6159 | // Key will not go to window because we have no focused window. |
| 6160 | // The 'no focused window' ANR timer should start instead. |
| 6161 | |
| 6162 | // Now, the focused application goes away. |
| 6163 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 6164 | // The key should get dropped and there should be no ANR. |
| 6165 | |
| 6166 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6167 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6168 | } |
| 6169 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6170 | // 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] | 6171 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 6172 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6173 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6175 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6176 | WINDOW_LOCATION)); |
| 6177 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6178 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 6179 | ASSERT_TRUE(sequenceNum); |
| 6180 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6181 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6182 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6183 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6184 | mWindow->consumeMotionEvent( |
| 6185 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6186 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6187 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6188 | } |
| 6189 | |
| 6190 | // Send a key to the app and have the app not respond right away. |
| 6191 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 6192 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6194 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 6195 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6196 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6197 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6198 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6199 | } |
| 6200 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6201 | // We have a focused application, but no focused window |
| 6202 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6203 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6204 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6205 | mWindow->consumeFocusEvent(false); |
| 6206 | |
| 6207 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6208 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6209 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6210 | WINDOW_LOCATION)); |
| 6211 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 6212 | mDispatcher->waitForIdle(); |
| 6213 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6214 | |
| 6215 | // Once a focused event arrives, we get an ANR for this application |
| 6216 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6217 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6218 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6219 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6220 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6221 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6222 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6223 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6224 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6225 | } |
| 6226 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6227 | /** |
| 6228 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 6229 | * there will not be an ANR. |
| 6230 | */ |
| 6231 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 6232 | mWindow->setFocusable(false); |
| 6233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6234 | mWindow->consumeFocusEvent(false); |
| 6235 | |
| 6236 | KeyEvent event; |
| 6237 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 6238 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 6239 | |
| 6240 | // Define a valid key down event that is stale (too old). |
| 6241 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 6242 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6243 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6244 | |
| 6245 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 6246 | |
| 6247 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6248 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6249 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 6250 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 6251 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 6252 | << "Injection should fail because the event is stale"; |
| 6253 | |
| 6254 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6255 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6256 | mWindow->assertNoEvents(); |
| 6257 | } |
| 6258 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6259 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6260 | // Make sure that we don't notify policy twice about the same ANR. |
| 6261 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6262 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6263 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6264 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6265 | |
| 6266 | // Once a focused event arrives, we get an ANR for this application |
| 6267 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6268 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6269 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6270 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6271 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6272 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6273 | const std::chrono::duration appTimeout = |
| 6274 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6275 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6276 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6277 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6278 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 6279 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6280 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6281 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 6282 | // '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] | 6283 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6284 | } |
| 6285 | |
| 6286 | // We have a focused application, but no focused window |
| 6287 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6288 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6289 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6290 | mWindow->consumeFocusEvent(false); |
| 6291 | |
| 6292 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6293 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6294 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6295 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6296 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6297 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6298 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6299 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6300 | |
| 6301 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6302 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6303 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6304 | mWindow->assertNoEvents(); |
| 6305 | } |
| 6306 | |
| 6307 | /** |
| 6308 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 6309 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 6310 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 6311 | * the ANR mechanism should still work. |
| 6312 | * |
| 6313 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 6314 | * DOWN event, while not responding on the second one. |
| 6315 | */ |
| 6316 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 6317 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6318 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6319 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6320 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6321 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6322 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6323 | |
| 6324 | // Now send ACTION_UP, with identical timestamp |
| 6325 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6326 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6327 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6328 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6329 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6330 | |
| 6331 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 6332 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6333 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6334 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6335 | } |
| 6336 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6337 | // A spy window can receive an ANR |
| 6338 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 6339 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 6340 | |
| 6341 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6342 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6343 | WINDOW_LOCATION)); |
| 6344 | mWindow->consumeMotionDown(); |
| 6345 | |
| 6346 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 6347 | ASSERT_TRUE(sequenceNum); |
| 6348 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6349 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6350 | |
| 6351 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6352 | spy->consumeMotionEvent( |
| 6353 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6354 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6355 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6356 | } |
| 6357 | |
| 6358 | // 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] | 6359 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6360 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 6361 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6362 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6363 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6364 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6365 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6367 | |
| 6368 | // Stuck on the ACTION_UP |
| 6369 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6370 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6371 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6372 | // 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] | 6373 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6374 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6375 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6376 | |
| 6377 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6378 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6379 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6380 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6381 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6382 | } |
| 6383 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6384 | // 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] | 6385 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6386 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 6387 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6388 | |
| 6389 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6390 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6391 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6392 | |
| 6393 | mWindow->consumeMotionDown(); |
| 6394 | // Stuck on the ACTION_UP |
| 6395 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6396 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6397 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6398 | // 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] | 6399 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6400 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6401 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6402 | |
| 6403 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6404 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6405 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6406 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6407 | spy->assertNoEvents(); |
| 6408 | } |
| 6409 | |
| 6410 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 6411 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 6412 | |
| 6413 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6414 | |
| 6415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6416 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6417 | WINDOW_LOCATION)); |
| 6418 | |
| 6419 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6420 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 6421 | ASSERT_TRUE(consumeSeq); |
| 6422 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6423 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6424 | |
| 6425 | monitor.finishEvent(*consumeSeq); |
| 6426 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6427 | |
| 6428 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6429 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 6433 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 6434 | // get an ANR again. |
| 6435 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 6436 | // 2. consume all pending events (= queue becomes healthy again) |
| 6437 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 6438 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 6439 | tapOnWindow(); |
| 6440 | |
| 6441 | mWindow->consumeMotionDown(); |
| 6442 | // Block on ACTION_UP |
| 6443 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6444 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6445 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 6446 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6447 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6448 | mWindow->assertNoEvents(); |
| 6449 | |
| 6450 | tapOnWindow(); |
| 6451 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6452 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6453 | mWindow->consumeMotionUp(); |
| 6454 | |
| 6455 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6456 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6457 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6458 | mWindow->assertNoEvents(); |
| 6459 | } |
| 6460 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6461 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 6462 | // it. |
| 6463 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6464 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6465 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6466 | WINDOW_LOCATION)); |
| 6467 | |
| 6468 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6469 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6470 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6471 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 6472 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6473 | // 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] | 6474 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6475 | mWindow->consumeMotionEvent( |
| 6476 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6477 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6478 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6479 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6480 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6481 | } |
| 6482 | |
| 6483 | /** |
| 6484 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6485 | * not to to the focused window until the motion is processed. |
| 6486 | * |
| 6487 | * Warning!!! |
| 6488 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6489 | * and the injection timeout that we specify when injecting the key. |
| 6490 | * We must have the injection timeout (10ms) be smaller than |
| 6491 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6492 | * |
| 6493 | * If that value changes, this test should also change. |
| 6494 | */ |
| 6495 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 6496 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6497 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6498 | |
| 6499 | tapOnWindow(); |
| 6500 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6501 | ASSERT_TRUE(downSequenceNum); |
| 6502 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6503 | ASSERT_TRUE(upSequenceNum); |
| 6504 | // Don't finish the events yet, and send a key |
| 6505 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 6506 | // window even if motions are still being processed. But because the injection timeout is short, |
| 6507 | // we will receive INJECTION_TIMED_OUT as the result. |
| 6508 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6509 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6510 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6511 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6512 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6513 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6514 | // and the key remains pending, waiting for the touch events to be processed |
| 6515 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6516 | ASSERT_FALSE(keySequenceNum); |
| 6517 | |
| 6518 | std::this_thread::sleep_for(500ms); |
| 6519 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 6520 | // to the focused window, even though we have not yet finished the motion event |
| 6521 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6522 | mWindow->finishEvent(*downSequenceNum); |
| 6523 | mWindow->finishEvent(*upSequenceNum); |
| 6524 | } |
| 6525 | |
| 6526 | /** |
| 6527 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6528 | * not go to the focused window until the motion is processed. |
| 6529 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 6530 | * focused window right away. |
| 6531 | */ |
| 6532 | TEST_F(InputDispatcherSingleWindowAnr, |
| 6533 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 6534 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6536 | |
| 6537 | tapOnWindow(); |
| 6538 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6539 | ASSERT_TRUE(downSequenceNum); |
| 6540 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6541 | ASSERT_TRUE(upSequenceNum); |
| 6542 | // Don't finish the events yet, and send a key |
| 6543 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6544 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6545 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6546 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6547 | // At this point, key is still pending, and should not be sent to the application yet. |
| 6548 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6549 | ASSERT_FALSE(keySequenceNum); |
| 6550 | |
| 6551 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 6552 | tapOnWindow(); |
| 6553 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 6554 | // the other events yet. We can finish events in any order. |
| 6555 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 6556 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 6557 | mWindow->consumeMotionDown(); |
| 6558 | mWindow->consumeMotionUp(); |
| 6559 | mWindow->assertNoEvents(); |
| 6560 | } |
| 6561 | |
| 6562 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 6563 | virtual void SetUp() override { |
| 6564 | InputDispatcherTest::SetUp(); |
| 6565 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6566 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6567 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6568 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 6569 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6570 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6571 | // 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] | 6572 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6573 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6574 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 6575 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6576 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6577 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6578 | |
| 6579 | // Set focused application. |
| 6580 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6581 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6582 | |
| 6583 | // Expect one focus window exist in display. |
| 6584 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6585 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6586 | mFocusedWindow->consumeFocusEvent(true); |
| 6587 | } |
| 6588 | |
| 6589 | virtual void TearDown() override { |
| 6590 | InputDispatcherTest::TearDown(); |
| 6591 | |
| 6592 | mUnfocusedWindow.clear(); |
| 6593 | mFocusedWindow.clear(); |
| 6594 | } |
| 6595 | |
| 6596 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6597 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6598 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 6599 | sp<FakeWindowHandle> mFocusedWindow; |
| 6600 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 6601 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 6602 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 6603 | |
| 6604 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 6605 | |
| 6606 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 6607 | |
| 6608 | private: |
| 6609 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6610 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6611 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6612 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6613 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6614 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6615 | location)); |
| 6616 | } |
| 6617 | }; |
| 6618 | |
| 6619 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 6620 | // should be ANR'd first. |
| 6621 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6623 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6624 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6625 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6626 | mFocusedWindow->consumeMotionDown(); |
| 6627 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6628 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6629 | // We consumed all events, so no ANR |
| 6630 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6631 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6632 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6634 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6635 | FOCUSED_WINDOW_LOCATION)); |
| 6636 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6637 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6638 | |
| 6639 | const std::chrono::duration timeout = |
| 6640 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6641 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6642 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 6643 | // sequence to make it consistent |
| 6644 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6645 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6646 | mFocusedWindow->consumeMotionDown(); |
| 6647 | // This cancel is generated because the connection was unresponsive |
| 6648 | mFocusedWindow->consumeMotionCancel(); |
| 6649 | mFocusedWindow->assertNoEvents(); |
| 6650 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6651 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6652 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6653 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6654 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 6658 | // it doesn't matter which order they should have ANR. |
| 6659 | // But we should receive ANR for both. |
| 6660 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 6661 | // Set the timeout for unfocused window to match the focused window |
| 6662 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 6663 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6664 | |
| 6665 | tapOnFocusedWindow(); |
| 6666 | // 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] | 6667 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 6668 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 6669 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6670 | |
| 6671 | // 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] | 6672 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 6673 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 6674 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 6675 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6676 | |
| 6677 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6678 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6679 | |
| 6680 | mFocusedWindow->consumeMotionDown(); |
| 6681 | mFocusedWindow->consumeMotionUp(); |
| 6682 | mUnfocusedWindow->consumeMotionOutside(); |
| 6683 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6684 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 6685 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 6686 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6687 | |
| 6688 | // Both applications should be marked as responsive, in any order |
| 6689 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 6690 | mFocusedWindow->getToken() == responsiveToken2); |
| 6691 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 6692 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 6693 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6694 | } |
| 6695 | |
| 6696 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 6697 | // We should also log an error to account for the dropped event (not tested here). |
| 6698 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 6699 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 6700 | tapOnFocusedWindow(); |
| 6701 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6702 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6703 | // Receive the events, but don't respond |
| 6704 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 6705 | ASSERT_TRUE(downEventSequenceNum); |
| 6706 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 6707 | ASSERT_TRUE(upEventSequenceNum); |
| 6708 | const std::chrono::duration timeout = |
| 6709 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6710 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6711 | |
| 6712 | // Tap once again |
| 6713 | // 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] | 6714 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6715 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6716 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6717 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6718 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6719 | FOCUSED_WINDOW_LOCATION)); |
| 6720 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 6721 | // valid touch target |
| 6722 | mUnfocusedWindow->assertNoEvents(); |
| 6723 | |
| 6724 | // Consume the first tap |
| 6725 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 6726 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 6727 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6728 | // The second tap did not go to the focused window |
| 6729 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6730 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6731 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6732 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6733 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6734 | } |
| 6735 | |
| 6736 | // If you tap outside of all windows, there will not be ANR |
| 6737 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6738 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6739 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6740 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 6741 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6742 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6743 | } |
| 6744 | |
| 6745 | // Since the focused window is paused, tapping on it should not produce any events |
| 6746 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 6747 | mFocusedWindow->setPaused(true); |
| 6748 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6749 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6750 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6751 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6752 | FOCUSED_WINDOW_LOCATION)); |
| 6753 | |
| 6754 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 6755 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6756 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 6757 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6758 | |
| 6759 | mFocusedWindow->assertNoEvents(); |
| 6760 | mUnfocusedWindow->assertNoEvents(); |
| 6761 | } |
| 6762 | |
| 6763 | /** |
| 6764 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6765 | * not to to the focused window until the motion is processed. |
| 6766 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 6767 | * |
| 6768 | * Warning!!! |
| 6769 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6770 | * and the injection timeout that we specify when injecting the key. |
| 6771 | * We must have the injection timeout (10ms) be smaller than |
| 6772 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6773 | * |
| 6774 | * If that value changes, this test should also change. |
| 6775 | */ |
| 6776 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 6777 | // Set a long ANR timeout to prevent it from triggering |
| 6778 | mFocusedWindow->setDispatchingTimeout(2s); |
| 6779 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6780 | |
| 6781 | tapOnUnfocusedWindow(); |
| 6782 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6783 | ASSERT_TRUE(downSequenceNum); |
| 6784 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6785 | ASSERT_TRUE(upSequenceNum); |
| 6786 | // Don't finish the events yet, and send a key |
| 6787 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 6788 | // window even if motions are still being processed. |
| 6789 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6790 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6791 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6792 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6794 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6795 | // and the key remains pending, waiting for the touch events to be processed |
| 6796 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 6797 | ASSERT_FALSE(keySequenceNum); |
| 6798 | |
| 6799 | // 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] | 6800 | mFocusedWindow->setFocusable(false); |
| 6801 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6802 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6803 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6804 | |
| 6805 | // Focus events should precede the key events |
| 6806 | mUnfocusedWindow->consumeFocusEvent(true); |
| 6807 | mFocusedWindow->consumeFocusEvent(false); |
| 6808 | |
| 6809 | // Finish the tap events, which should unblock dispatcher |
| 6810 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 6811 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 6812 | |
| 6813 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 6814 | // can finally go to the newly focused "mUnfocusedWindow". |
| 6815 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6816 | mFocusedWindow->assertNoEvents(); |
| 6817 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6818 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6819 | } |
| 6820 | |
| 6821 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 6822 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 6823 | // The other window should not be affected by that. |
| 6824 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 6825 | // Touch Window 1 |
| 6826 | NotifyMotionArgs motionArgs = |
| 6827 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6828 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 6829 | mDispatcher->notifyMotion(&motionArgs); |
| 6830 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6831 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6832 | |
| 6833 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6834 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6835 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6836 | mDispatcher->notifyMotion(&motionArgs); |
| 6837 | |
| 6838 | const std::chrono::duration timeout = |
| 6839 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6840 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6841 | |
| 6842 | mUnfocusedWindow->consumeMotionDown(); |
| 6843 | mFocusedWindow->consumeMotionDown(); |
| 6844 | // Focused window may or may not receive ACTION_MOVE |
| 6845 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 6846 | InputEvent* event; |
| 6847 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 6848 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 6849 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 6850 | ASSERT_NE(nullptr, event); |
| 6851 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 6852 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 6853 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 6854 | mFocusedWindow->consumeMotionCancel(); |
| 6855 | } else { |
| 6856 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 6857 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6858 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6859 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6860 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6861 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6862 | mUnfocusedWindow->assertNoEvents(); |
| 6863 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6864 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6865 | } |
| 6866 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6867 | /** |
| 6868 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 6869 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 6870 | * |
| 6871 | * If the user touches another application during this time, the key should be dropped. |
| 6872 | * Next, if a new focused window comes in, without toggling the focused application, |
| 6873 | * then no ANR should occur. |
| 6874 | * |
| 6875 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 6876 | * but in some cases the policy may not update the focused application. |
| 6877 | */ |
| 6878 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 6879 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 6880 | std::make_shared<FakeApplicationHandle>(); |
| 6881 | focusedApplication->setDispatchingTimeout(60ms); |
| 6882 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 6883 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 6884 | mFocusedWindow->setFocusable(false); |
| 6885 | |
| 6886 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6887 | mFocusedWindow->consumeFocusEvent(false); |
| 6888 | |
| 6889 | // Send a key. The ANR timer should start because there is no focused window. |
| 6890 | // 'focusedApplication' will get blamed if this timer completes. |
| 6891 | // 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] | 6892 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6893 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6894 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6895 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6896 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6897 | |
| 6898 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 6899 | // then the injected touches won't cause the focused event to get dropped. |
| 6900 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 6901 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 6902 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 6903 | // For this test, it means that the key would get delivered to the window once it becomes |
| 6904 | // focused. |
| 6905 | std::this_thread::sleep_for(10ms); |
| 6906 | |
| 6907 | // Touch unfocused window. This should force the pending key to get dropped. |
| 6908 | NotifyMotionArgs motionArgs = |
| 6909 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6910 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 6911 | mDispatcher->notifyMotion(&motionArgs); |
| 6912 | |
| 6913 | // We do not consume the motion right away, because that would require dispatcher to first |
| 6914 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 6915 | // Set the focused window first. |
| 6916 | mFocusedWindow->setFocusable(true); |
| 6917 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6918 | setFocusedWindow(mFocusedWindow); |
| 6919 | mFocusedWindow->consumeFocusEvent(true); |
| 6920 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 6921 | // to another application. This could be a bug / behaviour in the policy. |
| 6922 | |
| 6923 | mUnfocusedWindow->consumeMotionDown(); |
| 6924 | |
| 6925 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6926 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 6927 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 6928 | } |
| 6929 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6930 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 6931 | // that has feature NO_INPUT_CHANNEL. |
| 6932 | // Layout: |
| 6933 | // Top (closest to user) |
| 6934 | // mNoInputWindow (above all windows) |
| 6935 | // mBottomWindow |
| 6936 | // Bottom (furthest from user) |
| 6937 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 6938 | virtual void SetUp() override { |
| 6939 | InputDispatcherTest::SetUp(); |
| 6940 | |
| 6941 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6942 | mNoInputWindow = |
| 6943 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 6944 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6945 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6946 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6947 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6948 | // It's perfectly valid for this window to not have an associated input channel |
| 6949 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6950 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 6951 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6952 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6953 | |
| 6954 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 6955 | } |
| 6956 | |
| 6957 | protected: |
| 6958 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 6959 | sp<FakeWindowHandle> mNoInputWindow; |
| 6960 | sp<FakeWindowHandle> mBottomWindow; |
| 6961 | }; |
| 6962 | |
| 6963 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 6964 | PointF touchedPoint = {10, 10}; |
| 6965 | |
| 6966 | NotifyMotionArgs motionArgs = |
| 6967 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6968 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 6969 | mDispatcher->notifyMotion(&motionArgs); |
| 6970 | |
| 6971 | mNoInputWindow->assertNoEvents(); |
| 6972 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 6973 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 6974 | // and therefore should prevent mBottomWindow from receiving touches |
| 6975 | mBottomWindow->assertNoEvents(); |
| 6976 | } |
| 6977 | |
| 6978 | /** |
| 6979 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 6980 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 6981 | */ |
| 6982 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 6983 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6984 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 6985 | "Window with input channel and NO_INPUT_CHANNEL", |
| 6986 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6987 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6988 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 6989 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6990 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 6991 | |
| 6992 | PointF touchedPoint = {10, 10}; |
| 6993 | |
| 6994 | NotifyMotionArgs motionArgs = |
| 6995 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6996 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 6997 | mDispatcher->notifyMotion(&motionArgs); |
| 6998 | |
| 6999 | mNoInputWindow->assertNoEvents(); |
| 7000 | mBottomWindow->assertNoEvents(); |
| 7001 | } |
| 7002 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7003 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7004 | protected: |
| 7005 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7006 | sp<FakeWindowHandle> mWindow; |
| 7007 | sp<FakeWindowHandle> mMirror; |
| 7008 | |
| 7009 | virtual void SetUp() override { |
| 7010 | InputDispatcherTest::SetUp(); |
| 7011 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7012 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7013 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7014 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7015 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7016 | mWindow->setFocusable(true); |
| 7017 | mMirror->setFocusable(true); |
| 7018 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7019 | } |
| 7020 | }; |
| 7021 | |
| 7022 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7023 | // Request focus on a mirrored window |
| 7024 | setFocusedWindow(mMirror); |
| 7025 | |
| 7026 | // window gets focused |
| 7027 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7028 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7029 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7030 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7031 | } |
| 7032 | |
| 7033 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7034 | // focusable. |
| 7035 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7036 | setFocusedWindow(mMirror); |
| 7037 | |
| 7038 | // window gets focused |
| 7039 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7041 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7042 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7043 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7044 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7045 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7046 | |
| 7047 | mMirror->setFocusable(false); |
| 7048 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7049 | |
| 7050 | // window loses focus since one of the windows associated with the token in not focusable |
| 7051 | mWindow->consumeFocusEvent(false); |
| 7052 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7053 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7054 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7055 | mWindow->assertNoEvents(); |
| 7056 | } |
| 7057 | |
| 7058 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7059 | // invisible. |
| 7060 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 7061 | setFocusedWindow(mMirror); |
| 7062 | |
| 7063 | // window gets focused |
| 7064 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7065 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7066 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7067 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7068 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7069 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7070 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7071 | |
| 7072 | mMirror->setVisible(false); |
| 7073 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7074 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7075 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7076 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7077 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7078 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7079 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7080 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7081 | |
| 7082 | mWindow->setVisible(false); |
| 7083 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7084 | |
| 7085 | // window loses focus only after all windows associated with the token become invisible. |
| 7086 | mWindow->consumeFocusEvent(false); |
| 7087 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7088 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7089 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7090 | mWindow->assertNoEvents(); |
| 7091 | } |
| 7092 | |
| 7093 | // A focused & mirrored window remains focused until both windows are removed. |
| 7094 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7095 | setFocusedWindow(mMirror); |
| 7096 | |
| 7097 | // window gets focused |
| 7098 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7099 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7100 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7101 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7102 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7103 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7104 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7105 | |
| 7106 | // single window is removed but the window token remains focused |
| 7107 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7108 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7109 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7110 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7111 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7113 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7114 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7115 | |
| 7116 | // Both windows are removed |
| 7117 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7118 | mWindow->consumeFocusEvent(false); |
| 7119 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7120 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7121 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7122 | mWindow->assertNoEvents(); |
| 7123 | } |
| 7124 | |
| 7125 | // Focus request can be pending until one window becomes visible. |
| 7126 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7127 | // Request focus on an invisible mirror. |
| 7128 | mWindow->setVisible(false); |
| 7129 | mMirror->setVisible(false); |
| 7130 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7131 | setFocusedWindow(mMirror); |
| 7132 | |
| 7133 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7135 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7136 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7137 | |
| 7138 | mMirror->setVisible(true); |
| 7139 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7140 | |
| 7141 | // window gets focused |
| 7142 | mWindow->consumeFocusEvent(true); |
| 7143 | // window gets the pending key event |
| 7144 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7145 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7146 | |
| 7147 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7148 | protected: |
| 7149 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7150 | sp<FakeWindowHandle> mWindow; |
| 7151 | sp<FakeWindowHandle> mSecondWindow; |
| 7152 | |
| 7153 | void SetUp() override { |
| 7154 | InputDispatcherTest::SetUp(); |
| 7155 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7156 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7157 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7158 | mSecondWindow = |
| 7159 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7160 | mSecondWindow->setFocusable(true); |
| 7161 | |
| 7162 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7163 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7164 | |
| 7165 | setFocusedWindow(mWindow); |
| 7166 | mWindow->consumeFocusEvent(true); |
| 7167 | } |
| 7168 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7169 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 7170 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7171 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 7172 | } |
| 7173 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7174 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7175 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7176 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7177 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7178 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7179 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7180 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7181 | } |
| 7182 | }; |
| 7183 | |
| 7184 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7185 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7186 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7187 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7188 | mSecondWindow->assertNoEvents(); |
| 7189 | |
| 7190 | // Ensure that capture can be enabled from the focus window. |
| 7191 | requestAndVerifyPointerCapture(mWindow, true); |
| 7192 | |
| 7193 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7194 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7195 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7196 | |
| 7197 | // Ensure that capture can be disabled from the window with capture. |
| 7198 | requestAndVerifyPointerCapture(mWindow, false); |
| 7199 | } |
| 7200 | |
| 7201 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7202 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7203 | |
| 7204 | setFocusedWindow(mSecondWindow); |
| 7205 | |
| 7206 | // Ensure that the capture disabled event was sent first. |
| 7207 | mWindow->consumeCaptureEvent(false); |
| 7208 | mWindow->consumeFocusEvent(false); |
| 7209 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7210 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7211 | |
| 7212 | // 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] | 7213 | notifyPointerCaptureChanged({}); |
| 7214 | notifyPointerCaptureChanged(request); |
| 7215 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7216 | mWindow->assertNoEvents(); |
| 7217 | mSecondWindow->assertNoEvents(); |
| 7218 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7219 | } |
| 7220 | |
| 7221 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7222 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7223 | |
| 7224 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7225 | notifyPointerCaptureChanged({}); |
| 7226 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7227 | |
| 7228 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7229 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7230 | mWindow->consumeCaptureEvent(false); |
| 7231 | mWindow->assertNoEvents(); |
| 7232 | } |
| 7233 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7234 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7235 | requestAndVerifyPointerCapture(mWindow, true); |
| 7236 | |
| 7237 | // The first window loses focus. |
| 7238 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7239 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7240 | mWindow->consumeCaptureEvent(false); |
| 7241 | |
| 7242 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7243 | // arrives. |
| 7244 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7245 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7246 | |
| 7247 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7248 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7249 | |
| 7250 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7251 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7252 | |
| 7253 | mSecondWindow->consumeFocusEvent(true); |
| 7254 | mSecondWindow->consumeCaptureEvent(true); |
| 7255 | } |
| 7256 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7257 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7258 | // App repeatedly enables and disables capture. |
| 7259 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7260 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7261 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7262 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7263 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7264 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7265 | |
| 7266 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7267 | // first request is now stale, this should do nothing. |
| 7268 | notifyPointerCaptureChanged(firstRequest); |
| 7269 | mWindow->assertNoEvents(); |
| 7270 | |
| 7271 | // InputReader notifies that the second request was enabled. |
| 7272 | notifyPointerCaptureChanged(secondRequest); |
| 7273 | mWindow->consumeCaptureEvent(true); |
| 7274 | } |
| 7275 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7276 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7277 | requestAndVerifyPointerCapture(mWindow, true); |
| 7278 | |
| 7279 | // App toggles pointer capture off and on. |
| 7280 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7281 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7282 | |
| 7283 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7284 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7285 | |
| 7286 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7287 | // preceding "disable" request. |
| 7288 | notifyPointerCaptureChanged(enableRequest); |
| 7289 | |
| 7290 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7291 | // any notifications. |
| 7292 | mWindow->assertNoEvents(); |
| 7293 | } |
| 7294 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7295 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7296 | protected: |
| 7297 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7298 | |
| 7299 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7300 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7301 | |
| 7302 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7303 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7304 | |
| 7305 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7306 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7307 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7308 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7309 | MAXIMUM_OBSCURING_OPACITY); |
| 7310 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7311 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7312 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7313 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7314 | |
| 7315 | sp<FakeWindowHandle> mTouchWindow; |
| 7316 | |
| 7317 | virtual void SetUp() override { |
| 7318 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7319 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7320 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7321 | } |
| 7322 | |
| 7323 | virtual void TearDown() override { |
| 7324 | InputDispatcherTest::TearDown(); |
| 7325 | mTouchWindow.clear(); |
| 7326 | } |
| 7327 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7328 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 7329 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7330 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7331 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7332 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7333 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7334 | return window; |
| 7335 | } |
| 7336 | |
| 7337 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 7338 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7339 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7340 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7341 | // Generate an arbitrary PID based on the UID |
| 7342 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 7343 | return window; |
| 7344 | } |
| 7345 | |
| 7346 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 7347 | NotifyMotionArgs args = |
| 7348 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7349 | ADISPLAY_ID_DEFAULT, points); |
| 7350 | mDispatcher->notifyMotion(&args); |
| 7351 | } |
| 7352 | }; |
| 7353 | |
| 7354 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7355 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7356 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7357 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7358 | |
| 7359 | touch(); |
| 7360 | |
| 7361 | mTouchWindow->assertNoEvents(); |
| 7362 | } |
| 7363 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7364 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7365 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7366 | const sp<FakeWindowHandle>& w = |
| 7367 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7368 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7369 | |
| 7370 | touch(); |
| 7371 | |
| 7372 | mTouchWindow->assertNoEvents(); |
| 7373 | } |
| 7374 | |
| 7375 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7376 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7377 | const sp<FakeWindowHandle>& w = |
| 7378 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7379 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7380 | |
| 7381 | touch(); |
| 7382 | |
| 7383 | w->assertNoEvents(); |
| 7384 | } |
| 7385 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7386 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7387 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7388 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7389 | |
| 7390 | touch(); |
| 7391 | |
| 7392 | mTouchWindow->consumeAnyMotionDown(); |
| 7393 | } |
| 7394 | |
| 7395 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7396 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7397 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7398 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7399 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7400 | |
| 7401 | touch({PointF{100, 100}}); |
| 7402 | |
| 7403 | mTouchWindow->consumeAnyMotionDown(); |
| 7404 | } |
| 7405 | |
| 7406 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7407 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7408 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7409 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7410 | |
| 7411 | touch(); |
| 7412 | |
| 7413 | mTouchWindow->consumeAnyMotionDown(); |
| 7414 | } |
| 7415 | |
| 7416 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 7417 | const sp<FakeWindowHandle>& w = |
| 7418 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7419 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7420 | |
| 7421 | touch(); |
| 7422 | |
| 7423 | mTouchWindow->consumeAnyMotionDown(); |
| 7424 | } |
| 7425 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7426 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 7427 | const sp<FakeWindowHandle>& w = |
| 7428 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7429 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7430 | |
| 7431 | touch(); |
| 7432 | |
| 7433 | w->assertNoEvents(); |
| 7434 | } |
| 7435 | |
| 7436 | /** |
| 7437 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 7438 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 7439 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 7440 | */ |
| 7441 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7442 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 7443 | const sp<FakeWindowHandle>& w = |
| 7444 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7445 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7446 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7447 | |
| 7448 | touch(); |
| 7449 | |
| 7450 | w->consumeMotionOutside(); |
| 7451 | } |
| 7452 | |
| 7453 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 7454 | const sp<FakeWindowHandle>& w = |
| 7455 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7456 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7457 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7458 | |
| 7459 | touch(); |
| 7460 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7461 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7462 | } |
| 7463 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7464 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7465 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7466 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7467 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7468 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7469 | |
| 7470 | touch(); |
| 7471 | |
| 7472 | mTouchWindow->consumeAnyMotionDown(); |
| 7473 | } |
| 7474 | |
| 7475 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 7476 | const sp<FakeWindowHandle>& w = |
| 7477 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7478 | MAXIMUM_OBSCURING_OPACITY); |
| 7479 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7480 | |
| 7481 | touch(); |
| 7482 | |
| 7483 | mTouchWindow->consumeAnyMotionDown(); |
| 7484 | } |
| 7485 | |
| 7486 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7487 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7488 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7489 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7490 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7491 | |
| 7492 | touch(); |
| 7493 | |
| 7494 | mTouchWindow->assertNoEvents(); |
| 7495 | } |
| 7496 | |
| 7497 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 7498 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 7499 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7500 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7501 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7502 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7503 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7504 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7505 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7506 | |
| 7507 | touch(); |
| 7508 | |
| 7509 | mTouchWindow->assertNoEvents(); |
| 7510 | } |
| 7511 | |
| 7512 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 7513 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 7514 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7515 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7516 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7517 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7518 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7519 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7520 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7521 | |
| 7522 | touch(); |
| 7523 | |
| 7524 | mTouchWindow->consumeAnyMotionDown(); |
| 7525 | } |
| 7526 | |
| 7527 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7528 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 7529 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7530 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7531 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7532 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7533 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7534 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7536 | |
| 7537 | touch(); |
| 7538 | |
| 7539 | mTouchWindow->consumeAnyMotionDown(); |
| 7540 | } |
| 7541 | |
| 7542 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 7543 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7544 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7545 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7546 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7547 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7548 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7549 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7550 | |
| 7551 | touch(); |
| 7552 | |
| 7553 | mTouchWindow->assertNoEvents(); |
| 7554 | } |
| 7555 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7556 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7557 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 7558 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7559 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7560 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7561 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7562 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7563 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7564 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7565 | |
| 7566 | touch(); |
| 7567 | |
| 7568 | mTouchWindow->assertNoEvents(); |
| 7569 | } |
| 7570 | |
| 7571 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7572 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 7573 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7574 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7575 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7576 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7577 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7578 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7579 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7580 | |
| 7581 | touch(); |
| 7582 | |
| 7583 | mTouchWindow->consumeAnyMotionDown(); |
| 7584 | } |
| 7585 | |
| 7586 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 7587 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7588 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7589 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7590 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7591 | |
| 7592 | touch(); |
| 7593 | |
| 7594 | mTouchWindow->consumeAnyMotionDown(); |
| 7595 | } |
| 7596 | |
| 7597 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 7598 | const sp<FakeWindowHandle>& w = |
| 7599 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7600 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7601 | |
| 7602 | touch(); |
| 7603 | |
| 7604 | mTouchWindow->consumeAnyMotionDown(); |
| 7605 | } |
| 7606 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7607 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7608 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 7609 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7610 | const sp<FakeWindowHandle>& w = |
| 7611 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 7612 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7613 | |
| 7614 | touch(); |
| 7615 | |
| 7616 | mTouchWindow->assertNoEvents(); |
| 7617 | } |
| 7618 | |
| 7619 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 7620 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7621 | const sp<FakeWindowHandle>& w = |
| 7622 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 7623 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7624 | |
| 7625 | touch(); |
| 7626 | |
| 7627 | mTouchWindow->consumeAnyMotionDown(); |
| 7628 | } |
| 7629 | |
| 7630 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7631 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 7632 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 7633 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7634 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7635 | OPACITY_ABOVE_THRESHOLD); |
| 7636 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7637 | |
| 7638 | touch(); |
| 7639 | |
| 7640 | mTouchWindow->consumeAnyMotionDown(); |
| 7641 | } |
| 7642 | |
| 7643 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7644 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 7645 | const sp<FakeWindowHandle>& w1 = |
| 7646 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7647 | OPACITY_BELOW_THRESHOLD); |
| 7648 | const sp<FakeWindowHandle>& w2 = |
| 7649 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7650 | OPACITY_BELOW_THRESHOLD); |
| 7651 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7652 | |
| 7653 | touch(); |
| 7654 | |
| 7655 | mTouchWindow->assertNoEvents(); |
| 7656 | } |
| 7657 | |
| 7658 | /** |
| 7659 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 7660 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 7661 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 7662 | */ |
| 7663 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7664 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 7665 | const sp<FakeWindowHandle>& wB = |
| 7666 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7667 | OPACITY_BELOW_THRESHOLD); |
| 7668 | const sp<FakeWindowHandle>& wC = |
| 7669 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7670 | OPACITY_BELOW_THRESHOLD); |
| 7671 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7672 | |
| 7673 | touch(); |
| 7674 | |
| 7675 | mTouchWindow->assertNoEvents(); |
| 7676 | } |
| 7677 | |
| 7678 | /** |
| 7679 | * This test is testing that a window from a different UID but with same application token doesn't |
| 7680 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 7681 | */ |
| 7682 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7683 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 7684 | const sp<FakeWindowHandle>& w = |
| 7685 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7686 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7687 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7688 | |
| 7689 | touch(); |
| 7690 | |
| 7691 | mTouchWindow->consumeAnyMotionDown(); |
| 7692 | } |
| 7693 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7694 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 7695 | protected: |
| 7696 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7697 | sp<FakeWindowHandle> mWindow; |
| 7698 | sp<FakeWindowHandle> mSecondWindow; |
| 7699 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7700 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7701 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 7702 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7703 | |
| 7704 | void SetUp() override { |
| 7705 | InputDispatcherTest::SetUp(); |
| 7706 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7707 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7708 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7709 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7710 | mSecondWindow = |
| 7711 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7712 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7713 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7714 | mSpyWindow = |
| 7715 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7716 | mSpyWindow->setSpy(true); |
| 7717 | mSpyWindow->setTrustedOverlay(true); |
| 7718 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 7719 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7720 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7721 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7722 | } |
| 7723 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7724 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 7725 | switch (fromSource) { |
| 7726 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 7727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7728 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7729 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7730 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7731 | break; |
| 7732 | case AINPUT_SOURCE_STYLUS: |
| 7733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7734 | injectMotionEvent( |
| 7735 | mDispatcher, |
| 7736 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7737 | AINPUT_SOURCE_STYLUS) |
| 7738 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 7739 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7740 | .x(50) |
| 7741 | .y(50)) |
| 7742 | .build())); |
| 7743 | break; |
| 7744 | case AINPUT_SOURCE_MOUSE: |
| 7745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7746 | injectMotionEvent( |
| 7747 | mDispatcher, |
| 7748 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7749 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7750 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 7751 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 7752 | .x(50) |
| 7753 | .y(50)) |
| 7754 | .build())); |
| 7755 | break; |
| 7756 | default: |
| 7757 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 7758 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7759 | |
| 7760 | // Window should receive motion event. |
| 7761 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7762 | // Spy window should also receive motion event |
| 7763 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7764 | } |
| 7765 | |
| 7766 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 7767 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 7768 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7769 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7770 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7771 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7772 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7773 | |
| 7774 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7775 | mDragWindow = |
| 7776 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7777 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7778 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7779 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7780 | |
| 7781 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7782 | bool transferred = |
| 7783 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7784 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7785 | if (transferred) { |
| 7786 | mWindow->consumeMotionCancel(); |
| 7787 | mDragWindow->consumeMotionDown(); |
| 7788 | } |
| 7789 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7790 | } |
| 7791 | }; |
| 7792 | |
| 7793 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7794 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7795 | |
| 7796 | // Move on window. |
| 7797 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7798 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7799 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7800 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7801 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7802 | mWindow->consumeDragEvent(false, 50, 50); |
| 7803 | mSecondWindow->assertNoEvents(); |
| 7804 | |
| 7805 | // Move to another window. |
| 7806 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7807 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7808 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7809 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7810 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7811 | mWindow->consumeDragEvent(true, 150, 50); |
| 7812 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7813 | |
| 7814 | // Move back to original window. |
| 7815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7816 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7817 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7819 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7820 | mWindow->consumeDragEvent(false, 50, 50); |
| 7821 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 7822 | |
| 7823 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7824 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7825 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7826 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7827 | mWindow->assertNoEvents(); |
| 7828 | mSecondWindow->assertNoEvents(); |
| 7829 | } |
| 7830 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7831 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7832 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7833 | |
| 7834 | // No cancel event after drag start |
| 7835 | mSpyWindow->assertNoEvents(); |
| 7836 | |
| 7837 | const MotionEvent secondFingerDownEvent = |
| 7838 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7839 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7840 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7841 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7842 | .build(); |
| 7843 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7844 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7845 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7846 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7847 | |
| 7848 | // Receives cancel for first pointer after next pointer down |
| 7849 | mSpyWindow->consumeMotionCancel(); |
| 7850 | mSpyWindow->consumeMotionDown(); |
| 7851 | |
| 7852 | mSpyWindow->assertNoEvents(); |
| 7853 | } |
| 7854 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7855 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7856 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7857 | |
| 7858 | // Move on window. |
| 7859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7860 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7861 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7862 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7863 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7864 | mWindow->consumeDragEvent(false, 50, 50); |
| 7865 | mSecondWindow->assertNoEvents(); |
| 7866 | |
| 7867 | // Move to another window. |
| 7868 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7869 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7870 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7871 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7872 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7873 | mWindow->consumeDragEvent(true, 150, 50); |
| 7874 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7875 | |
| 7876 | // drop to another window. |
| 7877 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7878 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7879 | {150, 50})) |
| 7880 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7881 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7882 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7883 | mWindow->assertNoEvents(); |
| 7884 | mSecondWindow->assertNoEvents(); |
| 7885 | } |
| 7886 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7887 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7888 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7889 | |
| 7890 | // Move on window and keep button pressed. |
| 7891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7892 | injectMotionEvent(mDispatcher, |
| 7893 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7894 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 7895 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7896 | .x(50) |
| 7897 | .y(50)) |
| 7898 | .build())) |
| 7899 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7900 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7901 | mWindow->consumeDragEvent(false, 50, 50); |
| 7902 | mSecondWindow->assertNoEvents(); |
| 7903 | |
| 7904 | // Move to another window and release button, expect to drop item. |
| 7905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7906 | injectMotionEvent(mDispatcher, |
| 7907 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7908 | .buttonState(0) |
| 7909 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7910 | .x(150) |
| 7911 | .y(50)) |
| 7912 | .build())) |
| 7913 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7914 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7915 | mWindow->assertNoEvents(); |
| 7916 | mSecondWindow->assertNoEvents(); |
| 7917 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7918 | |
| 7919 | // nothing to the window. |
| 7920 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7921 | injectMotionEvent(mDispatcher, |
| 7922 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 7923 | .buttonState(0) |
| 7924 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_STYLUS) |
| 7925 | .x(150) |
| 7926 | .y(50)) |
| 7927 | .build())) |
| 7928 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7929 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7930 | mWindow->assertNoEvents(); |
| 7931 | mSecondWindow->assertNoEvents(); |
| 7932 | } |
| 7933 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7934 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7935 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 7936 | |
| 7937 | // Set second window invisible. |
| 7938 | mSecondWindow->setVisible(false); |
| 7939 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 7940 | |
| 7941 | // Move on window. |
| 7942 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7943 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7944 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7945 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7946 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7947 | mWindow->consumeDragEvent(false, 50, 50); |
| 7948 | mSecondWindow->assertNoEvents(); |
| 7949 | |
| 7950 | // Move to another window. |
| 7951 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7952 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7953 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7954 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7955 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7956 | mWindow->consumeDragEvent(true, 150, 50); |
| 7957 | mSecondWindow->assertNoEvents(); |
| 7958 | |
| 7959 | // drop to another window. |
| 7960 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7961 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7962 | {150, 50})) |
| 7963 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7964 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7965 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 7966 | mWindow->assertNoEvents(); |
| 7967 | mSecondWindow->assertNoEvents(); |
| 7968 | } |
| 7969 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7970 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7971 | // Ensure window could track pointerIds if it didn't support split touch. |
| 7972 | mWindow->setPreventSplitting(true); |
| 7973 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7974 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7975 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7976 | {50, 50})) |
| 7977 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7978 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7979 | |
| 7980 | const MotionEvent secondFingerDownEvent = |
| 7981 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7982 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7983 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7984 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 7985 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7986 | .build(); |
| 7987 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7988 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7989 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7990 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7991 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7992 | |
| 7993 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7994 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7995 | } |
| 7996 | |
| 7997 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 7998 | // First down on second window. |
| 7999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8000 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8001 | {150, 50})) |
| 8002 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8003 | |
| 8004 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8005 | |
| 8006 | // Second down on first window. |
| 8007 | const MotionEvent secondFingerDownEvent = |
| 8008 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8009 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8010 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8011 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 8012 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8013 | .build(); |
| 8014 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8015 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8016 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8017 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8018 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8019 | |
| 8020 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8021 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8022 | |
| 8023 | // Move on window. |
| 8024 | const MotionEvent secondFingerMoveEvent = |
| 8025 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8026 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8027 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 8028 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8029 | .build(); |
| 8030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8031 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8032 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8033 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8034 | mWindow->consumeDragEvent(false, 50, 50); |
| 8035 | mSecondWindow->consumeMotionMove(); |
| 8036 | |
| 8037 | // Release the drag pointer should perform drop. |
| 8038 | const MotionEvent secondFingerUpEvent = |
| 8039 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8040 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8041 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
| 8042 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8043 | .build(); |
| 8044 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8045 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8046 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8047 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8048 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 8049 | mWindow->assertNoEvents(); |
| 8050 | mSecondWindow->consumeMotionMove(); |
| 8051 | } |
| 8052 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8053 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8054 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8055 | |
| 8056 | // Update window of second display. |
| 8057 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8058 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8059 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8060 | |
| 8061 | // Let second display has a touch state. |
| 8062 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8063 | injectMotionEvent(mDispatcher, |
| 8064 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8065 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8066 | .displayId(SECOND_DISPLAY_ID) |
| 8067 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 8068 | .x(100) |
| 8069 | .y(100)) |
| 8070 | .build())); |
| 8071 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8072 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8073 | // Update window again. |
| 8074 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8075 | |
| 8076 | // Move on window. |
| 8077 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8078 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8079 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8080 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8081 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8082 | mWindow->consumeDragEvent(false, 50, 50); |
| 8083 | mSecondWindow->assertNoEvents(); |
| 8084 | |
| 8085 | // Move to another window. |
| 8086 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8087 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8088 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8089 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8090 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8091 | mWindow->consumeDragEvent(true, 150, 50); |
| 8092 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8093 | |
| 8094 | // drop to another window. |
| 8095 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8096 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8097 | {150, 50})) |
| 8098 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8099 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8100 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8101 | mWindow->assertNoEvents(); |
| 8102 | mSecondWindow->assertNoEvents(); |
| 8103 | } |
| 8104 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8105 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8106 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8107 | // Move on window. |
| 8108 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8109 | injectMotionEvent(mDispatcher, |
| 8110 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8111 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8112 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 8113 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 8114 | .x(50) |
| 8115 | .y(50)) |
| 8116 | .build())) |
| 8117 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8118 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8119 | mWindow->consumeDragEvent(false, 50, 50); |
| 8120 | mSecondWindow->assertNoEvents(); |
| 8121 | |
| 8122 | // Move to another window. |
| 8123 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8124 | injectMotionEvent(mDispatcher, |
| 8125 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8126 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8127 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 8128 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 8129 | .x(150) |
| 8130 | .y(50)) |
| 8131 | .build())) |
| 8132 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8133 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8134 | mWindow->consumeDragEvent(true, 150, 50); |
| 8135 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8136 | |
| 8137 | // drop to another window. |
| 8138 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8139 | injectMotionEvent(mDispatcher, |
| 8140 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8141 | .buttonState(0) |
| 8142 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 8143 | AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 8144 | .x(150) |
| 8145 | .y(50)) |
| 8146 | .build())) |
| 8147 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8148 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8149 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8150 | mWindow->assertNoEvents(); |
| 8151 | mSecondWindow->assertNoEvents(); |
| 8152 | } |
| 8153 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8154 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8155 | |
| 8156 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8157 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8158 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8159 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8160 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8161 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8162 | window->setFocusable(true); |
| 8163 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8164 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8165 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8166 | |
| 8167 | // With the flag set, window should not get any input |
| 8168 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 8169 | mDispatcher->notifyKey(&keyArgs); |
| 8170 | window->assertNoEvents(); |
| 8171 | |
| 8172 | NotifyMotionArgs motionArgs = |
| 8173 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8174 | ADISPLAY_ID_DEFAULT); |
| 8175 | mDispatcher->notifyMotion(&motionArgs); |
| 8176 | window->assertNoEvents(); |
| 8177 | |
| 8178 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8179 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8180 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8181 | |
| 8182 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 8183 | mDispatcher->notifyKey(&keyArgs); |
| 8184 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8185 | |
| 8186 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8187 | ADISPLAY_ID_DEFAULT); |
| 8188 | mDispatcher->notifyMotion(&motionArgs); |
| 8189 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8190 | window->assertNoEvents(); |
| 8191 | } |
| 8192 | |
| 8193 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8194 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8195 | std::make_shared<FakeApplicationHandle>(); |
| 8196 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8197 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8198 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8199 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8200 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8201 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8202 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8203 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8204 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8205 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8206 | window->setOwnerInfo(222, 222); |
| 8207 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8208 | window->setFocusable(true); |
| 8209 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8210 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8211 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8212 | |
| 8213 | // With the flag set, window should not get any input |
| 8214 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 8215 | mDispatcher->notifyKey(&keyArgs); |
| 8216 | window->assertNoEvents(); |
| 8217 | |
| 8218 | NotifyMotionArgs motionArgs = |
| 8219 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8220 | ADISPLAY_ID_DEFAULT); |
| 8221 | mDispatcher->notifyMotion(&motionArgs); |
| 8222 | window->assertNoEvents(); |
| 8223 | |
| 8224 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8225 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8226 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8227 | |
| 8228 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 8229 | mDispatcher->notifyKey(&keyArgs); |
| 8230 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8231 | |
| 8232 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8233 | ADISPLAY_ID_DEFAULT); |
| 8234 | mDispatcher->notifyMotion(&motionArgs); |
| 8235 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8236 | window->assertNoEvents(); |
| 8237 | } |
| 8238 | |
| 8239 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8240 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8241 | std::make_shared<FakeApplicationHandle>(); |
| 8242 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8243 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8244 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8245 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8246 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8247 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8248 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8249 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8250 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8251 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8252 | window->setOwnerInfo(222, 222); |
| 8253 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8254 | window->setFocusable(true); |
| 8255 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8256 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8257 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8258 | |
| 8259 | // With the flag set, window should not get any input |
| 8260 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 8261 | mDispatcher->notifyKey(&keyArgs); |
| 8262 | window->assertNoEvents(); |
| 8263 | |
| 8264 | NotifyMotionArgs motionArgs = |
| 8265 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8266 | ADISPLAY_ID_DEFAULT); |
| 8267 | mDispatcher->notifyMotion(&motionArgs); |
| 8268 | window->assertNoEvents(); |
| 8269 | |
| 8270 | // When the window is no longer obscured because it went on top, it should get input |
| 8271 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8272 | |
| 8273 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 8274 | mDispatcher->notifyKey(&keyArgs); |
| 8275 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8276 | |
| 8277 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8278 | ADISPLAY_ID_DEFAULT); |
| 8279 | mDispatcher->notifyMotion(&motionArgs); |
| 8280 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8281 | window->assertNoEvents(); |
| 8282 | } |
| 8283 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8284 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8285 | protected: |
| 8286 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8287 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8288 | sp<FakeWindowHandle> mWindow; |
| 8289 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8290 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8291 | |
| 8292 | void SetUp() override { |
| 8293 | InputDispatcherTest::SetUp(); |
| 8294 | |
| 8295 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8296 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8297 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8298 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8299 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8300 | mSecondWindow = |
| 8301 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8302 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8303 | mThirdWindow = |
| 8304 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8305 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8306 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8307 | |
| 8308 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8309 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8310 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8311 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8312 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8313 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8314 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8315 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8316 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8317 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8318 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8319 | mThirdWindow->assertNoEvents(); |
| 8320 | } |
| 8321 | |
| 8322 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8323 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8324 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8325 | SECOND_DISPLAY_ID)) { |
| 8326 | mWindow->assertNoEvents(); |
| 8327 | mSecondWindow->assertNoEvents(); |
| 8328 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8329 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8330 | } |
| 8331 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8332 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 8333 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8334 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8335 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8336 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8337 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8338 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8339 | } |
| 8340 | }; |
| 8341 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8342 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8343 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8344 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8345 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8346 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8347 | } |
| 8348 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8349 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8350 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8351 | int32_t ownerPid = windowInfo.ownerPid; |
| 8352 | int32_t ownerUid = windowInfo.ownerUid; |
| 8353 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 8354 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8355 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8356 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8357 | mWindow->assertNoEvents(); |
| 8358 | mSecondWindow->assertNoEvents(); |
| 8359 | } |
| 8360 | |
| 8361 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8362 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8363 | int32_t ownerPid = windowInfo.ownerPid; |
| 8364 | int32_t ownerUid = windowInfo.ownerUid; |
| 8365 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8366 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8367 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8368 | } |
| 8369 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8370 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8371 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8372 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8373 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8374 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8375 | mWindow->assertNoEvents(); |
| 8376 | mSecondWindow->assertNoEvents(); |
| 8377 | } |
| 8378 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8379 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8380 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8381 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8382 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8383 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8384 | mWindow->assertNoEvents(); |
| 8385 | mSecondWindow->assertNoEvents(); |
| 8386 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8387 | } |
| 8388 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8389 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8390 | // Interact with the window first. |
| 8391 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8392 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8393 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8394 | |
| 8395 | // Then remove focus. |
| 8396 | mWindow->setFocusable(false); |
| 8397 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8398 | |
| 8399 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8400 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8401 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8402 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8403 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8404 | } |
| 8405 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8406 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8407 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8408 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8409 | std::shared_ptr<FakeApplicationHandle> application = |
| 8410 | std::make_shared<FakeApplicationHandle>(); |
| 8411 | std::string name = "Fake Spy "; |
| 8412 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8413 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8414 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8415 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8416 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8417 | return spy; |
| 8418 | } |
| 8419 | |
| 8420 | sp<FakeWindowHandle> createForeground() { |
| 8421 | std::shared_ptr<FakeApplicationHandle> application = |
| 8422 | std::make_shared<FakeApplicationHandle>(); |
| 8423 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8424 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 8425 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8426 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8427 | return window; |
| 8428 | } |
| 8429 | |
| 8430 | private: |
| 8431 | int mSpyCount{0}; |
| 8432 | }; |
| 8433 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8434 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8435 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8436 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 8437 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8438 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 8439 | ScopedSilentDeath _silentDeath; |
| 8440 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8441 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8442 | spy->setTrustedOverlay(false); |
| 8443 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 8444 | ".* not a trusted overlay"); |
| 8445 | } |
| 8446 | |
| 8447 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8448 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 8449 | */ |
| 8450 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8451 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8452 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 8453 | |
| 8454 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8455 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8456 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8457 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8458 | } |
| 8459 | |
| 8460 | /** |
| 8461 | * Verify the order in which different input windows receive events. The touched foreground window |
| 8462 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 8463 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 8464 | * receive events before ones belows it. |
| 8465 | * |
| 8466 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 8467 | * spy1, spy2, window, spy3. |
| 8468 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 8469 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 8470 | * window. |
| 8471 | */ |
| 8472 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 8473 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8474 | auto spy1 = createSpy(); |
| 8475 | auto spy2 = createSpy(); |
| 8476 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8477 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 8478 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 8479 | const size_t numChannels = channels.size(); |
| 8480 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 8481 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8482 | if (!epollFd.ok()) { |
| 8483 | FAIL() << "Failed to create epoll fd"; |
| 8484 | } |
| 8485 | |
| 8486 | for (size_t i = 0; i < numChannels; i++) { |
| 8487 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 8488 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 8489 | FAIL() << "Failed to add fd to epoll"; |
| 8490 | } |
| 8491 | } |
| 8492 | |
| 8493 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8494 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8495 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8496 | |
| 8497 | std::vector<size_t> eventOrder; |
| 8498 | std::vector<struct epoll_event> events(numChannels); |
| 8499 | for (;;) { |
| 8500 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 8501 | (100ms).count()); |
| 8502 | if (nFds < 0) { |
| 8503 | FAIL() << "Failed to call epoll_wait"; |
| 8504 | } |
| 8505 | if (nFds == 0) { |
| 8506 | break; // epoll_wait timed out |
| 8507 | } |
| 8508 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 8509 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 8510 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8511 | channels[i]->consumeMotionDown(); |
| 8512 | } |
| 8513 | } |
| 8514 | |
| 8515 | // Verify the order in which the events were received. |
| 8516 | EXPECT_EQ(3u, eventOrder.size()); |
| 8517 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 8518 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 8519 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 8520 | } |
| 8521 | |
| 8522 | /** |
| 8523 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 8524 | */ |
| 8525 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 8526 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8527 | auto spy = createSpy(); |
| 8528 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8529 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8530 | |
| 8531 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8532 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8533 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8534 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8535 | spy->assertNoEvents(); |
| 8536 | } |
| 8537 | |
| 8538 | /** |
| 8539 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 8540 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 8541 | * to the window. |
| 8542 | */ |
| 8543 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 8544 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8545 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8546 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 8547 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8548 | |
| 8549 | // Inject an event outside the spy window's touchable region. |
| 8550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8551 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8552 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8553 | window->consumeMotionDown(); |
| 8554 | spy->assertNoEvents(); |
| 8555 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8556 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8557 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8558 | window->consumeMotionUp(); |
| 8559 | spy->assertNoEvents(); |
| 8560 | |
| 8561 | // Inject an event inside the spy window's touchable region. |
| 8562 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8563 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8564 | {5, 10})) |
| 8565 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8566 | window->consumeMotionDown(); |
| 8567 | spy->consumeMotionDown(); |
| 8568 | } |
| 8569 | |
| 8570 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8571 | * 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] | 8572 | * 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] | 8573 | */ |
| 8574 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 8575 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8576 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8577 | auto spy = createSpy(); |
| 8578 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8579 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8580 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 8581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8582 | |
| 8583 | // Inject an event outside the spy window's frame and touchable region. |
| 8584 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8585 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8586 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8587 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8588 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8589 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8590 | } |
| 8591 | |
| 8592 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8593 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 8594 | * pointers that are down within its bounds. |
| 8595 | */ |
| 8596 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 8597 | auto windowLeft = createForeground(); |
| 8598 | windowLeft->setFrame({0, 0, 100, 200}); |
| 8599 | auto windowRight = createForeground(); |
| 8600 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8601 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8602 | spy->setFrame({0, 0, 200, 200}); |
| 8603 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 8604 | |
| 8605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8606 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8607 | {50, 50})) |
| 8608 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8609 | windowLeft->consumeMotionDown(); |
| 8610 | spy->consumeMotionDown(); |
| 8611 | |
| 8612 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8613 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8614 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8615 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8616 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8617 | .build(); |
| 8618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8619 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8620 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8621 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8622 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8623 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8624 | } |
| 8625 | |
| 8626 | /** |
| 8627 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 8628 | * the spy should receive the second pointer with ACTION_DOWN. |
| 8629 | */ |
| 8630 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 8631 | auto window = createForeground(); |
| 8632 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8633 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8634 | spyRight->setFrame({100, 0, 200, 200}); |
| 8635 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 8636 | |
| 8637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8638 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8639 | {50, 50})) |
| 8640 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8641 | window->consumeMotionDown(); |
| 8642 | spyRight->assertNoEvents(); |
| 8643 | |
| 8644 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8645 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8646 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8647 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8648 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8649 | .build(); |
| 8650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8651 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8652 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8653 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8654 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8655 | spyRight->consumeMotionDown(); |
| 8656 | } |
| 8657 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8658 | /** |
| 8659 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 8660 | * windows should be allowed to control split touch. |
| 8661 | */ |
| 8662 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8663 | // 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] | 8664 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8665 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8666 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8667 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8668 | auto window = createForeground(); |
| 8669 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8670 | |
| 8671 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8672 | |
| 8673 | // First finger down, no window touched. |
| 8674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8675 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8676 | {100, 200})) |
| 8677 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8678 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8679 | window->assertNoEvents(); |
| 8680 | |
| 8681 | // Second finger down on window, the window should receive touch down. |
| 8682 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8683 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8684 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8685 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8686 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 8687 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8688 | .build(); |
| 8689 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8690 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8691 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8692 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8693 | |
| 8694 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8695 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8696 | } |
| 8697 | |
| 8698 | /** |
| 8699 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 8700 | * do not receive key events. |
| 8701 | */ |
| 8702 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8703 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8704 | spy->setFocusable(false); |
| 8705 | |
| 8706 | auto window = createForeground(); |
| 8707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8708 | setFocusedWindow(window); |
| 8709 | window->consumeFocusEvent(true); |
| 8710 | |
| 8711 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 8712 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8713 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8714 | |
| 8715 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 8716 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8717 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8718 | |
| 8719 | spy->assertNoEvents(); |
| 8720 | } |
| 8721 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8722 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 8723 | |
| 8724 | /** |
| 8725 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 8726 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 8727 | */ |
| 8728 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 8729 | auto window = createForeground(); |
| 8730 | auto spy1 = createSpy(); |
| 8731 | auto spy2 = createSpy(); |
| 8732 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 8733 | |
| 8734 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8735 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8736 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8737 | window->consumeMotionDown(); |
| 8738 | spy1->consumeMotionDown(); |
| 8739 | spy2->consumeMotionDown(); |
| 8740 | |
| 8741 | // Pilfer pointers from the second spy window. |
| 8742 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 8743 | spy2->assertNoEvents(); |
| 8744 | spy1->consumeMotionCancel(); |
| 8745 | window->consumeMotionCancel(); |
| 8746 | |
| 8747 | // The rest of the gesture should only be sent to the second spy window. |
| 8748 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8749 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8750 | ADISPLAY_ID_DEFAULT)) |
| 8751 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8752 | spy2->consumeMotionMove(); |
| 8753 | spy1->assertNoEvents(); |
| 8754 | window->assertNoEvents(); |
| 8755 | } |
| 8756 | |
| 8757 | /** |
| 8758 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 8759 | * in the middle of the gesture. |
| 8760 | */ |
| 8761 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 8762 | auto window = createForeground(); |
| 8763 | auto spy = createSpy(); |
| 8764 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8765 | |
| 8766 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8767 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8768 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8769 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8770 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8771 | |
| 8772 | window->releaseChannel(); |
| 8773 | |
| 8774 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8775 | |
| 8776 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8777 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8778 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8779 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8780 | } |
| 8781 | |
| 8782 | /** |
| 8783 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 8784 | * the spy, but not to any other windows. |
| 8785 | */ |
| 8786 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 8787 | auto spy = createSpy(); |
| 8788 | auto window = createForeground(); |
| 8789 | |
| 8790 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8791 | |
| 8792 | // First finger down on the window and the spy. |
| 8793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8794 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8795 | {100, 200})) |
| 8796 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8797 | spy->consumeMotionDown(); |
| 8798 | window->consumeMotionDown(); |
| 8799 | |
| 8800 | // Spy window pilfers the pointers. |
| 8801 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8802 | window->consumeMotionCancel(); |
| 8803 | |
| 8804 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 8805 | const MotionEvent secondFingerDownEvent = |
| 8806 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8807 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8808 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8809 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 8810 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8811 | .build(); |
| 8812 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8813 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8814 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8815 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8816 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8817 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8818 | |
| 8819 | // Third finger goes down outside all windows, so injection should fail. |
| 8820 | const MotionEvent thirdFingerDownEvent = |
| 8821 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8822 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8823 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8824 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(100).y(200)) |
| 8825 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
| 8826 | .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8827 | .build(); |
| 8828 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 8829 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8830 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8831 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8832 | |
| 8833 | spy->assertNoEvents(); |
| 8834 | window->assertNoEvents(); |
| 8835 | } |
| 8836 | |
| 8837 | /** |
| 8838 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 8839 | */ |
| 8840 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 8841 | auto spy = createSpy(); |
| 8842 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8843 | auto window = createForeground(); |
| 8844 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8845 | |
| 8846 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8847 | |
| 8848 | // First finger down on the window only |
| 8849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8850 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8851 | {150, 150})) |
| 8852 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8853 | window->consumeMotionDown(); |
| 8854 | |
| 8855 | // Second finger down on the spy and window |
| 8856 | const MotionEvent secondFingerDownEvent = |
| 8857 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8858 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8859 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8860 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
| 8861 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8862 | .build(); |
| 8863 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8864 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8865 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8866 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8867 | spy->consumeMotionDown(); |
| 8868 | window->consumeMotionPointerDown(1); |
| 8869 | |
| 8870 | // Third finger down on the spy and window |
| 8871 | const MotionEvent thirdFingerDownEvent = |
| 8872 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8873 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8874 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8875 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
| 8876 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8877 | .pointer(PointerBuilder(/*id=*/2, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8878 | .build(); |
| 8879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8880 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8881 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8882 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8883 | spy->consumeMotionPointerDown(1); |
| 8884 | window->consumeMotionPointerDown(2); |
| 8885 | |
| 8886 | // Spy window pilfers the pointers. |
| 8887 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8888 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8889 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8890 | |
| 8891 | spy->assertNoEvents(); |
| 8892 | window->assertNoEvents(); |
| 8893 | } |
| 8894 | |
| 8895 | /** |
| 8896 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 8897 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 8898 | * window, then that window should receive ACTION_CANCEL. |
| 8899 | */ |
| 8900 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 8901 | auto spy = createSpy(); |
| 8902 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8903 | auto window = createForeground(); |
| 8904 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8905 | |
| 8906 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8907 | |
| 8908 | // First finger down on both spy and window |
| 8909 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8910 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8911 | {10, 10})) |
| 8912 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8913 | window->consumeMotionDown(); |
| 8914 | spy->consumeMotionDown(); |
| 8915 | |
| 8916 | // Second finger down on the spy and window |
| 8917 | const MotionEvent secondFingerDownEvent = |
| 8918 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8919 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8920 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8921 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8922 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8923 | .build(); |
| 8924 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8925 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8926 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8927 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8928 | spy->consumeMotionPointerDown(1); |
| 8929 | window->consumeMotionPointerDown(1); |
| 8930 | |
| 8931 | // Spy window pilfers the pointers. |
| 8932 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8933 | window->consumeMotionCancel(); |
| 8934 | |
| 8935 | spy->assertNoEvents(); |
| 8936 | window->assertNoEvents(); |
| 8937 | } |
| 8938 | |
| 8939 | /** |
| 8940 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 8941 | * be sent to other windows |
| 8942 | */ |
| 8943 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 8944 | auto spy = createSpy(); |
| 8945 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8946 | auto window = createForeground(); |
| 8947 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8948 | |
| 8949 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8950 | |
| 8951 | // First finger down on both window and spy |
| 8952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8953 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8954 | {10, 10})) |
| 8955 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8956 | window->consumeMotionDown(); |
| 8957 | spy->consumeMotionDown(); |
| 8958 | |
| 8959 | // Spy window pilfers the pointers. |
| 8960 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8961 | window->consumeMotionCancel(); |
| 8962 | |
| 8963 | // Second finger down on the window only |
| 8964 | const MotionEvent secondFingerDownEvent = |
| 8965 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8966 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8967 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8968 | .pointer(PointerBuilder(/*id=*/0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(10).y(10)) |
| 8969 | .pointer(PointerBuilder(/*id=*/1, AMOTION_EVENT_TOOL_TYPE_FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8970 | .build(); |
| 8971 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8972 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8973 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8974 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8975 | window->consumeMotionDown(); |
| 8976 | window->assertNoEvents(); |
| 8977 | |
| 8978 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 8979 | spy->consumeMotionMove(); |
| 8980 | spy->assertNoEvents(); |
| 8981 | } |
| 8982 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8983 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 8984 | public: |
| 8985 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 8986 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 8987 | std::make_shared<FakeApplicationHandle>(); |
| 8988 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8989 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 8990 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8991 | overlay->setFocusable(false); |
| 8992 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8993 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8994 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 8995 | overlay->setTrustedOverlay(true); |
| 8996 | |
| 8997 | std::shared_ptr<FakeApplicationHandle> application = |
| 8998 | std::make_shared<FakeApplicationHandle>(); |
| 8999 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9000 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9001 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9002 | window->setFocusable(true); |
| 9003 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9004 | |
| 9005 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9006 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9007 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9008 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9009 | return {std::move(overlay), std::move(window)}; |
| 9010 | } |
| 9011 | |
| 9012 | void sendFingerEvent(int32_t action) { |
| 9013 | NotifyMotionArgs motionArgs = |
| 9014 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9015 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 9016 | mDispatcher->notifyMotion(&motionArgs); |
| 9017 | } |
| 9018 | |
| 9019 | void sendStylusEvent(int32_t action) { |
| 9020 | NotifyMotionArgs motionArgs = |
| 9021 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9022 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
| 9023 | motionArgs.pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 9024 | mDispatcher->notifyMotion(&motionArgs); |
| 9025 | } |
| 9026 | }; |
| 9027 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9028 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9029 | |
| 9030 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 9031 | ScopedSilentDeath _silentDeath; |
| 9032 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9033 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9034 | overlay->setTrustedOverlay(false); |
| 9035 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9036 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9037 | ".* not a trusted overlay"); |
| 9038 | } |
| 9039 | |
| 9040 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9041 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9042 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9043 | |
| 9044 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9045 | overlay->consumeMotionDown(); |
| 9046 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9047 | overlay->consumeMotionUp(); |
| 9048 | |
| 9049 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9050 | window->consumeMotionDown(); |
| 9051 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9052 | window->consumeMotionUp(); |
| 9053 | |
| 9054 | overlay->assertNoEvents(); |
| 9055 | window->assertNoEvents(); |
| 9056 | } |
| 9057 | |
| 9058 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9059 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9060 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9061 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9062 | |
| 9063 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9064 | overlay->consumeMotionDown(); |
| 9065 | window->consumeMotionDown(); |
| 9066 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9067 | overlay->consumeMotionUp(); |
| 9068 | window->consumeMotionUp(); |
| 9069 | |
| 9070 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9071 | window->consumeMotionDown(); |
| 9072 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9073 | window->consumeMotionUp(); |
| 9074 | |
| 9075 | overlay->assertNoEvents(); |
| 9076 | window->assertNoEvents(); |
| 9077 | } |
| 9078 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9079 | /** |
| 9080 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9081 | * The scenario is as follows: |
| 9082 | * - The stylus interceptor overlay is configured as a spy window. |
| 9083 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9084 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9085 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9086 | */ |
| 9087 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9088 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9089 | overlay->setSpy(true); |
| 9090 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9091 | |
| 9092 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9093 | overlay->consumeMotionDown(); |
| 9094 | window->consumeMotionDown(); |
| 9095 | |
| 9096 | // The interceptor pilfers the pointers. |
| 9097 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9098 | window->consumeMotionCancel(); |
| 9099 | |
| 9100 | // The interceptor configures itself so that it is no longer a spy. |
| 9101 | overlay->setSpy(false); |
| 9102 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9103 | |
| 9104 | // It continues to receive the rest of the stylus gesture. |
| 9105 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9106 | overlay->consumeMotionMove(); |
| 9107 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9108 | overlay->consumeMotionUp(); |
| 9109 | |
| 9110 | window->assertNoEvents(); |
| 9111 | } |
| 9112 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9113 | struct User { |
| 9114 | int32_t mPid; |
| 9115 | int32_t mUid; |
| 9116 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9117 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9118 | |
| 9119 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 9120 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9121 | |
| 9122 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9123 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9124 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9125 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9126 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9127 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9128 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9129 | } |
| 9130 | |
| 9131 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9132 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9133 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9134 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9135 | mPolicyFlags); |
| 9136 | } |
| 9137 | |
| 9138 | sp<FakeWindowHandle> createWindow() const { |
| 9139 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9140 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9141 | sp<FakeWindowHandle> window = |
| 9142 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9143 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9144 | window->setOwnerInfo(mPid, mUid); |
| 9145 | return window; |
| 9146 | } |
| 9147 | }; |
| 9148 | |
| 9149 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9150 | |
| 9151 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 9152 | auto owner = User(mDispatcher, 10, 11); |
| 9153 | auto window = owner.createWindow(); |
| 9154 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9155 | |
| 9156 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9157 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9158 | window->consumeMotionDown(); |
| 9159 | |
| 9160 | setFocusedWindow(window); |
| 9161 | window->consumeFocusEvent(true); |
| 9162 | |
| 9163 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9164 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9165 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9166 | } |
| 9167 | |
| 9168 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 9169 | auto owner = User(mDispatcher, 10, 11); |
| 9170 | auto window = owner.createWindow(); |
| 9171 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9172 | |
| 9173 | auto rando = User(mDispatcher, 20, 21); |
| 9174 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9175 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9176 | |
| 9177 | setFocusedWindow(window); |
| 9178 | window->consumeFocusEvent(true); |
| 9179 | |
| 9180 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9181 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9182 | window->assertNoEvents(); |
| 9183 | } |
| 9184 | |
| 9185 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 9186 | auto owner = User(mDispatcher, 10, 11); |
| 9187 | auto window = owner.createWindow(); |
| 9188 | auto spy = owner.createWindow(); |
| 9189 | spy->setSpy(true); |
| 9190 | spy->setTrustedOverlay(true); |
| 9191 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9192 | |
| 9193 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9194 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9195 | spy->consumeMotionDown(); |
| 9196 | window->consumeMotionDown(); |
| 9197 | } |
| 9198 | |
| 9199 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 9200 | auto owner = User(mDispatcher, 10, 11); |
| 9201 | auto window = owner.createWindow(); |
| 9202 | |
| 9203 | auto rando = User(mDispatcher, 20, 21); |
| 9204 | auto randosSpy = rando.createWindow(); |
| 9205 | randosSpy->setSpy(true); |
| 9206 | randosSpy->setTrustedOverlay(true); |
| 9207 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9208 | |
| 9209 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9210 | // not receive the event. |
| 9211 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9212 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9213 | randosSpy->assertNoEvents(); |
| 9214 | window->consumeMotionDown(); |
| 9215 | } |
| 9216 | |
| 9217 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 9218 | auto owner = User(mDispatcher, 10, 11); |
| 9219 | auto window = owner.createWindow(); |
| 9220 | |
| 9221 | auto rando = User(mDispatcher, 20, 21); |
| 9222 | auto randosSpy = rando.createWindow(); |
| 9223 | randosSpy->setSpy(true); |
| 9224 | randosSpy->setTrustedOverlay(true); |
| 9225 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9226 | |
| 9227 | // A user that has injection permission can inject into any window. |
| 9228 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9229 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9230 | ADISPLAY_ID_DEFAULT)); |
| 9231 | randosSpy->consumeMotionDown(); |
| 9232 | window->consumeMotionDown(); |
| 9233 | |
| 9234 | setFocusedWindow(randosSpy); |
| 9235 | randosSpy->consumeFocusEvent(true); |
| 9236 | |
| 9237 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9238 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9239 | window->assertNoEvents(); |
| 9240 | } |
| 9241 | |
| 9242 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 9243 | auto owner = User(mDispatcher, 10, 11); |
| 9244 | auto window = owner.createWindow(); |
| 9245 | |
| 9246 | auto rando = User(mDispatcher, 20, 21); |
| 9247 | auto randosWindow = rando.createWindow(); |
| 9248 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9249 | randosWindow->setWatchOutsideTouch(true); |
| 9250 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9251 | |
| 9252 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 9253 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9254 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9255 | window->consumeMotionDown(); |
| 9256 | randosWindow->consumeMotionOutside(); |
| 9257 | } |
| 9258 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9259 | } // namespace android::inputdispatcher |