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 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 108 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 109 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 110 | struct PointF { |
| 111 | float x; |
| 112 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 113 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 114 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 116 | /** |
| 117 | * Return a DOWN key event with KEYCODE_A. |
| 118 | */ |
| 119 | static KeyEvent getTestKeyEvent() { |
| 120 | KeyEvent event; |
| 121 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 122 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 123 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 124 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 125 | return event; |
| 126 | } |
| 127 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 128 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 129 | ASSERT_EQ(expectedAction, receivedAction) |
| 130 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 131 | << MotionEvent::actionToString(receivedAction); |
| 132 | } |
| 133 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 134 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 135 | bool matches = action == arg.getAction(); |
| 136 | if (!matches) { |
| 137 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 138 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 139 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 140 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 141 | if (!matches) { |
| 142 | *result_listener << "; "; |
| 143 | } |
| 144 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 145 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 146 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 147 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 148 | if (!matches) { |
| 149 | *result_listener << "; "; |
| 150 | } |
| 151 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 152 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 153 | } |
| 154 | return matches; |
| 155 | } |
| 156 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 157 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 158 | return arg.getDownTime() == downTime; |
| 159 | } |
| 160 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 161 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { |
| 162 | return arg.getDisplayId() == displayId; |
| 163 | } |
| 164 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 165 | MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") { |
| 166 | return arg.getDeviceId() == deviceId; |
| 167 | } |
| 168 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 169 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 170 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 171 | << inputEventSourceToString(arg.getSource()); |
| 172 | return arg.getSource() == source; |
| 173 | } |
| 174 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 175 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { |
| 176 | return arg.getFlags() == flags; |
| 177 | } |
| 178 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 179 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 180 | if (arg.getPointerCount() != 1) { |
| 181 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 182 | return false; |
| 183 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 184 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 187 | MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") { |
| 188 | return arg.getPointerCount() == pointerCount; |
| 189 | } |
| 190 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 191 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 192 | // Build a map for the received pointers, by pointer id |
| 193 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 194 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 195 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 196 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 197 | } |
| 198 | return pointers == actualPointers; |
| 199 | } |
| 200 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 201 | // --- FakeInputDispatcherPolicy --- |
| 202 | |
| 203 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 204 | InputDispatcherConfiguration mConfig; |
| 205 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 206 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 207 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 209 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | |
| 211 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 212 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 213 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 214 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 215 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 216 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 217 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 218 | |
| 219 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 220 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 221 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 222 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 223 | } |
| 224 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 225 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 226 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 227 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 228 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 229 | |
| 230 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 231 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 232 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 233 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 234 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 235 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 236 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 237 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 238 | } |
| 239 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 240 | void assertFilterInputEventWasNotCalled() { |
| 241 | std::scoped_lock lock(mLock); |
| 242 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 243 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 245 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 246 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 247 | ASSERT_TRUE(mConfigurationChangedTime) |
| 248 | << "Timed out waiting for configuration changed call"; |
| 249 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 250 | mConfigurationChangedTime = std::nullopt; |
| 251 | } |
| 252 | |
| 253 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 254 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 255 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 256 | // 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] | 257 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 258 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 259 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 260 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 261 | mLastNotifySwitch = std::nullopt; |
| 262 | } |
| 263 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 264 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 265 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 266 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 267 | mOnPointerDownToken.clear(); |
| 268 | } |
| 269 | |
| 270 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 271 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 272 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 273 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 276 | // This function must be called soon after the expected ANR timer starts, |
| 277 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 278 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 279 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 280 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 281 | std::unique_lock lock(mLock); |
| 282 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 283 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 284 | ASSERT_NO_FATAL_FAILURE( |
| 285 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 286 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 289 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 290 | const sp<WindowInfoHandle>& window) { |
| 291 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 292 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 293 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 294 | } |
| 295 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 296 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 297 | const sp<IBinder>& expectedToken, |
| 298 | int32_t expectedPid) { |
| 299 | std::unique_lock lock(mLock); |
| 300 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 301 | AnrResult result; |
| 302 | ASSERT_NO_FATAL_FAILURE(result = |
| 303 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 304 | const auto& [token, pid] = result; |
| 305 | ASSERT_EQ(expectedToken, token); |
| 306 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 307 | } |
| 308 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 309 | /** 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] | 310 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 311 | std::unique_lock lock(mLock); |
| 312 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 313 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 314 | const auto& [token, _] = result; |
| 315 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 318 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 319 | int32_t expectedPid) { |
| 320 | std::unique_lock lock(mLock); |
| 321 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 322 | AnrResult result; |
| 323 | ASSERT_NO_FATAL_FAILURE( |
| 324 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 325 | const auto& [token, pid] = result; |
| 326 | ASSERT_EQ(expectedToken, token); |
| 327 | ASSERT_EQ(expectedPid, pid); |
| 328 | } |
| 329 | |
| 330 | /** 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] | 331 | sp<IBinder> getResponsiveWindowToken() { |
| 332 | std::unique_lock lock(mLock); |
| 333 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 334 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 335 | const auto& [token, _] = result; |
| 336 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void assertNotifyAnrWasNotCalled() { |
| 340 | std::scoped_lock lock(mLock); |
| 341 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 342 | ASSERT_TRUE(mAnrWindows.empty()); |
| 343 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 344 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 345 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 348 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 349 | mConfig.keyRepeatTimeout = timeout; |
| 350 | mConfig.keyRepeatDelay = delay; |
| 351 | } |
| 352 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 353 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 354 | std::unique_lock lock(mLock); |
| 355 | base::ScopedLockAssertion assumeLocked(mLock); |
| 356 | |
| 357 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 358 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 359 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 360 | enabled; |
| 361 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 362 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 363 | << ") to be called."; |
| 364 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 365 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 366 | auto request = *mPointerCaptureRequest; |
| 367 | mPointerCaptureRequest.reset(); |
| 368 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void assertSetPointerCaptureNotCalled() { |
| 372 | std::unique_lock lock(mLock); |
| 373 | base::ScopedLockAssertion assumeLocked(mLock); |
| 374 | |
| 375 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 376 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 377 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 378 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 379 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 380 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 381 | } |
| 382 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 383 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 384 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 385 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 386 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 387 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 388 | } |
| 389 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 390 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 391 | std::unique_lock lock(mLock); |
| 392 | base::ScopedLockAssertion assumeLocked(mLock); |
| 393 | std::optional<sp<IBinder>> receivedToken = |
| 394 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 395 | mNotifyInputChannelBroken); |
| 396 | ASSERT_TRUE(receivedToken.has_value()); |
| 397 | ASSERT_EQ(token, *receivedToken); |
| 398 | } |
| 399 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 400 | /** |
| 401 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 402 | */ |
| 403 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 404 | mInterceptKeyTimeout = timeout; |
| 405 | } |
| 406 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 407 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 408 | std::mutex mLock; |
| 409 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 410 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 411 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 412 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 413 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 414 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 415 | |
| 416 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 417 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 418 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 419 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 420 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 421 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 422 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 423 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 424 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 425 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 426 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 427 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 428 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 429 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 430 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 431 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 432 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 433 | // first entry from the container and erase it. |
| 434 | template <class T> |
| 435 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 436 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 437 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 438 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 439 | // before checking if ANR was called. |
| 440 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 441 | // to provide it some time to act. 100ms seems reasonable. |
| 442 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 443 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 444 | std::optional<T> token = |
| 445 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 446 | if (!token.has_value()) { |
| 447 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 448 | return {}; |
| 449 | } |
| 450 | |
| 451 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 452 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 453 | // the dispatcher started counting before this function was called |
| 454 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 455 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 456 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 457 | << "ms, but waited " |
| 458 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 459 | << "ms instead"; |
| 460 | } |
| 461 | return *token; |
| 462 | } |
| 463 | |
| 464 | template <class T> |
| 465 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 466 | std::queue<T>& storage, |
| 467 | std::unique_lock<std::mutex>& lock, |
| 468 | std::condition_variable& condition) |
| 469 | REQUIRES(mLock) { |
| 470 | condition.wait_for(lock, timeout, |
| 471 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 472 | if (storage.empty()) { |
| 473 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 474 | return std::nullopt; |
| 475 | } |
| 476 | T item = storage.front(); |
| 477 | storage.pop(); |
| 478 | return std::make_optional(item); |
| 479 | } |
| 480 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 481 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 482 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 483 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 486 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 487 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 488 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 489 | ASSERT_TRUE(pid.has_value()); |
| 490 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 491 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 492 | } |
| 493 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 494 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 495 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 496 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 497 | ASSERT_TRUE(pid.has_value()); |
| 498 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 499 | mNotifyAnr.notify_all(); |
| 500 | } |
| 501 | |
| 502 | void notifyNoFocusedWindowAnr( |
| 503 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 504 | std::scoped_lock lock(mLock); |
| 505 | mAnrApplications.push(applicationHandle); |
| 506 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 509 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 510 | std::scoped_lock lock(mLock); |
| 511 | mBrokenInputChannels.push(connectionToken); |
| 512 | mNotifyInputChannelBroken.notify_all(); |
| 513 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 514 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 515 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 516 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 517 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 518 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 519 | const std::vector<float>& values) override {} |
| 520 | |
| 521 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 522 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 523 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 524 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 525 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 526 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 527 | *outConfig = mConfig; |
| 528 | } |
| 529 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 530 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 531 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 532 | switch (inputEvent->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 533 | case InputEventType::KEY: { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 534 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 535 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 539 | case InputEventType::MOTION: { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 540 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 541 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 542 | break; |
| 543 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 544 | default: { |
| 545 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 546 | break; |
| 547 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 548 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 549 | return true; |
| 550 | } |
| 551 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 552 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { |
| 553 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { |
| 554 | // Clear intercept state when we handled the event. |
| 555 | mInterceptKeyTimeout = 0ms; |
| 556 | } |
| 557 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 558 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 559 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 560 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 561 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 562 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 563 | // Clear intercept state so we could dispatch the event in next wake. |
| 564 | mInterceptKeyTimeout = 0ms; |
| 565 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 568 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 572 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 573 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 574 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 575 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 576 | * essentially a passthrough for notifySwitch. |
| 577 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 578 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 581 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 583 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 584 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 585 | mOnPointerDownToken = newToken; |
| 586 | } |
| 587 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 588 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 589 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 590 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 591 | mPointerCaptureChangedCondition.notify_all(); |
| 592 | } |
| 593 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 594 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 595 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 596 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 597 | mDropTargetWindowToken = token; |
| 598 | } |
| 599 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 600 | void assertFilterInputEventWasCalledInternal( |
| 601 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 602 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 603 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 604 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 605 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 606 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 607 | }; |
| 608 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 609 | // --- InputDispatcherTest --- |
| 610 | |
| 611 | class InputDispatcherTest : public testing::Test { |
| 612 | protected: |
| 613 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 614 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 615 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 616 | void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 617 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 618 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 619 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 620 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 621 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 624 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 625 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 627 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 628 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 629 | |
| 630 | /** |
| 631 | * Used for debugging when writing the test |
| 632 | */ |
| 633 | void dumpDispatcherState() { |
| 634 | std::string dump; |
| 635 | mDispatcher->dump(dump); |
| 636 | std::stringstream ss(dump); |
| 637 | std::string to; |
| 638 | |
| 639 | while (std::getline(ss, to, '\n')) { |
| 640 | ALOGE("%s", to.c_str()); |
| 641 | } |
| 642 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 643 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 644 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 645 | FocusRequest request; |
| 646 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 647 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 648 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 649 | request.displayId = window->getInfo()->displayId; |
| 650 | mDispatcher->setFocusedWindow(request); |
| 651 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 652 | }; |
| 653 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 654 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 655 | KeyEvent event; |
| 656 | |
| 657 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 658 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 659 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 660 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 661 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 662 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 663 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 664 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 665 | << "Should reject key events with undefined action."; |
| 666 | |
| 667 | // 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] | 668 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 669 | 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] | 670 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 671 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 672 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 673 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 674 | << "Should reject key events with ACTION_MULTIPLE."; |
| 675 | } |
| 676 | |
| 677 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 678 | MotionEvent event; |
| 679 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 680 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 681 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 682 | pointerProperties[i].clear(); |
| 683 | pointerProperties[i].id = i; |
| 684 | pointerCoords[i].clear(); |
| 685 | } |
| 686 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 687 | // Some constants commonly used below |
| 688 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 689 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 690 | constexpr int32_t metaState = AMETA_NONE; |
| 691 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 692 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 693 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 694 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 695 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 696 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 697 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 698 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 699 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 700 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 701 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 702 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 703 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 704 | << "Should reject motion events with undefined action."; |
| 705 | |
| 706 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 707 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 708 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 709 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 710 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 711 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 712 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 713 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 714 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 715 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | << "Should reject motion events with pointer down index too large."; |
| 717 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 718 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 719 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 720 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 721 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 722 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 723 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 724 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 725 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 726 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 727 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | << "Should reject motion events with pointer down index too small."; |
| 729 | |
| 730 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 731 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 732 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 733 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 734 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 735 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 736 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 737 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 738 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 739 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 740 | << "Should reject motion events with pointer up index too large."; |
| 741 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 742 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 743 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 744 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 745 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 746 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 747 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 748 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 749 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 750 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 751 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | << "Should reject motion events with pointer up index too small."; |
| 753 | |
| 754 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 755 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 756 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 757 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 758 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 759 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 760 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 761 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 762 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 763 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 764 | << "Should reject motion events with 0 pointers."; |
| 765 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 766 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 767 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 768 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 769 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 770 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 771 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 772 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 773 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 774 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 776 | |
| 777 | // Rejects motion events with invalid pointer ids. |
| 778 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 779 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 780 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 781 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 782 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 783 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 784 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 785 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 786 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 787 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | << "Should reject motion events with pointer ids less than 0."; |
| 789 | |
| 790 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 791 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 792 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 793 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 794 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 795 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 796 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 797 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 798 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 799 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 800 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 801 | |
| 802 | // Rejects motion events with duplicate pointer ids. |
| 803 | pointerProperties[0].id = 1; |
| 804 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 805 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 806 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 807 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 808 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 809 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 810 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 811 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 812 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 813 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 814 | << "Should reject motion events with duplicate pointer ids."; |
| 815 | } |
| 816 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 817 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 818 | |
| 819 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 820 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 821 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 822 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 823 | |
| 824 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 825 | } |
| 826 | |
| 827 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 828 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 829 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 830 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 831 | |
| 832 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 833 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 834 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 835 | } |
| 836 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 837 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 838 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 839 | // Default input dispatching timeout if there is no focused application or paused window |
| 840 | // from which to determine an appropriate dispatching timeout. |
| 841 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 842 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 843 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 844 | |
| 845 | class FakeApplicationHandle : public InputApplicationHandle { |
| 846 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 847 | FakeApplicationHandle() { |
| 848 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 849 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 850 | mInfo.dispatchingTimeoutMillis = |
| 851 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 852 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 853 | virtual ~FakeApplicationHandle() {} |
| 854 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 855 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 856 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 857 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 858 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 859 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 860 | }; |
| 861 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 862 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 863 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 864 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 865 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 866 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 869 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 870 | InputEvent* event; |
| 871 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 872 | if (!consumeSeq) { |
| 873 | return nullptr; |
| 874 | } |
| 875 | finishEvent(*consumeSeq); |
| 876 | return event; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Receive an event without acknowledging it. |
| 881 | * Return the sequence number that could later be used to send finished signal. |
| 882 | */ |
| 883 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 884 | uint32_t consumeSeq; |
| 885 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 886 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 887 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 888 | status_t status = WOULD_BLOCK; |
| 889 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 890 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 891 | &event); |
| 892 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 893 | if (elapsed > 100ms) { |
| 894 | break; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | if (status == WOULD_BLOCK) { |
| 899 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 900 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | if (status != OK) { |
| 904 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 905 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 906 | } |
| 907 | if (event == nullptr) { |
| 908 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 909 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 910 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 911 | if (outEvent != nullptr) { |
| 912 | *outEvent = event; |
| 913 | } |
| 914 | return consumeSeq; |
| 915 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 916 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 917 | /** |
| 918 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 919 | */ |
| 920 | void finishEvent(uint32_t consumeSeq) { |
| 921 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 922 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 923 | } |
| 924 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 925 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 926 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 927 | ASSERT_EQ(OK, status); |
| 928 | } |
| 929 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 930 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 931 | std::optional<int32_t> expectedDisplayId, |
| 932 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 933 | InputEvent* event = consume(); |
| 934 | |
| 935 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 936 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 937 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 938 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 939 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 940 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 941 | if (expectedDisplayId.has_value()) { |
| 942 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 943 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 944 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 945 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 946 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 947 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 948 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 949 | if (expectedFlags.has_value()) { |
| 950 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 951 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 952 | break; |
| 953 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 954 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 955 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 956 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 957 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 958 | if (expectedFlags.has_value()) { |
| 959 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 960 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 961 | break; |
| 962 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 963 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 964 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 965 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 966 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 967 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 968 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 969 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 970 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 971 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 972 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 973 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 974 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 975 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 976 | } |
| 977 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 978 | MotionEvent* consumeMotion() { |
| 979 | InputEvent* event = consume(); |
| 980 | |
| 981 | if (event == nullptr) { |
| 982 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 983 | return nullptr; |
| 984 | } |
| 985 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 986 | if (event->getType() != InputEventType::MOTION) { |
| 987 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 988 | return nullptr; |
| 989 | } |
| 990 | return static_cast<MotionEvent*>(event); |
| 991 | } |
| 992 | |
| 993 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 994 | MotionEvent* motionEvent = consumeMotion(); |
| 995 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 996 | ASSERT_THAT(*motionEvent, matcher); |
| 997 | } |
| 998 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 999 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 1000 | InputEvent* event = consume(); |
| 1001 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1002 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1003 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1004 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1005 | |
| 1006 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1007 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1008 | |
| 1009 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1010 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1013 | void consumeCaptureEvent(bool hasCapture) { |
| 1014 | const InputEvent* event = consume(); |
| 1015 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1016 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1017 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1018 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1019 | |
| 1020 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1021 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1022 | |
| 1023 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1024 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1025 | } |
| 1026 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1027 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1028 | const InputEvent* event = consume(); |
| 1029 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1030 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1031 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1032 | |
| 1033 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1034 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1035 | |
| 1036 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1037 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1038 | EXPECT_EQ(x, dragEvent.getX()); |
| 1039 | EXPECT_EQ(y, dragEvent.getY()); |
| 1040 | } |
| 1041 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1042 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1043 | const InputEvent* event = consume(); |
| 1044 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1045 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1046 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1047 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1048 | |
| 1049 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1050 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1051 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1052 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1053 | } |
| 1054 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1055 | void assertNoEvents() { |
| 1056 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1057 | if (event == nullptr) { |
| 1058 | return; |
| 1059 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1060 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1061 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1062 | ADD_FAILURE() << "Received key event " |
| 1063 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1064 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1065 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1066 | ADD_FAILURE() << "Received motion event " |
| 1067 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1068 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1069 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1070 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1071 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1072 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1073 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1074 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1075 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1076 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1077 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1078 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1079 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1080 | } |
| 1081 | FAIL() << mName.c_str() |
| 1082 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1086 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1087 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1088 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1089 | protected: |
| 1090 | std::unique_ptr<InputConsumer> mConsumer; |
| 1091 | PreallocatedInputEventFactory mEventFactory; |
| 1092 | |
| 1093 | std::string mName; |
| 1094 | }; |
| 1095 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1096 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1097 | public: |
| 1098 | static const int32_t WIDTH = 600; |
| 1099 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1100 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1101 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1102 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1103 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1104 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1105 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1106 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1107 | dispatcher->createInputChannel(name); |
| 1108 | token = (*channel)->getConnectionToken(); |
| 1109 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | inputApplicationHandle->updateInfo(); |
| 1113 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1114 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1115 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1116 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1117 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1118 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1119 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1120 | mInfo.frameLeft = 0; |
| 1121 | mInfo.frameTop = 0; |
| 1122 | mInfo.frameRight = WIDTH; |
| 1123 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1124 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1125 | mInfo.globalScaleFactor = 1.0; |
| 1126 | mInfo.touchableRegion.clear(); |
| 1127 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1128 | mInfo.ownerPid = WINDOW_PID; |
| 1129 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1130 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1131 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1134 | sp<FakeWindowHandle> clone( |
| 1135 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1136 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1137 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1138 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1139 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1140 | return handle; |
| 1141 | } |
| 1142 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1143 | void setTouchable(bool touchable) { |
| 1144 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1145 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1146 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1147 | void setFocusable(bool focusable) { |
| 1148 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1149 | } |
| 1150 | |
| 1151 | void setVisible(bool visible) { |
| 1152 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1153 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1154 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1155 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1156 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1159 | void setPaused(bool paused) { |
| 1160 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1161 | } |
| 1162 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1163 | void setPreventSplitting(bool preventSplitting) { |
| 1164 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | void setSlippery(bool slippery) { |
| 1168 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1169 | } |
| 1170 | |
| 1171 | void setWatchOutsideTouch(bool watchOutside) { |
| 1172 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1173 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1174 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1175 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1176 | |
| 1177 | void setInterceptsStylus(bool interceptsStylus) { |
| 1178 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1179 | } |
| 1180 | |
| 1181 | void setDropInput(bool dropInput) { |
| 1182 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1183 | } |
| 1184 | |
| 1185 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1186 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1187 | } |
| 1188 | |
| 1189 | void setNoInputChannel(bool noInputChannel) { |
| 1190 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1191 | } |
| 1192 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1193 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1194 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1195 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1196 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1197 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1198 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1199 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1200 | mInfo.frameLeft = frame.left; |
| 1201 | mInfo.frameTop = frame.top; |
| 1202 | mInfo.frameRight = frame.right; |
| 1203 | mInfo.frameBottom = frame.bottom; |
| 1204 | mInfo.touchableRegion.clear(); |
| 1205 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1206 | |
| 1207 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1208 | ui::Transform translate; |
| 1209 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1210 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1213 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1214 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1215 | void setIsWallpaper(bool isWallpaper) { |
| 1216 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1217 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1218 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1219 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1220 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1221 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1222 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1223 | void setTrustedOverlay(bool trustedOverlay) { |
| 1224 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1225 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1226 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1227 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1228 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1229 | } |
| 1230 | |
| 1231 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1232 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1233 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1234 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1235 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1236 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1239 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1240 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1243 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1244 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1245 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1246 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1250 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1251 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1252 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1256 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1257 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1258 | } |
| 1259 | |
| 1260 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1261 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1262 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1263 | expectedFlags); |
| 1264 | } |
| 1265 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1266 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1267 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1268 | int32_t expectedFlags = 0) { |
| 1269 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1270 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1271 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1275 | int32_t expectedFlags = 0) { |
| 1276 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1277 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1278 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1282 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1283 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1284 | expectedFlags); |
| 1285 | } |
| 1286 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1287 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1288 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1289 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1290 | expectedFlags); |
| 1291 | } |
| 1292 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1293 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1294 | int32_t expectedFlags = 0) { |
| 1295 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1296 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1297 | ASSERT_EQ(InputEventType::MOTION, event->getType()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1298 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1299 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1300 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1301 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1302 | } |
| 1303 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1304 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1305 | ASSERT_NE(mInputReceiver, nullptr) |
| 1306 | << "Cannot consume events from a window with no receiver"; |
| 1307 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1308 | } |
| 1309 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1310 | void consumeCaptureEvent(bool hasCapture) { |
| 1311 | ASSERT_NE(mInputReceiver, nullptr) |
| 1312 | << "Cannot consume events from a window with no receiver"; |
| 1313 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1314 | } |
| 1315 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1316 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1317 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1318 | 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] | 1319 | ASSERT_THAT(*motionEvent, matcher); |
| 1320 | } |
| 1321 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1322 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1323 | std::optional<int32_t> expectedDisplayId, |
| 1324 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1325 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1326 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1327 | expectedFlags); |
| 1328 | } |
| 1329 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1330 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1331 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1332 | } |
| 1333 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1334 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1335 | ASSERT_NE(mInputReceiver, nullptr) |
| 1336 | << "Cannot consume events from a window with no receiver"; |
| 1337 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1338 | } |
| 1339 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1340 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1341 | if (mInputReceiver == nullptr) { |
| 1342 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1343 | return std::nullopt; |
| 1344 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1345 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | void finishEvent(uint32_t sequenceNum) { |
| 1349 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1350 | mInputReceiver->finishEvent(sequenceNum); |
| 1351 | } |
| 1352 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1353 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1354 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1355 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1356 | } |
| 1357 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1358 | InputEvent* consume() { |
| 1359 | if (mInputReceiver == nullptr) { |
| 1360 | return nullptr; |
| 1361 | } |
| 1362 | return mInputReceiver->consume(); |
| 1363 | } |
| 1364 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1365 | MotionEvent* consumeMotion() { |
| 1366 | InputEvent* event = consume(); |
| 1367 | if (event == nullptr) { |
| 1368 | ADD_FAILURE() << "Consume failed : no event"; |
| 1369 | return nullptr; |
| 1370 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1371 | if (event->getType() != InputEventType::MOTION) { |
| 1372 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1373 | return nullptr; |
| 1374 | } |
| 1375 | return static_cast<MotionEvent*>(event); |
| 1376 | } |
| 1377 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1378 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1379 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1380 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1381 | return; // Can't receive events if the window does not have input channel |
| 1382 | } |
| 1383 | ASSERT_NE(nullptr, mInputReceiver) |
| 1384 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1385 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1386 | } |
| 1387 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1388 | sp<IBinder> getToken() { return mInfo.token; } |
| 1389 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1390 | const std::string& getName() { return mName; } |
| 1391 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1392 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1393 | mInfo.ownerPid = ownerPid; |
| 1394 | mInfo.ownerUid = ownerUid; |
| 1395 | } |
| 1396 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1397 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1398 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1399 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1400 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1401 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1402 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1403 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1404 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1405 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1406 | 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] | 1407 | }; |
| 1408 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1409 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1410 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1411 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1412 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1413 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1414 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1415 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1416 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1417 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1418 | KeyEvent event; |
| 1419 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1420 | |
| 1421 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1422 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1423 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1424 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1425 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1426 | if (!allowKeyRepeat) { |
| 1427 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1428 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1429 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1430 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1433 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1434 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1435 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1438 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1439 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1440 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1441 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1442 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1443 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1444 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1445 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1448 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1449 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1450 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1453 | class PointerBuilder { |
| 1454 | public: |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1455 | PointerBuilder(int32_t id, ToolType toolType) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1456 | mProperties.clear(); |
| 1457 | mProperties.id = id; |
| 1458 | mProperties.toolType = toolType; |
| 1459 | mCoords.clear(); |
| 1460 | } |
| 1461 | |
| 1462 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1463 | |
| 1464 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1465 | |
| 1466 | PointerBuilder& axis(int32_t axis, float value) { |
| 1467 | mCoords.setAxisValue(axis, value); |
| 1468 | return *this; |
| 1469 | } |
| 1470 | |
| 1471 | PointerProperties buildProperties() const { return mProperties; } |
| 1472 | |
| 1473 | PointerCoords buildCoords() const { return mCoords; } |
| 1474 | |
| 1475 | private: |
| 1476 | PointerProperties mProperties; |
| 1477 | PointerCoords mCoords; |
| 1478 | }; |
| 1479 | |
| 1480 | class MotionEventBuilder { |
| 1481 | public: |
| 1482 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1483 | mAction = action; |
| 1484 | mSource = source; |
| 1485 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1486 | mDownTime = mEventTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1489 | MotionEventBuilder& deviceId(int32_t deviceId) { |
| 1490 | mDeviceId = deviceId; |
| 1491 | return *this; |
| 1492 | } |
| 1493 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1494 | MotionEventBuilder& downTime(nsecs_t downTime) { |
| 1495 | mDownTime = downTime; |
| 1496 | return *this; |
| 1497 | } |
| 1498 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1499 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1500 | mEventTime = eventTime; |
| 1501 | return *this; |
| 1502 | } |
| 1503 | |
| 1504 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1505 | mDisplayId = displayId; |
| 1506 | return *this; |
| 1507 | } |
| 1508 | |
| 1509 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1510 | mActionButton = actionButton; |
| 1511 | return *this; |
| 1512 | } |
| 1513 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1514 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1515 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1516 | return *this; |
| 1517 | } |
| 1518 | |
| 1519 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1520 | mRawXCursorPosition = rawXCursorPosition; |
| 1521 | return *this; |
| 1522 | } |
| 1523 | |
| 1524 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1525 | mRawYCursorPosition = rawYCursorPosition; |
| 1526 | return *this; |
| 1527 | } |
| 1528 | |
| 1529 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1530 | mPointers.push_back(pointer); |
| 1531 | return *this; |
| 1532 | } |
| 1533 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1534 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1535 | mFlags |= flags; |
| 1536 | return *this; |
| 1537 | } |
| 1538 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1539 | MotionEvent build() { |
| 1540 | std::vector<PointerProperties> pointerProperties; |
| 1541 | std::vector<PointerCoords> pointerCoords; |
| 1542 | for (const PointerBuilder& pointer : mPointers) { |
| 1543 | pointerProperties.push_back(pointer.buildProperties()); |
| 1544 | pointerCoords.push_back(pointer.buildCoords()); |
| 1545 | } |
| 1546 | |
| 1547 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1548 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1549 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1550 | mPointers.size() == 1) { |
| 1551 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1552 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1553 | } |
| 1554 | |
| 1555 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1556 | ui::Transform identityTransform; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1557 | event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1558 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1559 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1560 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1561 | mRawYCursorPosition, identityTransform, mDownTime, mEventTime, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1562 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1563 | |
| 1564 | return event; |
| 1565 | } |
| 1566 | |
| 1567 | private: |
| 1568 | int32_t mAction; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1569 | int32_t mDeviceId = DEVICE_ID; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1570 | int32_t mSource; |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1571 | nsecs_t mDownTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1572 | nsecs_t mEventTime; |
| 1573 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1574 | int32_t mActionButton{0}; |
| 1575 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1576 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1577 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1578 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1579 | |
| 1580 | std::vector<PointerBuilder> mPointers; |
| 1581 | }; |
| 1582 | |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1583 | class MotionArgsBuilder { |
| 1584 | public: |
| 1585 | MotionArgsBuilder(int32_t action, int32_t source) { |
| 1586 | mAction = action; |
| 1587 | mSource = source; |
| 1588 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1589 | mDownTime = mEventTime; |
| 1590 | } |
| 1591 | |
| 1592 | MotionArgsBuilder& deviceId(int32_t deviceId) { |
| 1593 | mDeviceId = deviceId; |
| 1594 | return *this; |
| 1595 | } |
| 1596 | |
| 1597 | MotionArgsBuilder& downTime(nsecs_t downTime) { |
| 1598 | mDownTime = downTime; |
| 1599 | return *this; |
| 1600 | } |
| 1601 | |
| 1602 | MotionArgsBuilder& eventTime(nsecs_t eventTime) { |
| 1603 | mEventTime = eventTime; |
| 1604 | return *this; |
| 1605 | } |
| 1606 | |
| 1607 | MotionArgsBuilder& displayId(int32_t displayId) { |
| 1608 | mDisplayId = displayId; |
| 1609 | return *this; |
| 1610 | } |
| 1611 | |
| 1612 | MotionArgsBuilder& policyFlags(int32_t policyFlags) { |
| 1613 | mPolicyFlags = policyFlags; |
| 1614 | return *this; |
| 1615 | } |
| 1616 | |
| 1617 | MotionArgsBuilder& actionButton(int32_t actionButton) { |
| 1618 | mActionButton = actionButton; |
| 1619 | return *this; |
| 1620 | } |
| 1621 | |
| 1622 | MotionArgsBuilder& buttonState(int32_t buttonState) { |
| 1623 | mButtonState = buttonState; |
| 1624 | return *this; |
| 1625 | } |
| 1626 | |
| 1627 | MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1628 | mRawXCursorPosition = rawXCursorPosition; |
| 1629 | return *this; |
| 1630 | } |
| 1631 | |
| 1632 | MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1633 | mRawYCursorPosition = rawYCursorPosition; |
| 1634 | return *this; |
| 1635 | } |
| 1636 | |
| 1637 | MotionArgsBuilder& pointer(PointerBuilder pointer) { |
| 1638 | mPointers.push_back(pointer); |
| 1639 | return *this; |
| 1640 | } |
| 1641 | |
| 1642 | MotionArgsBuilder& addFlag(uint32_t flags) { |
| 1643 | mFlags |= flags; |
| 1644 | return *this; |
| 1645 | } |
| 1646 | |
| 1647 | NotifyMotionArgs build() { |
| 1648 | std::vector<PointerProperties> pointerProperties; |
| 1649 | std::vector<PointerCoords> pointerCoords; |
| 1650 | for (const PointerBuilder& pointer : mPointers) { |
| 1651 | pointerProperties.push_back(pointer.buildProperties()); |
| 1652 | pointerCoords.push_back(pointer.buildCoords()); |
| 1653 | } |
| 1654 | |
| 1655 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1656 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1657 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1658 | mPointers.size() == 1) { |
| 1659 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1660 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1661 | } |
| 1662 | |
| 1663 | NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId, |
| 1664 | mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags, |
| 1665 | AMETA_NONE, mButtonState, MotionClassification::NONE, /*edgeFlags=*/0, |
| 1666 | mPointers.size(), pointerProperties.data(), pointerCoords.data(), |
| 1667 | /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition, |
| 1668 | mRawYCursorPosition, mDownTime, /*videoFrames=*/{}); |
| 1669 | |
| 1670 | return args; |
| 1671 | } |
| 1672 | |
| 1673 | private: |
| 1674 | int32_t mAction; |
| 1675 | int32_t mDeviceId = DEVICE_ID; |
| 1676 | int32_t mSource; |
| 1677 | nsecs_t mDownTime; |
| 1678 | nsecs_t mEventTime; |
| 1679 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1680 | int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; |
| 1681 | int32_t mActionButton{0}; |
| 1682 | int32_t mButtonState{0}; |
| 1683 | int32_t mFlags{0}; |
| 1684 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1685 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1686 | |
| 1687 | std::vector<PointerBuilder> mPointers; |
| 1688 | }; |
| 1689 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1690 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1691 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1692 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1693 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1694 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1695 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1696 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1697 | } |
| 1698 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1699 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1700 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1701 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1702 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1703 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1704 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1705 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1706 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1707 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1708 | MotionEvent event = MotionEventBuilder(action, source) |
| 1709 | .displayId(displayId) |
| 1710 | .eventTime(eventTime) |
| 1711 | .rawXCursorPosition(cursorPosition.x) |
| 1712 | .rawYCursorPosition(cursorPosition.y) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1713 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1714 | .x(position.x) |
| 1715 | .y(position.y)) |
| 1716 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1717 | |
| 1718 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1719 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, |
| 1720 | policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1721 | } |
| 1722 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1723 | static InputEventInjectionResult injectMotionDown( |
| 1724 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1725 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1726 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1729 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1730 | int32_t source, int32_t displayId, |
| 1731 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1732 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1735 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1736 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1737 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1738 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1739 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1740 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1741 | |
| 1742 | return args; |
| 1743 | } |
| 1744 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1745 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1746 | int32_t displayId, |
| 1747 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1748 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1749 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1750 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1751 | } |
| 1752 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1753 | PointerProperties pointerProperties[pointerCount]; |
| 1754 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1755 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1756 | for (size_t i = 0; i < pointerCount; i++) { |
| 1757 | pointerProperties[i].clear(); |
| 1758 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1759 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1760 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1761 | pointerCoords[i].clear(); |
| 1762 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1763 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1764 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1765 | |
| 1766 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1767 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1768 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1769 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1770 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1771 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1772 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1773 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1774 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1775 | |
| 1776 | return args; |
| 1777 | } |
| 1778 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1779 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1780 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1781 | } |
| 1782 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1783 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1784 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1785 | } |
| 1786 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1787 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1788 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1789 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1792 | /** |
| 1793 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1794 | * broken channel. |
| 1795 | */ |
| 1796 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1797 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1798 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1799 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1800 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1801 | |
| 1802 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1803 | |
| 1804 | // Window closes its channel, but the window remains. |
| 1805 | window->destroyReceiver(); |
| 1806 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1807 | } |
| 1808 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1809 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1810 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1811 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1812 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1813 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1814 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1816 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1817 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1818 | |
| 1819 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1820 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1821 | } |
| 1822 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1823 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1824 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1825 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1826 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1827 | |
| 1828 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1829 | // Inject a MotionEvent to an unknown display. |
| 1830 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1831 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1832 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1833 | |
| 1834 | // Window should receive motion event. |
| 1835 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1836 | } |
| 1837 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1838 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1839 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1840 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1841 | * called twice. |
| 1842 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1843 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1844 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1845 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1846 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1847 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1848 | |
| 1849 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1850 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1851 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1852 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1853 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1854 | |
| 1855 | // Window should receive motion event. |
| 1856 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1857 | } |
| 1858 | |
| 1859 | /** |
| 1860 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1861 | */ |
| 1862 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1863 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1864 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1865 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1866 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1867 | |
| 1868 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1869 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1871 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1872 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1873 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1874 | |
| 1875 | // Window should receive motion event. |
| 1876 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1877 | } |
| 1878 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1879 | // The foreground window should receive the first touch down event. |
| 1880 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1881 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1882 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1883 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1884 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1885 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1886 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1887 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1889 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1890 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1891 | |
| 1892 | // 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] | 1893 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1894 | windowSecond->assertNoEvents(); |
| 1895 | } |
| 1896 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1897 | /** |
| 1898 | * Two windows: A top window, and a wallpaper behind the window. |
| 1899 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1900 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1901 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1902 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1903 | */ |
| 1904 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1905 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1906 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1907 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1908 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1909 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1910 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1911 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1912 | |
| 1913 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1914 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1915 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1916 | {100, 200})) |
| 1917 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1918 | |
| 1919 | // Both foreground window and its wallpaper should receive the touch down |
| 1920 | foregroundWindow->consumeMotionDown(); |
| 1921 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1922 | |
| 1923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1924 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1925 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1926 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1927 | |
| 1928 | foregroundWindow->consumeMotionMove(); |
| 1929 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1930 | |
| 1931 | // Now the foreground window goes away, but the wallpaper stays |
| 1932 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1933 | foregroundWindow->consumeMotionCancel(); |
| 1934 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1935 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1936 | } |
| 1937 | |
| 1938 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1939 | * Two fingers down on the window, and lift off the first finger. |
| 1940 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1941 | * contains a single pointer. |
| 1942 | */ |
| 1943 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1944 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1945 | sp<FakeWindowHandle> window = |
| 1946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1947 | |
| 1948 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1949 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1950 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1951 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1952 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1953 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1954 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1955 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1956 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1957 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1958 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1959 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1960 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1961 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1962 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1963 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1964 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1965 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1966 | |
| 1967 | // Remove the window. The gesture should be canceled |
| 1968 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 1969 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1970 | window->consumeMotionEvent( |
| 1971 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1972 | } |
| 1973 | |
| 1974 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1975 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1976 | * with the following differences: |
| 1977 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1978 | * clean up the connection. |
| 1979 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1980 | * Ensure that there's no crash in the dispatcher. |
| 1981 | */ |
| 1982 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1983 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1984 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1985 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1986 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1987 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1988 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1989 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1990 | |
| 1991 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1992 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1993 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1994 | {100, 200})) |
| 1995 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1996 | |
| 1997 | // Both foreground window and its wallpaper should receive the touch down |
| 1998 | foregroundWindow->consumeMotionDown(); |
| 1999 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2000 | |
| 2001 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2002 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2003 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 2004 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2005 | |
| 2006 | foregroundWindow->consumeMotionMove(); |
| 2007 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2008 | |
| 2009 | // Wallpaper closes its channel, but the window remains. |
| 2010 | wallpaperWindow->destroyReceiver(); |
| 2011 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 2012 | |
| 2013 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 2014 | // is no longer valid. |
| 2015 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 2016 | foregroundWindow->consumeMotionCancel(); |
| 2017 | } |
| 2018 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2019 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 2020 | public ::testing::WithParamInterface<bool> {}; |
| 2021 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 2022 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 2023 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2024 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 2025 | * The top window gets a multitouch gesture. |
| 2026 | * Ensure that wallpaper gets the same gesture. |
| 2027 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2028 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2029 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2030 | sp<FakeWindowHandle> foregroundWindow = |
| 2031 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 2032 | foregroundWindow->setDupTouchToWallpaper(true); |
| 2033 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2034 | |
| 2035 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2036 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2037 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2038 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2039 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2040 | |
| 2041 | // Touch down on top window |
| 2042 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2043 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2044 | {100, 100})) |
| 2045 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2046 | |
| 2047 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2048 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2049 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2050 | |
| 2051 | // Second finger down on the top window |
| 2052 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2053 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2054 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2055 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2056 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2057 | .build(); |
| 2058 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2059 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2060 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2061 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2062 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2063 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 2064 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2065 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2066 | |
| 2067 | const MotionEvent secondFingerUpEvent = |
| 2068 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2069 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2070 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2071 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2072 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2073 | .build(); |
| 2074 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2075 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 2076 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2077 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2078 | foregroundWindow->consumeMotionPointerUp(0); |
| 2079 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2080 | |
| 2081 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2082 | injectMotionEvent(mDispatcher, |
| 2083 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2084 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2085 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2086 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2087 | .pointer(PointerBuilder(/* id */ 1, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2088 | ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2089 | .x(100) |
| 2090 | .y(100)) |
| 2091 | .build(), |
| 2092 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2093 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2094 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2095 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | /** |
| 2099 | * Two windows: a window on the left and window on the right. |
| 2100 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 2101 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 2102 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 2103 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 2104 | * ACTION_POINTER_DOWN(1). |
| 2105 | */ |
| 2106 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 2107 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2108 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2109 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2110 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2111 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2112 | |
| 2113 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2114 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2115 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2116 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2117 | |
| 2118 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2119 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2120 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2121 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2122 | |
| 2123 | mDispatcher->setInputWindows( |
| 2124 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 2125 | |
| 2126 | // Touch down on left window |
| 2127 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2128 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2129 | {100, 100})) |
| 2130 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2131 | |
| 2132 | // Both foreground window and its wallpaper should receive the touch down |
| 2133 | leftWindow->consumeMotionDown(); |
| 2134 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2135 | |
| 2136 | // Second finger down on the right window |
| 2137 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2138 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2139 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2140 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2141 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2142 | .build(); |
| 2143 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2144 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2145 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2146 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2147 | |
| 2148 | leftWindow->consumeMotionMove(); |
| 2149 | // Since the touch is split, right window gets ACTION_DOWN |
| 2150 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2151 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2152 | expectedWallpaperFlags); |
| 2153 | |
| 2154 | // Now, leftWindow, which received the first finger, disappears. |
| 2155 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 2156 | leftWindow->consumeMotionCancel(); |
| 2157 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2158 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2159 | |
| 2160 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2161 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2162 | const MotionEvent secondFingerMoveEvent = |
| 2163 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2164 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2165 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2166 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2167 | .build(); |
| 2168 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2169 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2170 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2171 | rightWindow->consumeMotionMove(); |
| 2172 | |
| 2173 | leftWindow->assertNoEvents(); |
| 2174 | rightWindow->assertNoEvents(); |
| 2175 | wallpaperWindow->assertNoEvents(); |
| 2176 | } |
| 2177 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2178 | /** |
| 2179 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2180 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2181 | * The right window should receive ACTION_DOWN. |
| 2182 | */ |
| 2183 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2184 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2185 | sp<FakeWindowHandle> leftWindow = |
| 2186 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2187 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2188 | leftWindow->setDupTouchToWallpaper(true); |
| 2189 | leftWindow->setSlippery(true); |
| 2190 | |
| 2191 | sp<FakeWindowHandle> rightWindow = |
| 2192 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2193 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2194 | |
| 2195 | sp<FakeWindowHandle> wallpaperWindow = |
| 2196 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2197 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2198 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2199 | mDispatcher->setInputWindows( |
| 2200 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2201 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2202 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2204 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2205 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2206 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2207 | |
| 2208 | // Both foreground window and its wallpaper should receive the touch down |
| 2209 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2210 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2211 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2212 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2214 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2215 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2216 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2217 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2218 | leftWindow->consumeMotionCancel(); |
| 2219 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2220 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2223 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2224 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2225 | * interactive, it might stop sending this flag. |
| 2226 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2227 | * to have a consistent input stream. |
| 2228 | * |
| 2229 | * Test procedure: |
| 2230 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2231 | * DOWN (new gesture). |
| 2232 | * |
| 2233 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2234 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2235 | * |
| 2236 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2237 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2238 | */ |
| 2239 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2240 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2241 | sp<FakeWindowHandle> spyWindow = |
| 2242 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2243 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2244 | spyWindow->setTrustedOverlay(true); |
| 2245 | spyWindow->setSpy(true); |
| 2246 | |
| 2247 | sp<FakeWindowHandle> window = |
| 2248 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2249 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2250 | |
| 2251 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2252 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2253 | |
| 2254 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2255 | mDispatcher->notifyMotion( |
| 2256 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2257 | .deviceId(touchDeviceId) |
| 2258 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2259 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2260 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2261 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2262 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2263 | .deviceId(touchDeviceId) |
| 2264 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2265 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2266 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2267 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2268 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2269 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2270 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2271 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2272 | |
| 2273 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2274 | mDispatcher->notifyMotion( |
| 2275 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2276 | .deviceId(touchDeviceId) |
| 2277 | .policyFlags(0) |
| 2278 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2279 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2280 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2281 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2282 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2283 | |
| 2284 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2285 | // follows, so we keep it here to model the actual listener behaviour more closely. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2286 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2287 | |
| 2288 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2289 | mDispatcher->notifyMotion( |
| 2290 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2291 | .deviceId(touchDeviceId) |
| 2292 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2293 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2294 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2295 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2296 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2297 | |
| 2298 | // No more events |
| 2299 | spyWindow->assertNoEvents(); |
| 2300 | window->assertNoEvents(); |
| 2301 | } |
| 2302 | |
| 2303 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2304 | * Two windows: a window on the left and a window on the right. |
| 2305 | * Mouse is hovered from the right window into the left window. |
| 2306 | * Next, we tap on the left window, where the cursor was last seen. |
| 2307 | * The second tap is done onto the right window. |
| 2308 | * The mouse and tap are from two different devices. |
| 2309 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2310 | * explicitly helps during debugging. |
| 2311 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2312 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2313 | */ |
| 2314 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2315 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2316 | sp<FakeWindowHandle> leftWindow = |
| 2317 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2318 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2319 | |
| 2320 | sp<FakeWindowHandle> rightWindow = |
| 2321 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2322 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2323 | |
| 2324 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2325 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2326 | // stale. |
| 2327 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2328 | const int32_t mouseDeviceId = 6; |
| 2329 | const int32_t touchDeviceId = 4; |
| 2330 | // Move the cursor from right |
| 2331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2332 | injectMotionEvent(mDispatcher, |
| 2333 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2334 | AINPUT_SOURCE_MOUSE) |
| 2335 | .deviceId(mouseDeviceId) |
| 2336 | .downTime(baseTime + 10) |
| 2337 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2338 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2339 | .x(300) |
| 2340 | .y(100)) |
| 2341 | .build())); |
| 2342 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2343 | |
| 2344 | // .. to the left window |
| 2345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2346 | injectMotionEvent(mDispatcher, |
| 2347 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2348 | AINPUT_SOURCE_MOUSE) |
| 2349 | .deviceId(mouseDeviceId) |
| 2350 | .downTime(baseTime + 10) |
| 2351 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2352 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2353 | .x(110) |
| 2354 | .y(100)) |
| 2355 | .build())); |
| 2356 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2357 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2358 | // Now tap the left window |
| 2359 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2360 | injectMotionEvent(mDispatcher, |
| 2361 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2362 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2363 | .deviceId(touchDeviceId) |
| 2364 | .downTime(baseTime + 40) |
| 2365 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2366 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2367 | .x(100) |
| 2368 | .y(100)) |
| 2369 | .build())); |
| 2370 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2371 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2372 | |
| 2373 | // release tap |
| 2374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2375 | injectMotionEvent(mDispatcher, |
| 2376 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2377 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2378 | .deviceId(touchDeviceId) |
| 2379 | .downTime(baseTime + 40) |
| 2380 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2381 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2382 | .x(100) |
| 2383 | .y(100)) |
| 2384 | .build())); |
| 2385 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2386 | |
| 2387 | // Tap the window on the right |
| 2388 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2389 | injectMotionEvent(mDispatcher, |
| 2390 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2391 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2392 | .deviceId(touchDeviceId) |
| 2393 | .downTime(baseTime + 60) |
| 2394 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2395 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2396 | .x(300) |
| 2397 | .y(100)) |
| 2398 | .build())); |
| 2399 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2400 | |
| 2401 | // release tap |
| 2402 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2403 | injectMotionEvent(mDispatcher, |
| 2404 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2405 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2406 | .deviceId(touchDeviceId) |
| 2407 | .downTime(baseTime + 60) |
| 2408 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2409 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2410 | .x(300) |
| 2411 | .y(100)) |
| 2412 | .build())); |
| 2413 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2414 | |
| 2415 | // No more events |
| 2416 | leftWindow->assertNoEvents(); |
| 2417 | rightWindow->assertNoEvents(); |
| 2418 | } |
| 2419 | |
| 2420 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2421 | * Two windows: a window on the left and a window on the right. |
| 2422 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2423 | * down. Then, on the left window, also place second touch pointer down. |
| 2424 | * This test tries to reproduce a crash. |
| 2425 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2426 | */ |
| 2427 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { |
| 2428 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2429 | sp<FakeWindowHandle> leftWindow = |
| 2430 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2431 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2432 | |
| 2433 | sp<FakeWindowHandle> rightWindow = |
| 2434 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2435 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2436 | |
| 2437 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2438 | |
| 2439 | const int32_t touchDeviceId = 4; |
| 2440 | const int32_t mouseDeviceId = 6; |
| 2441 | NotifyMotionArgs args; |
| 2442 | |
| 2443 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2444 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2445 | .deviceId(mouseDeviceId) |
| 2446 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2447 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2448 | leftWindow->consumeMotionEvent( |
| 2449 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2450 | |
| 2451 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2452 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2453 | .deviceId(mouseDeviceId) |
| 2454 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2455 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2456 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2457 | |
| 2458 | leftWindow->consumeMotionEvent( |
| 2459 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2460 | leftWindow->consumeMotionEvent( |
| 2461 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2462 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2463 | mDispatcher->notifyMotion( |
| 2464 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2465 | .deviceId(mouseDeviceId) |
| 2466 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2467 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2468 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2469 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2470 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2471 | |
| 2472 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2473 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2474 | .deviceId(touchDeviceId) |
| 2475 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2476 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2477 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2478 | |
| 2479 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2480 | |
| 2481 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2482 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2483 | .deviceId(touchDeviceId) |
| 2484 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2485 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2486 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2487 | leftWindow->consumeMotionEvent( |
| 2488 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2489 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2490 | // current implementation. |
| 2491 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2492 | rightWindow->consumeMotionEvent( |
| 2493 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2494 | |
| 2495 | leftWindow->assertNoEvents(); |
| 2496 | rightWindow->assertNoEvents(); |
| 2497 | } |
| 2498 | |
| 2499 | /** |
| 2500 | * On a single window, use two different devices: mouse and touch. |
| 2501 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2502 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2503 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2504 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2505 | * represent a new gesture. |
| 2506 | */ |
| 2507 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { |
| 2508 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2509 | sp<FakeWindowHandle> window = |
| 2510 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2511 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2512 | |
| 2513 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2514 | |
| 2515 | const int32_t touchDeviceId = 4; |
| 2516 | const int32_t mouseDeviceId = 6; |
| 2517 | NotifyMotionArgs args; |
| 2518 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2519 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2520 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2521 | .deviceId(touchDeviceId) |
| 2522 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2523 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2524 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2525 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2526 | .deviceId(touchDeviceId) |
| 2527 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2528 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2529 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2530 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2531 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2532 | .deviceId(touchDeviceId) |
| 2533 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2534 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2535 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2536 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2537 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2538 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2539 | |
| 2540 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2541 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2542 | .deviceId(mouseDeviceId) |
| 2543 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2544 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2545 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2546 | |
| 2547 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2548 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2549 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2550 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2551 | mDispatcher->notifyMotion( |
| 2552 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2553 | .deviceId(mouseDeviceId) |
| 2554 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2555 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2556 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2557 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2558 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2559 | |
| 2560 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2561 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2562 | .deviceId(touchDeviceId) |
| 2563 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2564 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2565 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2566 | // The pointer_down event should be ignored |
| 2567 | window->assertNoEvents(); |
| 2568 | } |
| 2569 | |
| 2570 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2571 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 2572 | * the injected event. |
| 2573 | */ |
| 2574 | TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) { |
| 2575 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2576 | sp<FakeWindowHandle> window = |
| 2577 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2578 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2579 | |
| 2580 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2581 | |
| 2582 | const int32_t touchDeviceId = 4; |
| 2583 | NotifyMotionArgs args; |
| 2584 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 2585 | // completion. |
| 2586 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2587 | injectMotionEvent(mDispatcher, |
| 2588 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2589 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2590 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2591 | .x(50) |
| 2592 | .y(50)) |
| 2593 | .build())); |
| 2594 | window->consumeMotionEvent( |
| 2595 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2596 | |
| 2597 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 2598 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2599 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2600 | .deviceId(touchDeviceId) |
| 2601 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2602 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2603 | |
| 2604 | window->consumeMotionEvent( |
| 2605 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2606 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2607 | } |
| 2608 | |
| 2609 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2610 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2611 | * |
| 2612 | * Two windows: a window on the left and a window on the right. |
| 2613 | * Mouse is hovered over the left window. |
| 2614 | * Next, we tap on the left window, where the cursor was last seen. |
| 2615 | * |
| 2616 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2617 | * the left window. |
| 2618 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2619 | * window (first), and then another on the left window (second). |
| 2620 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2621 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2622 | */ |
| 2623 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2624 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2625 | sp<FakeWindowHandle> leftWindow = |
| 2626 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2627 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2628 | |
| 2629 | sp<FakeWindowHandle> rightWindow = |
| 2630 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2631 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2632 | |
| 2633 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2634 | |
| 2635 | const int32_t mouseDeviceId = 6; |
| 2636 | const int32_t touchDeviceId = 4; |
| 2637 | // Hover over the left window. Keep the cursor there. |
| 2638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2639 | injectMotionEvent(mDispatcher, |
| 2640 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2641 | AINPUT_SOURCE_MOUSE) |
| 2642 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2643 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2644 | .x(50) |
| 2645 | .y(50)) |
| 2646 | .build())); |
| 2647 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2648 | |
| 2649 | // Tap on left window |
| 2650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2651 | injectMotionEvent(mDispatcher, |
| 2652 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2653 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2654 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2655 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2656 | .x(100) |
| 2657 | .y(100)) |
| 2658 | .build())); |
| 2659 | |
| 2660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2661 | injectMotionEvent(mDispatcher, |
| 2662 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2663 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2664 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2665 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2666 | .x(100) |
| 2667 | .y(100)) |
| 2668 | .build())); |
| 2669 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2670 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2671 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2672 | |
| 2673 | // First finger down on right window |
| 2674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2675 | injectMotionEvent(mDispatcher, |
| 2676 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2677 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2678 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2679 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2680 | .x(300) |
| 2681 | .y(100)) |
| 2682 | .build())); |
| 2683 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2684 | |
| 2685 | // Second finger down on the left window |
| 2686 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2687 | injectMotionEvent(mDispatcher, |
| 2688 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2689 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2690 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2691 | .x(300) |
| 2692 | .y(100)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2693 | .pointer(PointerBuilder(1, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2694 | .x(100) |
| 2695 | .y(100)) |
| 2696 | .build())); |
| 2697 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2698 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2699 | |
| 2700 | // No more events |
| 2701 | leftWindow->assertNoEvents(); |
| 2702 | rightWindow->assertNoEvents(); |
| 2703 | } |
| 2704 | |
| 2705 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2706 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2707 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2708 | * touch is gone, stylus hovering should start working again. |
| 2709 | */ |
| 2710 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 2711 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2712 | sp<FakeWindowHandle> window = |
| 2713 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2714 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2715 | |
| 2716 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2717 | |
| 2718 | const int32_t stylusDeviceId = 5; |
| 2719 | const int32_t touchDeviceId = 4; |
| 2720 | // Start hovering with stylus |
| 2721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2722 | injectMotionEvent(mDispatcher, |
| 2723 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2724 | AINPUT_SOURCE_STYLUS) |
| 2725 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2726 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2727 | .x(50) |
| 2728 | .y(50)) |
| 2729 | .build())); |
| 2730 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2731 | |
| 2732 | // Finger down on the window |
| 2733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2734 | injectMotionEvent(mDispatcher, |
| 2735 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2736 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2737 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2738 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2739 | .x(100) |
| 2740 | .y(100)) |
| 2741 | .build())); |
| 2742 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2743 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2744 | |
| 2745 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2746 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2747 | injectMotionEvent(mDispatcher, |
| 2748 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2749 | AINPUT_SOURCE_STYLUS) |
| 2750 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2751 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2752 | .x(50) |
| 2753 | .y(50)) |
| 2754 | .build())); |
| 2755 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2756 | // is already down. |
| 2757 | |
| 2758 | // Lift up the finger |
| 2759 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2760 | injectMotionEvent(mDispatcher, |
| 2761 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2762 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2763 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2764 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2765 | .x(100) |
| 2766 | .y(100)) |
| 2767 | .build())); |
| 2768 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2769 | |
| 2770 | // Now that the touch is gone, stylus hovering should start working again |
| 2771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2772 | injectMotionEvent(mDispatcher, |
| 2773 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2774 | AINPUT_SOURCE_STYLUS) |
| 2775 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2776 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2777 | .x(50) |
| 2778 | .y(50)) |
| 2779 | .build())); |
| 2780 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2781 | // No more events |
| 2782 | window->assertNoEvents(); |
| 2783 | } |
| 2784 | |
| 2785 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2786 | * A spy window above a window with no input channel. |
| 2787 | * Start hovering with a stylus device, and then tap with it. |
| 2788 | * Ensure spy window receives the entire sequence. |
| 2789 | */ |
| 2790 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 2791 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2792 | sp<FakeWindowHandle> spyWindow = |
| 2793 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2794 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2795 | spyWindow->setTrustedOverlay(true); |
| 2796 | spyWindow->setSpy(true); |
| 2797 | sp<FakeWindowHandle> window = |
| 2798 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2799 | window->setNoInputChannel(true); |
| 2800 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2801 | |
| 2802 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2803 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2804 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2805 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2806 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2807 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2808 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2809 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2810 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2811 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2812 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2813 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2814 | |
| 2815 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2816 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2817 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2818 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2819 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2820 | |
| 2821 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2822 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 2823 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2824 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2825 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 2826 | |
| 2827 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2828 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2829 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2830 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2831 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2832 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2833 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2834 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2835 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2836 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2837 | |
| 2838 | // No more events |
| 2839 | spyWindow->assertNoEvents(); |
| 2840 | window->assertNoEvents(); |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 2845 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 2846 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 2847 | * While the mouse is down, new move events from the touch device should be ignored. |
| 2848 | */ |
| 2849 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 2850 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2851 | sp<FakeWindowHandle> spyWindow = |
| 2852 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2853 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2854 | spyWindow->setTrustedOverlay(true); |
| 2855 | spyWindow->setSpy(true); |
| 2856 | sp<FakeWindowHandle> window = |
| 2857 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2858 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2859 | |
| 2860 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2861 | |
| 2862 | const int32_t mouseDeviceId = 7; |
| 2863 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2864 | |
| 2865 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2866 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2867 | .deviceId(mouseDeviceId) |
| 2868 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2869 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2870 | spyWindow->consumeMotionEvent( |
| 2871 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2872 | window->consumeMotionEvent( |
| 2873 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2874 | |
| 2875 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2876 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2877 | .deviceId(touchDeviceId) |
| 2878 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 2879 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2880 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2881 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2882 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2883 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2884 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2885 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2886 | .deviceId(touchDeviceId) |
| 2887 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 2888 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2889 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2890 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2891 | |
| 2892 | // Pilfer the stream |
| 2893 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2894 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2895 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2896 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2897 | .deviceId(touchDeviceId) |
| 2898 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 2899 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2900 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2901 | |
| 2902 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2903 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2904 | .deviceId(mouseDeviceId) |
| 2905 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2906 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2907 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2908 | |
| 2909 | spyWindow->consumeMotionEvent( |
| 2910 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2911 | spyWindow->consumeMotionEvent( |
| 2912 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2913 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2914 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2915 | mDispatcher->notifyMotion( |
| 2916 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2917 | .deviceId(mouseDeviceId) |
| 2918 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2919 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2920 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2921 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2922 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2923 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2924 | |
| 2925 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2926 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 2927 | .deviceId(mouseDeviceId) |
| 2928 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2929 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 2930 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2931 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2932 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2933 | |
| 2934 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2935 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2936 | .deviceId(touchDeviceId) |
| 2937 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 2938 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2939 | |
| 2940 | // No more events |
| 2941 | spyWindow->assertNoEvents(); |
| 2942 | window->assertNoEvents(); |
| 2943 | } |
| 2944 | |
| 2945 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2946 | * On the display, have a single window, and also an area where there's no window. |
| 2947 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2948 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2949 | */ |
| 2950 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2951 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2952 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2953 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2954 | |
| 2955 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2956 | |
| 2957 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2958 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2959 | |
| 2960 | mDispatcher->waitForIdle(); |
| 2961 | window->assertNoEvents(); |
| 2962 | |
| 2963 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2964 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2965 | mDispatcher->waitForIdle(); |
| 2966 | window->consumeMotionDown(); |
| 2967 | } |
| 2968 | |
| 2969 | /** |
| 2970 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2971 | * non-touchable window. |
| 2972 | */ |
| 2973 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2974 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2975 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2976 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2977 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2978 | window1->setTouchable(false); |
| 2979 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2980 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2981 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2982 | |
| 2983 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2984 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2985 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2986 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2987 | |
| 2988 | mDispatcher->waitForIdle(); |
| 2989 | window1->assertNoEvents(); |
| 2990 | window2->assertNoEvents(); |
| 2991 | |
| 2992 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2993 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2994 | mDispatcher->waitForIdle(); |
| 2995 | window2->consumeMotionDown(); |
| 2996 | } |
| 2997 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2998 | /** |
| 2999 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3000 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3001 | */ |
| 3002 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3003 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3004 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3005 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3006 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3007 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3008 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3009 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3010 | |
| 3011 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 3012 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3013 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3014 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3015 | |
| 3016 | mDispatcher->waitForIdle(); |
| 3017 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3018 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3019 | window2->assertNoEvents(); |
| 3020 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 3021 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 3022 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 3023 | |
| 3024 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3025 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3026 | mDispatcher->waitForIdle(); |
| 3027 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3028 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3029 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 3030 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 3031 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 3032 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 3033 | |
| 3034 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3035 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3036 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3037 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3038 | |
| 3039 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3040 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3041 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3042 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3043 | |
| 3044 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3045 | window1->consumeMotionMove(); |
| 3046 | window1->assertNoEvents(); |
| 3047 | |
| 3048 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3049 | mDispatcher->notifyMotion( |
| 3050 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3051 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3052 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3053 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3054 | mDispatcher->notifyMotion( |
| 3055 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3056 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3057 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3060 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3061 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3062 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3063 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3064 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3065 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3066 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3067 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3068 | |
| 3069 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3070 | |
| 3071 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 3072 | |
| 3073 | // 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] | 3074 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3075 | injectMotionEvent(mDispatcher, |
| 3076 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3077 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3078 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3079 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3080 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3081 | |
| 3082 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3083 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3084 | injectMotionEvent(mDispatcher, |
| 3085 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3086 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3087 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3088 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3089 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3090 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3091 | |
| 3092 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3093 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3094 | injectMotionEvent(mDispatcher, |
| 3095 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3096 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3097 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3098 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3099 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3100 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
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_BUTTON_PRESS, |
| 3105 | AINPUT_SOURCE_MOUSE) |
| 3106 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3107 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3108 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3109 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3110 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3111 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3113 | injectMotionEvent(mDispatcher, |
| 3114 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3115 | AINPUT_SOURCE_MOUSE) |
| 3116 | .buttonState(0) |
| 3117 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3118 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3119 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3120 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3121 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3122 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3123 | injectMotionEvent(mDispatcher, |
| 3124 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3125 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3126 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3127 | .build())); |
| 3128 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3129 | |
| 3130 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3131 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3132 | injectMotionEvent(mDispatcher, |
| 3133 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3134 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3135 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3136 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3137 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3138 | |
| 3139 | // No more events |
| 3140 | windowLeft->assertNoEvents(); |
| 3141 | windowRight->assertNoEvents(); |
| 3142 | } |
| 3143 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3144 | /** |
| 3145 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3146 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3147 | * currently active gesture should be canceled, and the new one should proceed. |
| 3148 | */ |
| 3149 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3150 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3151 | sp<FakeWindowHandle> window = |
| 3152 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3153 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3154 | |
| 3155 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3156 | |
| 3157 | const int32_t touchDeviceId = 4; |
| 3158 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3159 | |
| 3160 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3161 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3162 | .deviceId(touchDeviceId) |
| 3163 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3164 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3165 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3166 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3167 | .deviceId(touchDeviceId) |
| 3168 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3169 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3170 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3171 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3172 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3173 | |
| 3174 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3175 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3176 | .deviceId(mouseDeviceId) |
| 3177 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3178 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3179 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3180 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3181 | WithPointerCount(2u))); |
| 3182 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3183 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3184 | mDispatcher->notifyMotion( |
| 3185 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3186 | .deviceId(mouseDeviceId) |
| 3187 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3188 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3189 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3190 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3191 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3192 | |
| 3193 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3194 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3195 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3196 | .deviceId(touchDeviceId) |
| 3197 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3198 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3199 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3200 | window->assertNoEvents(); |
| 3201 | } |
| 3202 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3203 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3204 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3205 | |
| 3206 | sp<FakeWindowHandle> spyWindow = |
| 3207 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3208 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3209 | spyWindow->setTrustedOverlay(true); |
| 3210 | spyWindow->setSpy(true); |
| 3211 | sp<FakeWindowHandle> window = |
| 3212 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3213 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3214 | |
| 3215 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3216 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3217 | |
| 3218 | // Send mouse cursor to the window |
| 3219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3220 | injectMotionEvent(mDispatcher, |
| 3221 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3222 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3223 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3224 | .x(100) |
| 3225 | .y(100)) |
| 3226 | .build())); |
| 3227 | |
| 3228 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3229 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3230 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3231 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3232 | |
| 3233 | window->assertNoEvents(); |
| 3234 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3235 | } |
| 3236 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3237 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3238 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3239 | |
| 3240 | sp<FakeWindowHandle> spyWindow = |
| 3241 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3242 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3243 | spyWindow->setTrustedOverlay(true); |
| 3244 | spyWindow->setSpy(true); |
| 3245 | sp<FakeWindowHandle> window = |
| 3246 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3247 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3248 | |
| 3249 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3250 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3251 | |
| 3252 | // Send mouse cursor to the window |
| 3253 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3254 | injectMotionEvent(mDispatcher, |
| 3255 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3256 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3257 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3258 | .x(100) |
| 3259 | .y(100)) |
| 3260 | .build())); |
| 3261 | |
| 3262 | // Move mouse cursor |
| 3263 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3264 | injectMotionEvent(mDispatcher, |
| 3265 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3266 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3267 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3268 | .x(110) |
| 3269 | .y(110)) |
| 3270 | .build())); |
| 3271 | |
| 3272 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3273 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3274 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3275 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3276 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3277 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3278 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3279 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3280 | // Touch down on the window |
| 3281 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3282 | injectMotionEvent(mDispatcher, |
| 3283 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3284 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3285 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3286 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3287 | .x(200) |
| 3288 | .y(200)) |
| 3289 | .build())); |
| 3290 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3291 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3292 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3293 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3294 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3295 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3296 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3297 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3298 | |
| 3299 | // pilfer the motion, retaining the gesture on the spy window. |
| 3300 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3301 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3302 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3303 | |
| 3304 | // Touch UP on the window |
| 3305 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3306 | injectMotionEvent(mDispatcher, |
| 3307 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3308 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3309 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3310 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3311 | .x(200) |
| 3312 | .y(200)) |
| 3313 | .build())); |
| 3314 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3315 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3316 | |
| 3317 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3318 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3319 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3320 | |
| 3321 | // One more tap - DOWN |
| 3322 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3323 | injectMotionEvent(mDispatcher, |
| 3324 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3325 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3326 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3327 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3328 | .x(250) |
| 3329 | .y(250)) |
| 3330 | .build())); |
| 3331 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3332 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3333 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3334 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3335 | |
| 3336 | // Touch UP on the window |
| 3337 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3338 | injectMotionEvent(mDispatcher, |
| 3339 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3340 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3341 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3342 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3343 | .x(250) |
| 3344 | .y(250)) |
| 3345 | .build())); |
| 3346 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3347 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3348 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3349 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3350 | |
| 3351 | window->assertNoEvents(); |
| 3352 | spyWindow->assertNoEvents(); |
| 3353 | } |
| 3354 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3355 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3356 | // directly in this test. |
| 3357 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3359 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3360 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3361 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3362 | |
| 3363 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3364 | |
| 3365 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3366 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3367 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3368 | injectMotionEvent(mDispatcher, |
| 3369 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3370 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3371 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3372 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3373 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3374 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3375 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3376 | injectMotionEvent(mDispatcher, |
| 3377 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3378 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3379 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3380 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3381 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3382 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3383 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3385 | injectMotionEvent(mDispatcher, |
| 3386 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3387 | AINPUT_SOURCE_MOUSE) |
| 3388 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3389 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3390 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3391 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3392 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3393 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3394 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3395 | injectMotionEvent(mDispatcher, |
| 3396 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3397 | AINPUT_SOURCE_MOUSE) |
| 3398 | .buttonState(0) |
| 3399 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3400 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3401 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3402 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3403 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3404 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3405 | injectMotionEvent(mDispatcher, |
| 3406 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3407 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3408 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3409 | .build())); |
| 3410 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3411 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3412 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3413 | injectMotionEvent(mDispatcher, |
| 3414 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3415 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3416 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3417 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3418 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3421 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3422 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3423 | * is generated. |
| 3424 | */ |
| 3425 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3426 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3427 | sp<FakeWindowHandle> window = |
| 3428 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3429 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3430 | |
| 3431 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3432 | |
| 3433 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3434 | |
| 3435 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3436 | injectMotionEvent(mDispatcher, |
| 3437 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3438 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3439 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3440 | .x(300) |
| 3441 | .y(400)) |
| 3442 | .build())); |
| 3443 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3444 | |
| 3445 | // Remove the window, but keep the channel. |
| 3446 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3447 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3448 | } |
| 3449 | |
| 3450 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3451 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3452 | */ |
| 3453 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3454 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3455 | sp<FakeWindowHandle> window = |
| 3456 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3457 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3458 | |
| 3459 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3460 | |
| 3461 | const int32_t mouseDeviceId = 7; |
| 3462 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3463 | |
| 3464 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3465 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3466 | .deviceId(mouseDeviceId) |
| 3467 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 3468 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3469 | window->consumeMotionEvent( |
| 3470 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3471 | |
| 3472 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3473 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3474 | .deviceId(touchDeviceId) |
| 3475 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3476 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3477 | |
| 3478 | window->consumeMotionEvent( |
| 3479 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3480 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3481 | } |
| 3482 | |
| 3483 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3484 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3485 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3486 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3487 | */ |
| 3488 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3489 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3490 | sp<FakeWindowHandle> window = |
| 3491 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3492 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3493 | |
| 3494 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3495 | |
| 3496 | // Inject a hover_move from mouse. |
| 3497 | NotifyMotionArgs motionArgs = |
| 3498 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 3499 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3500 | motionArgs.xCursorPosition = 50; |
| 3501 | motionArgs.yCursorPosition = 50; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3502 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3503 | ASSERT_NO_FATAL_FAILURE( |
| 3504 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3505 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3506 | |
| 3507 | // Tap on the window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3508 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3509 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3510 | {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3511 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3512 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3513 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3514 | |
| 3515 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3516 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3517 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3518 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3519 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3520 | ADISPLAY_ID_DEFAULT, {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3521 | ASSERT_NO_FATAL_FAILURE( |
| 3522 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3523 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3524 | } |
| 3525 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3526 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3527 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3528 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3529 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3530 | ADISPLAY_ID_DEFAULT); |
| 3531 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3532 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3533 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3534 | SECOND_DISPLAY_ID); |
| 3535 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3536 | |
| 3537 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3538 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3539 | |
| 3540 | // Set cursor position in window in default display and check that hover enter and move |
| 3541 | // events are generated. |
| 3542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3543 | injectMotionEvent(mDispatcher, |
| 3544 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3545 | AINPUT_SOURCE_MOUSE) |
| 3546 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3547 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3548 | .x(300) |
| 3549 | .y(600)) |
| 3550 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3551 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3552 | |
| 3553 | // Remove all windows in secondary display and check that no event happens on window in |
| 3554 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3555 | mDispatcher->setInputWindows( |
| 3556 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3557 | windowDefaultDisplay->assertNoEvents(); |
| 3558 | |
| 3559 | // Move cursor position in window in default display and check that only hover move |
| 3560 | // event is generated and not hover enter event. |
| 3561 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3562 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3563 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3564 | injectMotionEvent(mDispatcher, |
| 3565 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3566 | AINPUT_SOURCE_MOUSE) |
| 3567 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3568 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3569 | .x(400) |
| 3570 | .y(700)) |
| 3571 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3572 | windowDefaultDisplay->consumeMotionEvent( |
| 3573 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3574 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3575 | windowDefaultDisplay->assertNoEvents(); |
| 3576 | } |
| 3577 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3578 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3579 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3580 | |
| 3581 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3582 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3583 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3584 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3585 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3586 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3587 | |
| 3588 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3589 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3590 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3591 | |
| 3592 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3593 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3594 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3595 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3596 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3597 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3598 | windowRight->assertNoEvents(); |
| 3599 | } |
| 3600 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3601 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3602 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3603 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3604 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3605 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3606 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3607 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3608 | setFocusedWindow(window); |
| 3609 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3610 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3611 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3612 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3613 | |
| 3614 | // Window should receive key down event. |
| 3615 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3616 | |
| 3617 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3618 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3619 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3620 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3621 | AKEY_EVENT_FLAG_CANCELED); |
| 3622 | } |
| 3623 | |
| 3624 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3625 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3626 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3627 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3628 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3629 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3630 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3631 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3632 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3633 | |
| 3634 | // Window should receive motion down event. |
| 3635 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3636 | |
| 3637 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 3638 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3639 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3640 | window->consumeMotionEvent( |
| 3641 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3642 | } |
| 3643 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3644 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 3645 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3646 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3647 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3648 | window->setFocusable(true); |
| 3649 | |
| 3650 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3651 | setFocusedWindow(window); |
| 3652 | |
| 3653 | window->consumeFocusEvent(true); |
| 3654 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3655 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3656 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3657 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3658 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3659 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3660 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3661 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3662 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3663 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3664 | } |
| 3665 | |
| 3666 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3667 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3668 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3669 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3670 | window->setFocusable(true); |
| 3671 | |
| 3672 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3673 | setFocusedWindow(window); |
| 3674 | |
| 3675 | window->consumeFocusEvent(true); |
| 3676 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3677 | mFakePolicy->setInterceptKeyTimeout(150ms); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3678 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 3679 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3680 | |
| 3681 | // Window should receive key event immediately when same key up. |
| 3682 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3683 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3684 | } |
| 3685 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3686 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3687 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3688 | * WATCH_OUTSIDE_TOUCH. |
| 3689 | * Both windows are owned by the same UID. |
| 3690 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3691 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3692 | */ |
| 3693 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3694 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3695 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3696 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3697 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3698 | |
| 3699 | sp<FakeWindowHandle> outsideWindow = |
| 3700 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3701 | ADISPLAY_ID_DEFAULT); |
| 3702 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3703 | outsideWindow->setWatchOutsideTouch(true); |
| 3704 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3705 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3706 | |
| 3707 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3708 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3709 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3710 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3711 | window->consumeMotionDown(); |
| 3712 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3713 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3714 | outsideWindow->consumeMotionEvent( |
| 3715 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3716 | } |
| 3717 | |
| 3718 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3719 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3720 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3721 | * ACTION_OUTSIDE event is sent per gesture. |
| 3722 | */ |
| 3723 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3724 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 3725 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3726 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3727 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3728 | window->setWatchOutsideTouch(true); |
| 3729 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3730 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3731 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3732 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3733 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3734 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3735 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3736 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3737 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3738 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3739 | |
| 3740 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3741 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3742 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3743 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3744 | window->assertNoEvents(); |
| 3745 | secondWindow->assertNoEvents(); |
| 3746 | |
| 3747 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3748 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3749 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3750 | ADISPLAY_ID_DEFAULT, |
| 3751 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3752 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3753 | window->consumeMotionEvent( |
| 3754 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3755 | secondWindow->consumeMotionDown(); |
| 3756 | thirdWindow->assertNoEvents(); |
| 3757 | |
| 3758 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3759 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3760 | mDispatcher->notifyMotion( |
| 3761 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3762 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3763 | window->assertNoEvents(); |
| 3764 | secondWindow->consumeMotionMove(); |
| 3765 | thirdWindow->consumeMotionDown(); |
| 3766 | } |
| 3767 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3768 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3769 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3770 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3771 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3772 | window->setFocusable(true); |
| 3773 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame^] | 3774 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3775 | setFocusedWindow(window); |
| 3776 | |
| 3777 | window->consumeFocusEvent(true); |
| 3778 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3779 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3780 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3781 | mDispatcher->notifyKey(keyDown); |
| 3782 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3783 | |
| 3784 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3785 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3786 | |
| 3787 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame^] | 3788 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3789 | |
| 3790 | window->consumeFocusEvent(false); |
| 3791 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3792 | mDispatcher->notifyKey(keyDown); |
| 3793 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3794 | window->assertNoEvents(); |
| 3795 | } |
| 3796 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3797 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3798 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3799 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3800 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3801 | // Ensure window is non-split and have some transform. |
| 3802 | window->setPreventSplitting(true); |
| 3803 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame^] | 3804 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3805 | |
| 3806 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3807 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3808 | {50, 50})) |
| 3809 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3810 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3811 | |
| 3812 | const MotionEvent secondFingerDownEvent = |
| 3813 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3814 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3815 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3816 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 3817 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3818 | .build(); |
| 3819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3820 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3821 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3823 | |
| 3824 | const MotionEvent* event = window->consumeMotion(); |
| 3825 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3826 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3827 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3828 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3829 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3830 | } |
| 3831 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3832 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3833 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 3834 | * |
| 3835 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 3836 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 3837 | * space. |
| 3838 | */ |
| 3839 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 3840 | public: |
| 3841 | void SetUp() override { |
| 3842 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3843 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 3847 | gui::DisplayInfo info; |
| 3848 | info.displayId = displayId; |
| 3849 | info.transform = transform; |
| 3850 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame^] | 3851 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 3855 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame^] | 3856 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3857 | } |
| 3858 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3859 | void removeAllWindowsAndDisplays() { |
| 3860 | mDisplayInfos.clear(); |
| 3861 | mWindowInfos.clear(); |
| 3862 | } |
| 3863 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3864 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 3865 | // on the display. |
| 3866 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 3867 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 3868 | // respectively. |
| 3869 | ui::Transform displayTransform; |
| 3870 | displayTransform.set(2, 0, 0, 4); |
| 3871 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 3872 | |
| 3873 | std::shared_ptr<FakeApplicationHandle> application = |
| 3874 | std::make_shared<FakeApplicationHandle>(); |
| 3875 | |
| 3876 | // Add two windows to the display. Their frames are represented in the display space. |
| 3877 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3878 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3879 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3880 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 3881 | addWindow(firstWindow); |
| 3882 | |
| 3883 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3884 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3885 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3886 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 3887 | addWindow(secondWindow); |
| 3888 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 3889 | } |
| 3890 | |
| 3891 | private: |
| 3892 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 3893 | std::vector<gui::WindowInfo> mWindowInfos; |
| 3894 | }; |
| 3895 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3896 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3897 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3898 | // Send down to the first window. The point is represented in the display space. The point is |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3899 | // selected so that if the hit test was performed with the point and the bounds being in |
| 3900 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3901 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3902 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3903 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3904 | |
| 3905 | firstWindow->consumeMotionDown(); |
| 3906 | secondWindow->assertNoEvents(); |
| 3907 | } |
| 3908 | |
| 3909 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 3910 | // the event should be treated as being in the logical display space. |
| 3911 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 3912 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3913 | // Send down to the first window. The point is represented in the logical display space. The |
| 3914 | // point is selected so that if the hit test was done in logical display space, then it would |
| 3915 | // end up in the incorrect window. |
| 3916 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3917 | PointF{75 * 2, 55 * 4}); |
| 3918 | |
| 3919 | firstWindow->consumeMotionDown(); |
| 3920 | secondWindow->assertNoEvents(); |
| 3921 | } |
| 3922 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3923 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 3924 | // event should be treated as being in the logical display space. |
| 3925 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 3926 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3927 | |
| 3928 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 3929 | ui::Transform injectedEventTransform; |
| 3930 | injectedEventTransform.set(matrix); |
| 3931 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 3932 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 3933 | |
| 3934 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3935 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3936 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3937 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3938 | .x(untransformedPoint.x) |
| 3939 | .y(untransformedPoint.y)) |
| 3940 | .build(); |
| 3941 | event.transform(matrix); |
| 3942 | |
| 3943 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 3944 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 3945 | |
| 3946 | firstWindow->consumeMotionDown(); |
| 3947 | secondWindow->assertNoEvents(); |
| 3948 | } |
| 3949 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3950 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 3951 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3952 | |
| 3953 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3954 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3955 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3956 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3957 | |
| 3958 | firstWindow->assertNoEvents(); |
| 3959 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 3960 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 3961 | |
| 3962 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 3963 | EXPECT_EQ(300, event->getRawX(0)); |
| 3964 | EXPECT_EQ(880, event->getRawY(0)); |
| 3965 | |
| 3966 | // Ensure that the x and y values are in the window's coordinate space. |
| 3967 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 3968 | // the logical display space. This will be the origin of the window space. |
| 3969 | EXPECT_EQ(100, event->getX(0)); |
| 3970 | EXPECT_EQ(80, event->getY(0)); |
| 3971 | } |
| 3972 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3973 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 3974 | class InputDispatcherDisplayOrientationFixture |
| 3975 | : public InputDispatcherDisplayProjectionTest, |
| 3976 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 3977 | |
| 3978 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 3979 | // in different locations on the display, specifically points close to the four corners of a |
| 3980 | // window. |
| 3981 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 3982 | constexpr static int32_t displayWidth = 400; |
| 3983 | constexpr static int32_t displayHeight = 800; |
| 3984 | |
| 3985 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3986 | |
| 3987 | const auto rotation = GetParam(); |
| 3988 | |
| 3989 | // Set up the display with the specified rotation. |
| 3990 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 3991 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 3992 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 3993 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 3994 | logicalDisplayWidth, logicalDisplayHeight); |
| 3995 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 3996 | |
| 3997 | // Create a window with its bounds determined in the logical display. |
| 3998 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 3999 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4000 | sp<FakeWindowHandle> window = |
| 4001 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4002 | window->setFrame(frameInDisplay, displayTransform); |
| 4003 | addWindow(window); |
| 4004 | |
| 4005 | // The following points in logical display space should be inside the window. |
| 4006 | static const std::array<vec2, 4> insidePoints{ |
| 4007 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4008 | for (const auto pointInsideWindow : insidePoints) { |
| 4009 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4010 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4011 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4012 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4013 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4014 | window->consumeMotionDown(); |
| 4015 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4016 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4017 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4018 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4019 | window->consumeMotionUp(); |
| 4020 | } |
| 4021 | |
| 4022 | // The following points in logical display space should be outside the window. |
| 4023 | static const std::array<vec2, 5> outsidePoints{ |
| 4024 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4025 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4026 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4027 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4028 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4029 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4030 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4031 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4032 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4033 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4034 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4035 | } |
| 4036 | window->assertNoEvents(); |
| 4037 | } |
| 4038 | |
| 4039 | // Run the precision tests for all rotations. |
| 4040 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4041 | InputDispatcherDisplayOrientationFixture, |
| 4042 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4043 | ui::ROTATION_270), |
| 4044 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4045 | return ftl::enum_string(testParamInfo.param); |
| 4046 | }); |
| 4047 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4048 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4049 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4050 | |
| 4051 | class TransferTouchFixture : public InputDispatcherTest, |
| 4052 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4053 | |
| 4054 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4055 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4056 | |
| 4057 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4058 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4059 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4060 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4061 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4062 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4063 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4064 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4065 | sp<FakeWindowHandle> wallpaper = |
| 4066 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 4067 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4068 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4069 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4070 | |
| 4071 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4072 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4073 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4074 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4075 | // Only the first window should get the down event |
| 4076 | firstWindow->consumeMotionDown(); |
| 4077 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4078 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4079 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4080 | // Transfer touch to the second window |
| 4081 | TransferFunction f = GetParam(); |
| 4082 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4083 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4084 | // The first window gets cancel and the second gets down |
| 4085 | firstWindow->consumeMotionCancel(); |
| 4086 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4087 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4088 | |
| 4089 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4090 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4091 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4092 | // The first window gets no events and the second gets up |
| 4093 | firstWindow->assertNoEvents(); |
| 4094 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4095 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4096 | } |
| 4097 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4098 | /** |
| 4099 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 4100 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 4101 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 4102 | * natural to the user. |
| 4103 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 4104 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 4105 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 4106 | * the first window to the second. |
| 4107 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 4108 | * the other API, as well. |
| 4109 | */ |
| 4110 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 4111 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4112 | |
| 4113 | // Create a couple of windows + a spy window |
| 4114 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4115 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4116 | spyWindow->setTrustedOverlay(true); |
| 4117 | spyWindow->setSpy(true); |
| 4118 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4119 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4120 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4121 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4122 | |
| 4123 | // Add the windows to the dispatcher |
| 4124 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 4125 | |
| 4126 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4127 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4128 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4129 | // Only the first window and spy should get the down event |
| 4130 | spyWindow->consumeMotionDown(); |
| 4131 | firstWindow->consumeMotionDown(); |
| 4132 | |
| 4133 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 4134 | // if f === 'transferTouch'. |
| 4135 | TransferFunction f = GetParam(); |
| 4136 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4137 | ASSERT_TRUE(success); |
| 4138 | // The first window gets cancel and the second gets down |
| 4139 | firstWindow->consumeMotionCancel(); |
| 4140 | secondWindow->consumeMotionDown(); |
| 4141 | |
| 4142 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4143 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4144 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4145 | // The first window gets no events and the second+spy get up |
| 4146 | firstWindow->assertNoEvents(); |
| 4147 | spyWindow->consumeMotionUp(); |
| 4148 | secondWindow->consumeMotionUp(); |
| 4149 | } |
| 4150 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4151 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4152 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4153 | |
| 4154 | PointF touchPoint = {10, 10}; |
| 4155 | |
| 4156 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4157 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4158 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4159 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4160 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4161 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4162 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4163 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4164 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4165 | |
| 4166 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4167 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4168 | |
| 4169 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4170 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4171 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4172 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4173 | // Only the first window should get the down event |
| 4174 | firstWindow->consumeMotionDown(); |
| 4175 | secondWindow->assertNoEvents(); |
| 4176 | |
| 4177 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4178 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4179 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4180 | // Only the first window should get the pointer down event |
| 4181 | firstWindow->consumeMotionPointerDown(1); |
| 4182 | secondWindow->assertNoEvents(); |
| 4183 | |
| 4184 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4185 | TransferFunction f = GetParam(); |
| 4186 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4187 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4188 | // The first window gets cancel and the second gets down and pointer down |
| 4189 | firstWindow->consumeMotionCancel(); |
| 4190 | secondWindow->consumeMotionDown(); |
| 4191 | secondWindow->consumeMotionPointerDown(1); |
| 4192 | |
| 4193 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4194 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4195 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4196 | // The first window gets nothing and the second gets pointer up |
| 4197 | firstWindow->assertNoEvents(); |
| 4198 | secondWindow->consumeMotionPointerUp(1); |
| 4199 | |
| 4200 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4201 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4202 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4203 | // The first window gets nothing and the second gets up |
| 4204 | firstWindow->assertNoEvents(); |
| 4205 | secondWindow->consumeMotionUp(); |
| 4206 | } |
| 4207 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4208 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 4209 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4210 | |
| 4211 | // Create a couple of windows |
| 4212 | sp<FakeWindowHandle> firstWindow = |
| 4213 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4214 | ADISPLAY_ID_DEFAULT); |
| 4215 | firstWindow->setDupTouchToWallpaper(true); |
| 4216 | sp<FakeWindowHandle> secondWindow = |
| 4217 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4218 | ADISPLAY_ID_DEFAULT); |
| 4219 | secondWindow->setDupTouchToWallpaper(true); |
| 4220 | |
| 4221 | sp<FakeWindowHandle> wallpaper1 = |
| 4222 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 4223 | wallpaper1->setIsWallpaper(true); |
| 4224 | |
| 4225 | sp<FakeWindowHandle> wallpaper2 = |
| 4226 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 4227 | wallpaper2->setIsWallpaper(true); |
| 4228 | // Add the windows to the dispatcher |
| 4229 | mDispatcher->setInputWindows( |
| 4230 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 4231 | |
| 4232 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4233 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4234 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4235 | |
| 4236 | // Only the first window should get the down event |
| 4237 | firstWindow->consumeMotionDown(); |
| 4238 | secondWindow->assertNoEvents(); |
| 4239 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4240 | wallpaper2->assertNoEvents(); |
| 4241 | |
| 4242 | // Transfer touch focus to the second window |
| 4243 | TransferFunction f = GetParam(); |
| 4244 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4245 | ASSERT_TRUE(success); |
| 4246 | |
| 4247 | // The first window gets cancel and the second gets down |
| 4248 | firstWindow->consumeMotionCancel(); |
| 4249 | secondWindow->consumeMotionDown(); |
| 4250 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4251 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4252 | |
| 4253 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4254 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4255 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4256 | // The first window gets no events and the second gets up |
| 4257 | firstWindow->assertNoEvents(); |
| 4258 | secondWindow->consumeMotionUp(); |
| 4259 | wallpaper1->assertNoEvents(); |
| 4260 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4261 | } |
| 4262 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4263 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 4264 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 4265 | // for the case where there are multiple pointers split across several windows. |
| 4266 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 4267 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4268 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4269 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4270 | return dispatcher->transferTouch(destChannelToken, |
| 4271 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4272 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4273 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4274 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4275 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4276 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4277 | })); |
| 4278 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4279 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4280 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4281 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4282 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4283 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4284 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4285 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4286 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4287 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4288 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4289 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4290 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4291 | |
| 4292 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4293 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4294 | |
| 4295 | PointF pointInFirst = {300, 200}; |
| 4296 | PointF pointInSecond = {300, 600}; |
| 4297 | |
| 4298 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4299 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4300 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4301 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4302 | // Only the first window should get the down event |
| 4303 | firstWindow->consumeMotionDown(); |
| 4304 | secondWindow->assertNoEvents(); |
| 4305 | |
| 4306 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4307 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4308 | ADISPLAY_ID_DEFAULT, |
| 4309 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4310 | // The first window gets a move and the second a down |
| 4311 | firstWindow->consumeMotionMove(); |
| 4312 | secondWindow->consumeMotionDown(); |
| 4313 | |
| 4314 | // Transfer touch focus to the second window |
| 4315 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4316 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 4317 | firstWindow->consumeMotionCancel(); |
| 4318 | secondWindow->consumeMotionPointerDown(1); |
| 4319 | |
| 4320 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4321 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4322 | ADISPLAY_ID_DEFAULT, |
| 4323 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4324 | // The first window gets nothing and the second gets pointer up |
| 4325 | firstWindow->assertNoEvents(); |
| 4326 | secondWindow->consumeMotionPointerUp(1); |
| 4327 | |
| 4328 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4329 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4330 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4331 | // The first window gets nothing and the second gets up |
| 4332 | firstWindow->assertNoEvents(); |
| 4333 | secondWindow->consumeMotionUp(); |
| 4334 | } |
| 4335 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4336 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 4337 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 4338 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 4339 | // window should get nothing. |
| 4340 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 4341 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4342 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4343 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4344 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4345 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4346 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4347 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4348 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4349 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4350 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4351 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4352 | |
| 4353 | // Add the windows to the dispatcher |
| 4354 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4355 | |
| 4356 | PointF pointInFirst = {300, 200}; |
| 4357 | PointF pointInSecond = {300, 600}; |
| 4358 | |
| 4359 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4360 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4361 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4362 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4363 | // Only the first window should get the down event |
| 4364 | firstWindow->consumeMotionDown(); |
| 4365 | secondWindow->assertNoEvents(); |
| 4366 | |
| 4367 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4368 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4369 | ADISPLAY_ID_DEFAULT, |
| 4370 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4371 | // The first window gets a move and the second a down |
| 4372 | firstWindow->consumeMotionMove(); |
| 4373 | secondWindow->consumeMotionDown(); |
| 4374 | |
| 4375 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4376 | const bool transferred = |
| 4377 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4378 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 4379 | ASSERT_FALSE(transferred); |
| 4380 | firstWindow->assertNoEvents(); |
| 4381 | secondWindow->assertNoEvents(); |
| 4382 | |
| 4383 | // The rest of the dispatch should proceed as normal |
| 4384 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4385 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4386 | ADISPLAY_ID_DEFAULT, |
| 4387 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4388 | // The first window gets MOVE and the second gets pointer up |
| 4389 | firstWindow->consumeMotionMove(); |
| 4390 | secondWindow->consumeMotionUp(); |
| 4391 | |
| 4392 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4393 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4394 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4395 | // The first window gets nothing and the second gets up |
| 4396 | firstWindow->consumeMotionUp(); |
| 4397 | secondWindow->assertNoEvents(); |
| 4398 | } |
| 4399 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4400 | // This case will create two windows and one mirrored window on the default display and mirror |
| 4401 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 4402 | // the windows info of second display before default display. |
| 4403 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 4404 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4405 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4406 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4407 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4408 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4409 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4410 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4411 | |
| 4412 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4413 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4414 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4415 | |
| 4416 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4417 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4418 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4419 | |
| 4420 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4421 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4422 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4423 | |
| 4424 | // Update window info, let it find window handle of second display first. |
| 4425 | mDispatcher->setInputWindows( |
| 4426 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4427 | {ADISPLAY_ID_DEFAULT, |
| 4428 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4429 | |
| 4430 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4431 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4432 | {50, 50})) |
| 4433 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4434 | |
| 4435 | // Window should receive motion event. |
| 4436 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4437 | |
| 4438 | // Transfer touch focus |
| 4439 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 4440 | secondWindowInPrimary->getToken())); |
| 4441 | // The first window gets cancel. |
| 4442 | firstWindowInPrimary->consumeMotionCancel(); |
| 4443 | secondWindowInPrimary->consumeMotionDown(); |
| 4444 | |
| 4445 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4446 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4447 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4448 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4449 | firstWindowInPrimary->assertNoEvents(); |
| 4450 | secondWindowInPrimary->consumeMotionMove(); |
| 4451 | |
| 4452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4453 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4454 | {150, 50})) |
| 4455 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4456 | firstWindowInPrimary->assertNoEvents(); |
| 4457 | secondWindowInPrimary->consumeMotionUp(); |
| 4458 | } |
| 4459 | |
| 4460 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 4461 | // 'transferTouch' api. |
| 4462 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 4463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4464 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4465 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4466 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4467 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4468 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4469 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4470 | |
| 4471 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4472 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4473 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4474 | |
| 4475 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4476 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4477 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4478 | |
| 4479 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4480 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4481 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4482 | |
| 4483 | // Update window info, let it find window handle of second display first. |
| 4484 | mDispatcher->setInputWindows( |
| 4485 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4486 | {ADISPLAY_ID_DEFAULT, |
| 4487 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4488 | |
| 4489 | // Touch on second display. |
| 4490 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4491 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 4492 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4493 | |
| 4494 | // Window should receive motion event. |
| 4495 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4496 | |
| 4497 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4498 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4499 | |
| 4500 | // The first window gets cancel. |
| 4501 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4502 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4503 | |
| 4504 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4505 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4506 | SECOND_DISPLAY_ID, {150, 50})) |
| 4507 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4508 | firstWindowInPrimary->assertNoEvents(); |
| 4509 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 4510 | |
| 4511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4512 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 4513 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4514 | firstWindowInPrimary->assertNoEvents(); |
| 4515 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 4516 | } |
| 4517 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4518 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4519 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4520 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4521 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4522 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4523 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4524 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4525 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4526 | |
| 4527 | window->consumeFocusEvent(true); |
| 4528 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4529 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4530 | |
| 4531 | // Window should receive key down event. |
| 4532 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4533 | } |
| 4534 | |
| 4535 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4536 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4537 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4538 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4539 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4540 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4541 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4542 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4543 | mDispatcher->waitForIdle(); |
| 4544 | |
| 4545 | window->assertNoEvents(); |
| 4546 | } |
| 4547 | |
| 4548 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4549 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4550 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4551 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4552 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4553 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4554 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4555 | |
| 4556 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4557 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4558 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4559 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4560 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4561 | |
| 4562 | // Window should receive only the motion event |
| 4563 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4564 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4565 | } |
| 4566 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4567 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4568 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4569 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4570 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4571 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4572 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4573 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4574 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4575 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4576 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4577 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4578 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4579 | |
| 4580 | // Add the windows to the dispatcher |
| 4581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4582 | |
| 4583 | PointF pointInFirst = {300, 200}; |
| 4584 | PointF pointInSecond = {300, 600}; |
| 4585 | |
| 4586 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4587 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4588 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4589 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4590 | // Only the first window should get the down event |
| 4591 | firstWindow->consumeMotionDown(); |
| 4592 | secondWindow->assertNoEvents(); |
| 4593 | |
| 4594 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4595 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4596 | ADISPLAY_ID_DEFAULT, |
| 4597 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4598 | // The first window gets a move and the second a down |
| 4599 | firstWindow->consumeMotionMove(); |
| 4600 | secondWindow->consumeMotionDown(); |
| 4601 | |
| 4602 | // Send pointer cancel to the second window |
| 4603 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4604 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4605 | {pointInFirst, pointInSecond}); |
| 4606 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4607 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4608 | // The first window gets move and the second gets cancel. |
| 4609 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4610 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4611 | |
| 4612 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4613 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4614 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4615 | // The first window gets up and the second gets nothing. |
| 4616 | firstWindow->consumeMotionUp(); |
| 4617 | secondWindow->assertNoEvents(); |
| 4618 | } |
| 4619 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4620 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 4621 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4622 | |
| 4623 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4624 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4625 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4626 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 4627 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 4628 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 4629 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4630 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4631 | window->assertNoEvents(); |
| 4632 | mDispatcher->waitForIdle(); |
| 4633 | } |
| 4634 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4635 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4636 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4637 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4638 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4639 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4640 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4641 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4644 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 4645 | |
| 4646 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4647 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 4648 | expectedFlags); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4649 | } |
| 4650 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4651 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 4652 | |
| 4653 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 4654 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4655 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4656 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4657 | expectedDisplayId, expectedFlags); |
| 4658 | } |
| 4659 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4660 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4661 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4662 | expectedDisplayId, expectedFlags); |
| 4663 | } |
| 4664 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4665 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4666 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4667 | expectedDisplayId, expectedFlags); |
| 4668 | } |
| 4669 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4670 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4671 | mInputReceiver->consumeMotionEvent( |
| 4672 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4673 | WithDisplayId(expectedDisplayId), |
| 4674 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4677 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 4678 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 4679 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4680 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4681 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4684 | MotionEvent* consumeMotion() { |
| 4685 | InputEvent* event = mInputReceiver->consume(); |
| 4686 | if (!event) { |
| 4687 | ADD_FAILURE() << "No event was produced"; |
| 4688 | return nullptr; |
| 4689 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4690 | if (event->getType() != InputEventType::MOTION) { |
| 4691 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4692 | return nullptr; |
| 4693 | } |
| 4694 | return static_cast<MotionEvent*>(event); |
| 4695 | } |
| 4696 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4697 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 4698 | |
| 4699 | private: |
| 4700 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4701 | }; |
| 4702 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4703 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 4704 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4705 | /** |
| 4706 | * Two entities that receive touch: A window, and a global monitor. |
| 4707 | * The touch goes to the window, and then the window disappears. |
| 4708 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 4709 | * for the monitor, as well. |
| 4710 | * 1. foregroundWindow |
| 4711 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 4712 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4713 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4714 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4715 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4716 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4717 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4718 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4719 | |
| 4720 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4722 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4723 | {100, 200})) |
| 4724 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4725 | |
| 4726 | // Both the foreground window and the global monitor should receive the touch down |
| 4727 | window->consumeMotionDown(); |
| 4728 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4729 | |
| 4730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4731 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4732 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4733 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4734 | |
| 4735 | window->consumeMotionMove(); |
| 4736 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4737 | |
| 4738 | // Now the foreground window goes away |
| 4739 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4740 | window->consumeMotionCancel(); |
| 4741 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 4742 | |
| 4743 | // If more events come in, there will be no more foreground window to send them to. This will |
| 4744 | // cause a cancel for the monitor, as well. |
| 4745 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4746 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4747 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 4748 | << "Injection should fail because the window was removed"; |
| 4749 | window->assertNoEvents(); |
| 4750 | // Global monitor now gets the cancel |
| 4751 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4752 | } |
| 4753 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4754 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4755 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4756 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4757 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4758 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4759 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4760 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4761 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4762 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4763 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4764 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4765 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4766 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4769 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 4770 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4771 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4772 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4773 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4774 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4775 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4776 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4777 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4778 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4779 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4780 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4781 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4782 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4783 | // Pilfer pointers from the monitor. |
| 4784 | // This should not do anything and the window should continue to receive events. |
| 4785 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4786 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4787 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4788 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4789 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4790 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4791 | |
| 4792 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4793 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4796 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4797 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4798 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4799 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4800 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4801 | window->setWindowOffset(20, 40); |
| 4802 | window->setWindowTransform(0, 1, -1, 0); |
| 4803 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4804 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4805 | |
| 4806 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4807 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4808 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4809 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4810 | MotionEvent* event = monitor.consumeMotion(); |
| 4811 | // Even though window has transform, gesture monitor must not. |
| 4812 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 4813 | } |
| 4814 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4815 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4816 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4817 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4818 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4819 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4820 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4821 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 4822 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4825 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4826 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4827 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4828 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4829 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4830 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4831 | |
| 4832 | NotifyMotionArgs motionArgs = |
| 4833 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4834 | ADISPLAY_ID_DEFAULT); |
| 4835 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4836 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4837 | // Window should receive motion down event. |
| 4838 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4839 | |
| 4840 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4841 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4842 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4843 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4844 | motionArgs.pointerCoords[0].getX() - 10); |
| 4845 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4846 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4847 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4848 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4849 | } |
| 4850 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4851 | /** |
| 4852 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 4853 | * the device default right away. In the test scenario, we check both the default value, |
| 4854 | * and the action of enabling / disabling. |
| 4855 | */ |
| 4856 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4857 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4858 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4859 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4860 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4861 | |
| 4862 | // Set focused application. |
| 4863 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4864 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4865 | |
| 4866 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4867 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4868 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4869 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4870 | |
| 4871 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4872 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4873 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4874 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4875 | |
| 4876 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4877 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4878 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4879 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4880 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4881 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4882 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4883 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4884 | |
| 4885 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4886 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4887 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4888 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4889 | |
| 4890 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4891 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4892 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4893 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4894 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4895 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4896 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4897 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4898 | |
| 4899 | window->assertNoEvents(); |
| 4900 | } |
| 4901 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4902 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4903 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4904 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4905 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4906 | |
| 4907 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4908 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4909 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4910 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4911 | setFocusedWindow(window); |
| 4912 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4913 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4914 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4915 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 4916 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4917 | |
| 4918 | InputEvent* event = window->consume(); |
| 4919 | ASSERT_NE(event, nullptr); |
| 4920 | |
| 4921 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4922 | ASSERT_NE(verified, nullptr); |
| 4923 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 4924 | |
| 4925 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 4926 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 4927 | ASSERT_EQ(keyArgs.source, verified->source); |
| 4928 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 4929 | |
| 4930 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 4931 | |
| 4932 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4933 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4934 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 4935 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 4936 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 4937 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 4938 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 4939 | } |
| 4940 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4941 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4942 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4943 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4944 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4945 | |
| 4946 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4947 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4948 | ui::Transform transform; |
| 4949 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 4950 | |
| 4951 | gui::DisplayInfo displayInfo; |
| 4952 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 4953 | displayInfo.transform = transform; |
| 4954 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame^] | 4955 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4956 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4957 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4958 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4959 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4960 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4961 | |
| 4962 | InputEvent* event = window->consume(); |
| 4963 | ASSERT_NE(event, nullptr); |
| 4964 | |
| 4965 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 4966 | ASSERT_NE(verified, nullptr); |
| 4967 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 4968 | |
| 4969 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 4970 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 4971 | EXPECT_EQ(motionArgs.source, verified->source); |
| 4972 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 4973 | |
| 4974 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 4975 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 4976 | const vec2 rawXY = |
| 4977 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 4978 | motionArgs.pointerCoords[0].getXYValue()); |
| 4979 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 4980 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4981 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4982 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 4983 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 4984 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 4985 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 4986 | } |
| 4987 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 4988 | /** |
| 4989 | * Ensure that separate calls to sign the same data are generating the same key. |
| 4990 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 4991 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 4992 | * tests. |
| 4993 | */ |
| 4994 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 4995 | KeyEvent event = getTestKeyEvent(); |
| 4996 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 4997 | |
| 4998 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 4999 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5000 | ASSERT_EQ(hmac1, hmac2); |
| 5001 | } |
| 5002 | |
| 5003 | /** |
| 5004 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 5005 | */ |
| 5006 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 5007 | KeyEvent event = getTestKeyEvent(); |
| 5008 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5009 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 5010 | |
| 5011 | verifiedEvent.deviceId += 1; |
| 5012 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5013 | |
| 5014 | verifiedEvent.source += 1; |
| 5015 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5016 | |
| 5017 | verifiedEvent.eventTimeNanos += 1; |
| 5018 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5019 | |
| 5020 | verifiedEvent.displayId += 1; |
| 5021 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5022 | |
| 5023 | verifiedEvent.action += 1; |
| 5024 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5025 | |
| 5026 | verifiedEvent.downTimeNanos += 1; |
| 5027 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5028 | |
| 5029 | verifiedEvent.flags += 1; |
| 5030 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5031 | |
| 5032 | verifiedEvent.keyCode += 1; |
| 5033 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5034 | |
| 5035 | verifiedEvent.scanCode += 1; |
| 5036 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5037 | |
| 5038 | verifiedEvent.metaState += 1; |
| 5039 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5040 | |
| 5041 | verifiedEvent.repeatCount += 1; |
| 5042 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5043 | } |
| 5044 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5045 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 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 | // Top window is also focusable but is not granted focus. |
| 5054 | windowTop->setFocusable(true); |
| 5055 | windowSecond->setFocusable(true); |
| 5056 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5057 | setFocusedWindow(windowSecond); |
| 5058 | |
| 5059 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5060 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5061 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5062 | |
| 5063 | // Focused window should receive event. |
| 5064 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5065 | windowTop->assertNoEvents(); |
| 5066 | } |
| 5067 | |
| 5068 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5069 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5070 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5071 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5072 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5073 | |
| 5074 | window->setFocusable(true); |
| 5075 | // Release channel for window is no longer valid. |
| 5076 | window->releaseChannel(); |
| 5077 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5078 | setFocusedWindow(window); |
| 5079 | |
| 5080 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5081 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5082 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5083 | |
| 5084 | // window channel is invalid, so it should not receive any input event. |
| 5085 | window->assertNoEvents(); |
| 5086 | } |
| 5087 | |
| 5088 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5089 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5090 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5091 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5092 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5093 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5094 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5095 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5096 | setFocusedWindow(window); |
| 5097 | |
| 5098 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5099 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5100 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5101 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5102 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5103 | window->assertNoEvents(); |
| 5104 | } |
| 5105 | |
| 5106 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5107 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5108 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5109 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5110 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5111 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5112 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5113 | |
| 5114 | windowTop->setFocusable(true); |
| 5115 | windowSecond->setFocusable(true); |
| 5116 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5117 | setFocusedWindow(windowTop); |
| 5118 | windowTop->consumeFocusEvent(true); |
| 5119 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5120 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
| 5121 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5122 | windowSecond->consumeFocusEvent(true); |
| 5123 | windowTop->consumeFocusEvent(false); |
| 5124 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5125 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5126 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5127 | |
| 5128 | // Focused window should receive event. |
| 5129 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5130 | } |
| 5131 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5132 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5133 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5134 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5135 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5136 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5137 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5138 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5139 | |
| 5140 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5141 | windowSecond->setFocusable(false); |
| 5142 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5143 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5144 | setFocusedWindow(windowTop); |
| 5145 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5146 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5147 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5148 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5149 | |
| 5150 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5151 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5152 | windowSecond->assertNoEvents(); |
| 5153 | } |
| 5154 | |
| 5155 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5156 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5157 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5158 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5159 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5160 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5161 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5162 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5163 | |
| 5164 | window->setFocusable(true); |
| 5165 | previousFocusedWindow->setFocusable(true); |
| 5166 | window->setVisible(false); |
| 5167 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5168 | setFocusedWindow(previousFocusedWindow); |
| 5169 | previousFocusedWindow->consumeFocusEvent(true); |
| 5170 | |
| 5171 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5172 | setFocusedWindow(window); |
| 5173 | previousFocusedWindow->consumeFocusEvent(false); |
| 5174 | |
| 5175 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5177 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5178 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5179 | |
| 5180 | // Window does not get focus event or key down. |
| 5181 | window->assertNoEvents(); |
| 5182 | |
| 5183 | // Window becomes visible. |
| 5184 | window->setVisible(true); |
| 5185 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5186 | |
| 5187 | // Window receives focus event. |
| 5188 | window->consumeFocusEvent(true); |
| 5189 | // Focused window receives key down. |
| 5190 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5191 | } |
| 5192 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5193 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5194 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5195 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5196 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5197 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5198 | |
| 5199 | // window is granted focus. |
| 5200 | window->setFocusable(true); |
| 5201 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5202 | setFocusedWindow(window); |
| 5203 | window->consumeFocusEvent(true); |
| 5204 | |
| 5205 | // When a display is removed window loses focus. |
| 5206 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5207 | window->consumeFocusEvent(false); |
| 5208 | } |
| 5209 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5210 | /** |
| 5211 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5212 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5213 | * of the 'slipperyEnterWindow'. |
| 5214 | * |
| 5215 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5216 | * a way so that the touched location is no longer covered by the top window. |
| 5217 | * |
| 5218 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5219 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5220 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5221 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5222 | * with ACTION_DOWN). |
| 5223 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5224 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5225 | * |
| 5226 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5227 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5228 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5229 | * |
| 5230 | * In this test, we ensure that the event received by the bottom window has |
| 5231 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5232 | */ |
| 5233 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5234 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 5235 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5236 | |
| 5237 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5238 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5239 | |
| 5240 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5241 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5242 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5243 | // Make sure this one overlaps the bottom window |
| 5244 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5245 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5246 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5247 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5248 | |
| 5249 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5250 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5251 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5252 | |
| 5253 | mDispatcher->setInputWindows( |
| 5254 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5255 | |
| 5256 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5257 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5258 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5259 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5260 | slipperyExitWindow->consumeMotionDown(); |
| 5261 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5262 | mDispatcher->setInputWindows( |
| 5263 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5264 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5265 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 5266 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5267 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5268 | |
| 5269 | slipperyExitWindow->consumeMotionCancel(); |
| 5270 | |
| 5271 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5272 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5273 | } |
| 5274 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5275 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5276 | protected: |
| 5277 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5278 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5279 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5280 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5281 | sp<FakeWindowHandle> mWindow; |
| 5282 | |
| 5283 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5284 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5285 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5286 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 5287 | mDispatcher->requestRefreshConfiguration(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5288 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 5289 | ASSERT_EQ(OK, mDispatcher->start()); |
| 5290 | |
| 5291 | setUpWindow(); |
| 5292 | } |
| 5293 | |
| 5294 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5295 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5296 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5297 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5298 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5299 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5300 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5301 | mWindow->consumeFocusEvent(true); |
| 5302 | } |
| 5303 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5304 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5305 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5306 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5307 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5308 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5309 | |
| 5310 | // Window should receive key down event. |
| 5311 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5312 | } |
| 5313 | |
| 5314 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 5315 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 5316 | InputEvent* repeatEvent = mWindow->consume(); |
| 5317 | ASSERT_NE(nullptr, repeatEvent); |
| 5318 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5319 | ASSERT_EQ(InputEventType::KEY, repeatEvent->getType()); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5320 | |
| 5321 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 5322 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 5323 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 5324 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 5325 | } |
| 5326 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5327 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5328 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5329 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5330 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5331 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5332 | |
| 5333 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5334 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5335 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5336 | } |
| 5337 | }; |
| 5338 | |
| 5339 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5340 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5341 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5342 | expectKeyRepeatOnce(repeatCount); |
| 5343 | } |
| 5344 | } |
| 5345 | |
| 5346 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5347 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5348 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5349 | expectKeyRepeatOnce(repeatCount); |
| 5350 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5351 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5352 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5353 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5354 | expectKeyRepeatOnce(repeatCount); |
| 5355 | } |
| 5356 | } |
| 5357 | |
| 5358 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5359 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5360 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5361 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5362 | mWindow->assertNoEvents(); |
| 5363 | } |
| 5364 | |
| 5365 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5366 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5367 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5368 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5369 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5370 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5371 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5372 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5373 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 5374 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5375 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5376 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5377 | mWindow->assertNoEvents(); |
| 5378 | } |
| 5379 | |
| 5380 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5381 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5382 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5383 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5384 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5385 | // 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] | 5386 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5387 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5388 | mWindow->assertNoEvents(); |
| 5389 | } |
| 5390 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5391 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 5392 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5393 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5394 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5395 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 5396 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 5397 | mWindow->assertNoEvents(); |
| 5398 | } |
| 5399 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5400 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5401 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5402 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5403 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5404 | InputEvent* repeatEvent = mWindow->consume(); |
| 5405 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5406 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 5407 | IdGenerator::getSource(repeatEvent->getId())); |
| 5408 | } |
| 5409 | } |
| 5410 | |
| 5411 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5412 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5413 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5414 | |
| 5415 | std::unordered_set<int32_t> idSet; |
| 5416 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5417 | InputEvent* repeatEvent = mWindow->consume(); |
| 5418 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5419 | int32_t id = repeatEvent->getId(); |
| 5420 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 5421 | idSet.insert(id); |
| 5422 | } |
| 5423 | } |
| 5424 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5425 | /* Test InputDispatcher for MultiDisplay */ |
| 5426 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 5427 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5428 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5429 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5430 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5431 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5432 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5433 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5434 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5435 | // Set focus window for primary display, but focused display would be second one. |
| 5436 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5437 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5438 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5439 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5440 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5441 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5442 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5443 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5444 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5445 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5446 | // Set focus display to second one. |
| 5447 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 5448 | // Set focus window for second display. |
| 5449 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5450 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5451 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5452 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5453 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5454 | } |
| 5455 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5456 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5457 | InputDispatcherTest::TearDown(); |
| 5458 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5459 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5460 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5461 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5462 | windowInSecondary.clear(); |
| 5463 | } |
| 5464 | |
| 5465 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5466 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5467 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5468 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5469 | sp<FakeWindowHandle> windowInSecondary; |
| 5470 | }; |
| 5471 | |
| 5472 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 5473 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5474 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5475 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5476 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5477 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5478 | windowInSecondary->assertNoEvents(); |
| 5479 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5480 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5481 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5482 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5483 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5484 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5485 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5486 | } |
| 5487 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5488 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5489 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5490 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5491 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5492 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5493 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5494 | windowInSecondary->assertNoEvents(); |
| 5495 | |
| 5496 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5497 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5498 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5499 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5500 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5501 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5502 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5503 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5504 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5505 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5506 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5507 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5508 | |
| 5509 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5510 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5511 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5512 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5513 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5514 | windowInSecondary->assertNoEvents(); |
| 5515 | } |
| 5516 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5517 | // Test per-display input monitors for motion event. |
| 5518 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5519 | FakeMonitorReceiver monitorInPrimary = |
| 5520 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5521 | FakeMonitorReceiver monitorInSecondary = |
| 5522 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5523 | |
| 5524 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5525 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5526 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5527 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5528 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5529 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5530 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5531 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5532 | |
| 5533 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5534 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5535 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5536 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5537 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5538 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5539 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5540 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5541 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 5542 | // Lift up the touch from the second display |
| 5543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5544 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5545 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5546 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5547 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 5548 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5549 | // Test inject a non-pointer motion event. |
| 5550 | // If specific a display, it will dispatch to the focused window of particular display, |
| 5551 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5552 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5553 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 5554 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5555 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5556 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5557 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5558 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | // Test per-display input monitors for key event. |
| 5562 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5563 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5564 | FakeMonitorReceiver monitorInPrimary = |
| 5565 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5566 | FakeMonitorReceiver monitorInSecondary = |
| 5567 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5568 | |
| 5569 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5570 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5571 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5572 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5573 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5574 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5575 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5576 | } |
| 5577 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5578 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 5579 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5580 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5581 | secondWindowInPrimary->setFocusable(true); |
| 5582 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 5583 | setFocusedWindow(secondWindowInPrimary); |
| 5584 | windowInPrimary->consumeFocusEvent(false); |
| 5585 | secondWindowInPrimary->consumeFocusEvent(true); |
| 5586 | |
| 5587 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5588 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 5589 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5590 | windowInPrimary->assertNoEvents(); |
| 5591 | windowInSecondary->assertNoEvents(); |
| 5592 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5593 | } |
| 5594 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 5595 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 5596 | FakeMonitorReceiver monitorInPrimary = |
| 5597 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5598 | FakeMonitorReceiver monitorInSecondary = |
| 5599 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 5600 | |
| 5601 | // Test touch down on primary display. |
| 5602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5603 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5604 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5605 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5606 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5607 | |
| 5608 | // Test touch down on second display. |
| 5609 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5610 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5611 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5612 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5613 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 5614 | |
| 5615 | // Trigger cancel touch. |
| 5616 | mDispatcher->cancelCurrentTouch(); |
| 5617 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5618 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5619 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5620 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5621 | |
| 5622 | // Test inject a move motion event, no window/monitor should receive the event. |
| 5623 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5624 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5625 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5626 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5627 | windowInPrimary->assertNoEvents(); |
| 5628 | monitorInPrimary.assertNoEvents(); |
| 5629 | |
| 5630 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5631 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5632 | SECOND_DISPLAY_ID, {110, 200})) |
| 5633 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5634 | windowInSecondary->assertNoEvents(); |
| 5635 | monitorInSecondary.assertNoEvents(); |
| 5636 | } |
| 5637 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5638 | class InputFilterTest : public InputDispatcherTest { |
| 5639 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5640 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 5641 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5642 | NotifyMotionArgs motionArgs; |
| 5643 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5644 | motionArgs = |
| 5645 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5646 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5647 | motionArgs = |
| 5648 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5649 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5650 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5651 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5652 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 5653 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5654 | } else { |
| 5655 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5656 | } |
| 5657 | } |
| 5658 | |
| 5659 | void testNotifyKey(bool expectToBeFiltered) { |
| 5660 | NotifyKeyArgs keyArgs; |
| 5661 | |
| 5662 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5663 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5664 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5665 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5666 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5667 | |
| 5668 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 5669 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5670 | } else { |
| 5671 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5672 | } |
| 5673 | } |
| 5674 | }; |
| 5675 | |
| 5676 | // Test InputFilter for MotionEvent |
| 5677 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 5678 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 5679 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5680 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5681 | |
| 5682 | // Enable InputFilter |
| 5683 | mDispatcher->setInputFilterEnabled(true); |
| 5684 | // Test touch on both primary and second display, and check if both events are filtered. |
| 5685 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 5686 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 5687 | |
| 5688 | // Disable InputFilter |
| 5689 | mDispatcher->setInputFilterEnabled(false); |
| 5690 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 5691 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5692 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5693 | } |
| 5694 | |
| 5695 | // Test InputFilter for KeyEvent |
| 5696 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 5697 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 5698 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5699 | |
| 5700 | // Enable InputFilter |
| 5701 | mDispatcher->setInputFilterEnabled(true); |
| 5702 | // Send a key event, and check if it is filtered. |
| 5703 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 5704 | |
| 5705 | // Disable InputFilter |
| 5706 | mDispatcher->setInputFilterEnabled(false); |
| 5707 | // Send a key event, and check if it isn't filtered. |
| 5708 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5709 | } |
| 5710 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5711 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 5712 | // logical display coordinate space. |
| 5713 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 5714 | ui::Transform firstDisplayTransform; |
| 5715 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5716 | ui::Transform secondDisplayTransform; |
| 5717 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 5718 | |
| 5719 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 5720 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 5721 | displayInfos[0].transform = firstDisplayTransform; |
| 5722 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 5723 | displayInfos[1].transform = secondDisplayTransform; |
| 5724 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame^] | 5725 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5726 | |
| 5727 | // Enable InputFilter |
| 5728 | mDispatcher->setInputFilterEnabled(true); |
| 5729 | |
| 5730 | // Ensure the correct transforms are used for the displays. |
| 5731 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 5732 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 5733 | } |
| 5734 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5735 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 5736 | protected: |
| 5737 | virtual void SetUp() override { |
| 5738 | InputDispatcherTest::SetUp(); |
| 5739 | |
| 5740 | /** |
| 5741 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 5742 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 5743 | * on. |
| 5744 | */ |
| 5745 | mDispatcher->setInputFilterEnabled(true); |
| 5746 | |
| 5747 | std::shared_ptr<InputApplicationHandle> application = |
| 5748 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5749 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 5750 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5751 | |
| 5752 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5753 | mWindow->setFocusable(true); |
| 5754 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5755 | setFocusedWindow(mWindow); |
| 5756 | mWindow->consumeFocusEvent(true); |
| 5757 | } |
| 5758 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5759 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5760 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5761 | KeyEvent event; |
| 5762 | |
| 5763 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5764 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 5765 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5766 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5767 | const int32_t additionalPolicyFlags = |
| 5768 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 5769 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5770 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5771 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5772 | policyFlags | additionalPolicyFlags)); |
| 5773 | |
| 5774 | InputEvent* received = mWindow->consume(); |
| 5775 | ASSERT_NE(nullptr, received); |
| 5776 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5777 | ASSERT_EQ(received->getType(), InputEventType::KEY); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5778 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 5779 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 5780 | } |
| 5781 | |
| 5782 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5783 | int32_t flags) { |
| 5784 | MotionEvent event; |
| 5785 | PointerProperties pointerProperties[1]; |
| 5786 | PointerCoords pointerCoords[1]; |
| 5787 | pointerProperties[0].clear(); |
| 5788 | pointerProperties[0].id = 0; |
| 5789 | pointerCoords[0].clear(); |
| 5790 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 5791 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 5792 | |
| 5793 | ui::Transform identityTransform; |
| 5794 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5795 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 5796 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 5797 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 5798 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 5799 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 5800 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5801 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 5802 | |
| 5803 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 5804 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5805 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5806 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5807 | policyFlags | additionalPolicyFlags)); |
| 5808 | |
| 5809 | InputEvent* received = mWindow->consume(); |
| 5810 | ASSERT_NE(nullptr, received); |
| 5811 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5812 | ASSERT_EQ(received->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5813 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 5814 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | private: |
| 5818 | sp<FakeWindowHandle> mWindow; |
| 5819 | }; |
| 5820 | |
| 5821 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5822 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 5823 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 5824 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5825 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 5826 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5829 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5830 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5831 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5832 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 5833 | } |
| 5834 | |
| 5835 | TEST_F(InputFilterInjectionPolicyTest, |
| 5836 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 5837 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5838 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5839 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5843 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 5844 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5847 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5848 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5849 | InputDispatcherTest::SetUp(); |
| 5850 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5851 | std::shared_ptr<FakeApplicationHandle> application = |
| 5852 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5853 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5854 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5855 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5856 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5857 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5858 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5859 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5860 | |
| 5861 | // Set focused application. |
| 5862 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5863 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5864 | |
| 5865 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5866 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5867 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5868 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5869 | } |
| 5870 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5871 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5872 | InputDispatcherTest::TearDown(); |
| 5873 | |
| 5874 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5875 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | protected: |
| 5879 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5880 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5881 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5882 | }; |
| 5883 | |
| 5884 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5885 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 5886 | // the onPointerDownOutsideFocus callback. |
| 5887 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5889 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5890 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5891 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5892 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5893 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5894 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5895 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 5896 | } |
| 5897 | |
| 5898 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 5899 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 5900 | // onPointerDownOutsideFocus callback. |
| 5901 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5903 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5904 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5905 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5906 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5907 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5908 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 5912 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 5913 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5914 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5915 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5916 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5917 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5918 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5919 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5920 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5921 | } |
| 5922 | |
| 5923 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5924 | // DOWN on the window that already has focus. Ensure no window received the |
| 5925 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5926 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5927 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5928 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5929 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5930 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5931 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5932 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5933 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5934 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5935 | } |
| 5936 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 5937 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 5938 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 5939 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 5940 | const MotionEvent event = |
| 5941 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5942 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5943 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 5944 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 5945 | .build(); |
| 5946 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 5947 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5948 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 5949 | |
| 5950 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 5951 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 5952 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 5953 | mUnfocusedWindow->assertNoEvents(); |
| 5954 | } |
| 5955 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5956 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 5957 | // windows that point to the same client token. |
| 5958 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 5959 | virtual void SetUp() override { |
| 5960 | InputDispatcherTest::SetUp(); |
| 5961 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5962 | std::shared_ptr<FakeApplicationHandle> application = |
| 5963 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5964 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 5965 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5966 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 5967 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5968 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 5969 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5970 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 5971 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5972 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5973 | } |
| 5974 | |
| 5975 | protected: |
| 5976 | sp<FakeWindowHandle> mWindow1; |
| 5977 | sp<FakeWindowHandle> mWindow2; |
| 5978 | |
| 5979 | // 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] | 5980 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 5981 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 5982 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 5986 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5987 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5988 | InputEvent* event = window->consume(); |
| 5989 | |
| 5990 | ASSERT_NE(nullptr, event) << name.c_str() |
| 5991 | << ": consumer should have returned non-NULL event."; |
| 5992 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5993 | ASSERT_EQ(InputEventType::MOTION, event->getType()) |
| 5994 | << name.c_str() << ": expected MotionEvent, got " << *event; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5995 | |
| 5996 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5997 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 5998 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 5999 | |
| 6000 | for (size_t i = 0; i < points.size(); i++) { |
| 6001 | float expectedX = points[i].x; |
| 6002 | float expectedY = points[i].y; |
| 6003 | |
| 6004 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 6005 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 6006 | << ", got " << motionEvent.getX(i); |
| 6007 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 6008 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 6009 | << ", got " << motionEvent.getY(i); |
| 6010 | } |
| 6011 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6012 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6013 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6014 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6015 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 6016 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6017 | |
| 6018 | // Always consume from window1 since it's the window that has the InputReceiver |
| 6019 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 6020 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6021 | }; |
| 6022 | |
| 6023 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 6024 | // Touch Window 1 |
| 6025 | PointF touchedPoint = {10, 10}; |
| 6026 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6027 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6028 | |
| 6029 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6030 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6031 | |
| 6032 | // Touch Window 2 |
| 6033 | touchedPoint = {150, 150}; |
| 6034 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6035 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6036 | } |
| 6037 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6038 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 6039 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6040 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6041 | |
| 6042 | // Touch Window 1 |
| 6043 | PointF touchedPoint = {10, 10}; |
| 6044 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6045 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6046 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6047 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6048 | |
| 6049 | // Touch Window 2 |
| 6050 | touchedPoint = {150, 150}; |
| 6051 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6052 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 6053 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6054 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6055 | // Update the transform so rotation is set |
| 6056 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6057 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 6058 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6059 | } |
| 6060 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6061 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6062 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6063 | |
| 6064 | // Touch Window 1 |
| 6065 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6066 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6067 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6068 | |
| 6069 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6070 | touchedPoints.push_back(PointF{150, 150}); |
| 6071 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6072 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6073 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6074 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6075 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6076 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6077 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6078 | // Update the transform so rotation is set for Window 2 |
| 6079 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6080 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6081 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6082 | } |
| 6083 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6084 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6085 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6086 | |
| 6087 | // Touch Window 1 |
| 6088 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6089 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6090 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6091 | |
| 6092 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6093 | touchedPoints.push_back(PointF{150, 150}); |
| 6094 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6095 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6096 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6097 | |
| 6098 | // Move both windows |
| 6099 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6100 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6101 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6102 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6103 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6104 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6105 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6106 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6107 | expectedPoints.pop_back(); |
| 6108 | |
| 6109 | // Touch Window 2 |
| 6110 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6111 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6112 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6113 | |
| 6114 | // Move both windows |
| 6115 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6116 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6117 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6118 | |
| 6119 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 6123 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 6124 | |
| 6125 | // Touch Window 1 |
| 6126 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6127 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6128 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6129 | |
| 6130 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6131 | touchedPoints.push_back(PointF{150, 150}); |
| 6132 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6133 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6134 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6135 | |
| 6136 | // Move both windows |
| 6137 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6138 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6139 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6140 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6141 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 6144 | /** |
| 6145 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 6146 | * same input channel. |
| 6147 | */ |
| 6148 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 6149 | mWindow1->setSlippery(true); |
| 6150 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6151 | |
| 6152 | // Touch down in window 1 |
| 6153 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6154 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6155 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 6156 | |
| 6157 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 6158 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 6159 | // getting generated. |
| 6160 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6161 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6162 | |
| 6163 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 6164 | } |
| 6165 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6166 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 6167 | virtual void SetUp() override { |
| 6168 | InputDispatcherTest::SetUp(); |
| 6169 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6170 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6171 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6172 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 6173 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6174 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6175 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6176 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6177 | |
| 6178 | // Set focused application. |
| 6179 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 6180 | |
| 6181 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6182 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6183 | mWindow->consumeFocusEvent(true); |
| 6184 | } |
| 6185 | |
| 6186 | virtual void TearDown() override { |
| 6187 | InputDispatcherTest::TearDown(); |
| 6188 | mWindow.clear(); |
| 6189 | } |
| 6190 | |
| 6191 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6192 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6193 | sp<FakeWindowHandle> mWindow; |
| 6194 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 6195 | |
| 6196 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6198 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6199 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6201 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6202 | WINDOW_LOCATION)); |
| 6203 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6204 | |
| 6205 | sp<FakeWindowHandle> addSpyWindow() { |
| 6206 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6207 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6208 | spy->setTrustedOverlay(true); |
| 6209 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6210 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6211 | spy->setDispatchingTimeout(30ms); |
| 6212 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 6213 | return spy; |
| 6214 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6215 | }; |
| 6216 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6217 | // Send a tap and respond, which should not cause an ANR. |
| 6218 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 6219 | tapOnWindow(); |
| 6220 | mWindow->consumeMotionDown(); |
| 6221 | mWindow->consumeMotionUp(); |
| 6222 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6223 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6224 | } |
| 6225 | |
| 6226 | // Send a regular key and respond, which should not cause an ANR. |
| 6227 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6228 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6229 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6230 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6231 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6232 | } |
| 6233 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6234 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 6235 | mWindow->setFocusable(false); |
| 6236 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6237 | mWindow->consumeFocusEvent(false); |
| 6238 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6239 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6240 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6241 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6242 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6244 | // Key will not go to window because we have no focused window. |
| 6245 | // The 'no focused window' ANR timer should start instead. |
| 6246 | |
| 6247 | // Now, the focused application goes away. |
| 6248 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 6249 | // The key should get dropped and there should be no ANR. |
| 6250 | |
| 6251 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6252 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6253 | } |
| 6254 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6255 | // 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] | 6256 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 6257 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6258 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6259 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6260 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6261 | WINDOW_LOCATION)); |
| 6262 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6263 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 6264 | ASSERT_TRUE(sequenceNum); |
| 6265 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6266 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6267 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6268 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6269 | mWindow->consumeMotionEvent( |
| 6270 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6271 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6272 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6273 | } |
| 6274 | |
| 6275 | // Send a key to the app and have the app not respond right away. |
| 6276 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 6277 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6278 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6279 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 6280 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6281 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6282 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6283 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6284 | } |
| 6285 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6286 | // We have a focused application, but no focused window |
| 6287 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
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 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6293 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6294 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6295 | WINDOW_LOCATION)); |
| 6296 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 6297 | mDispatcher->waitForIdle(); |
| 6298 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6299 | |
| 6300 | // Once a focused event arrives, we get an ANR for this application |
| 6301 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6302 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6303 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6304 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6305 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6306 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6307 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6308 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6309 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6310 | } |
| 6311 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6312 | /** |
| 6313 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 6314 | * there will not be an ANR. |
| 6315 | */ |
| 6316 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 6317 | mWindow->setFocusable(false); |
| 6318 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6319 | mWindow->consumeFocusEvent(false); |
| 6320 | |
| 6321 | KeyEvent event; |
| 6322 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 6323 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 6324 | |
| 6325 | // Define a valid key down event that is stale (too old). |
| 6326 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 6327 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6328 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6329 | |
| 6330 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 6331 | |
| 6332 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6333 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6334 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 6335 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 6336 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 6337 | << "Injection should fail because the event is stale"; |
| 6338 | |
| 6339 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6340 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6341 | mWindow->assertNoEvents(); |
| 6342 | } |
| 6343 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6344 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6345 | // Make sure that we don't notify policy twice about the same ANR. |
| 6346 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6347 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6348 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6349 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6350 | |
| 6351 | // Once a focused event arrives, we get an ANR for this application |
| 6352 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6353 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6354 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6355 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6356 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6357 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6358 | const std::chrono::duration appTimeout = |
| 6359 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6360 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6361 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6362 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6363 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 6364 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6365 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6366 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 6367 | // '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] | 6368 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6369 | } |
| 6370 | |
| 6371 | // We have a focused application, but no focused window |
| 6372 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6373 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6374 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6375 | mWindow->consumeFocusEvent(false); |
| 6376 | |
| 6377 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6378 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6379 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6380 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6381 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6382 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6383 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6384 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6385 | |
| 6386 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6387 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6388 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6389 | mWindow->assertNoEvents(); |
| 6390 | } |
| 6391 | |
| 6392 | /** |
| 6393 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 6394 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 6395 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 6396 | * the ANR mechanism should still work. |
| 6397 | * |
| 6398 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 6399 | * DOWN event, while not responding on the second one. |
| 6400 | */ |
| 6401 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 6402 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6403 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6404 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6405 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6406 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6407 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6408 | |
| 6409 | // Now send ACTION_UP, with identical timestamp |
| 6410 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6411 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6412 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6413 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6414 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6415 | |
| 6416 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 6417 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6418 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6419 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6420 | } |
| 6421 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6422 | // A spy window can receive an ANR |
| 6423 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 6424 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 6425 | |
| 6426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6427 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6428 | WINDOW_LOCATION)); |
| 6429 | mWindow->consumeMotionDown(); |
| 6430 | |
| 6431 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 6432 | ASSERT_TRUE(sequenceNum); |
| 6433 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6434 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6435 | |
| 6436 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6437 | spy->consumeMotionEvent( |
| 6438 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6439 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6440 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6441 | } |
| 6442 | |
| 6443 | // 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] | 6444 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6445 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 6446 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6447 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6448 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6449 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6450 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6451 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6452 | |
| 6453 | // Stuck on the ACTION_UP |
| 6454 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6455 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6456 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6457 | // 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] | 6458 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6459 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6460 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6461 | |
| 6462 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6463 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6464 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6465 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6466 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6467 | } |
| 6468 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6469 | // 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] | 6470 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6471 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 6472 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6473 | |
| 6474 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6475 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6476 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6477 | |
| 6478 | mWindow->consumeMotionDown(); |
| 6479 | // Stuck on the ACTION_UP |
| 6480 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6481 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6482 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6483 | // 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] | 6484 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6485 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6486 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6487 | |
| 6488 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6489 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6490 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6491 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6492 | spy->assertNoEvents(); |
| 6493 | } |
| 6494 | |
| 6495 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 6496 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 6497 | |
| 6498 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6499 | |
| 6500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6501 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6502 | WINDOW_LOCATION)); |
| 6503 | |
| 6504 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6505 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 6506 | ASSERT_TRUE(consumeSeq); |
| 6507 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6508 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6509 | |
| 6510 | monitor.finishEvent(*consumeSeq); |
| 6511 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6512 | |
| 6513 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6514 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 6518 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 6519 | // get an ANR again. |
| 6520 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 6521 | // 2. consume all pending events (= queue becomes healthy again) |
| 6522 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 6523 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 6524 | tapOnWindow(); |
| 6525 | |
| 6526 | mWindow->consumeMotionDown(); |
| 6527 | // Block on ACTION_UP |
| 6528 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6529 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6530 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 6531 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6532 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6533 | mWindow->assertNoEvents(); |
| 6534 | |
| 6535 | tapOnWindow(); |
| 6536 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6537 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6538 | mWindow->consumeMotionUp(); |
| 6539 | |
| 6540 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6541 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6542 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6543 | mWindow->assertNoEvents(); |
| 6544 | } |
| 6545 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6546 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 6547 | // it. |
| 6548 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6549 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6550 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6551 | WINDOW_LOCATION)); |
| 6552 | |
| 6553 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6554 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6555 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6556 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 6557 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6558 | // 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] | 6559 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6560 | mWindow->consumeMotionEvent( |
| 6561 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6562 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6563 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6564 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6565 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6566 | } |
| 6567 | |
| 6568 | /** |
| 6569 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6570 | * not to to the focused window until the motion is processed. |
| 6571 | * |
| 6572 | * Warning!!! |
| 6573 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6574 | * and the injection timeout that we specify when injecting the key. |
| 6575 | * We must have the injection timeout (10ms) be smaller than |
| 6576 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6577 | * |
| 6578 | * If that value changes, this test should also change. |
| 6579 | */ |
| 6580 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 6581 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6582 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6583 | |
| 6584 | tapOnWindow(); |
| 6585 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6586 | ASSERT_TRUE(downSequenceNum); |
| 6587 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6588 | ASSERT_TRUE(upSequenceNum); |
| 6589 | // Don't finish the events yet, and send a key |
| 6590 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 6591 | // window even if motions are still being processed. But because the injection timeout is short, |
| 6592 | // we will receive INJECTION_TIMED_OUT as the result. |
| 6593 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6594 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6595 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6596 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6597 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6598 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6599 | // and the key remains pending, waiting for the touch events to be processed |
| 6600 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6601 | ASSERT_FALSE(keySequenceNum); |
| 6602 | |
| 6603 | std::this_thread::sleep_for(500ms); |
| 6604 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 6605 | // to the focused window, even though we have not yet finished the motion event |
| 6606 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6607 | mWindow->finishEvent(*downSequenceNum); |
| 6608 | mWindow->finishEvent(*upSequenceNum); |
| 6609 | } |
| 6610 | |
| 6611 | /** |
| 6612 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6613 | * not go to the focused window until the motion is processed. |
| 6614 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 6615 | * focused window right away. |
| 6616 | */ |
| 6617 | TEST_F(InputDispatcherSingleWindowAnr, |
| 6618 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 6619 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6620 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6621 | |
| 6622 | tapOnWindow(); |
| 6623 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6624 | ASSERT_TRUE(downSequenceNum); |
| 6625 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6626 | ASSERT_TRUE(upSequenceNum); |
| 6627 | // Don't finish the events yet, and send a key |
| 6628 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6630 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6631 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6632 | // At this point, key is still pending, and should not be sent to the application yet. |
| 6633 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6634 | ASSERT_FALSE(keySequenceNum); |
| 6635 | |
| 6636 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 6637 | tapOnWindow(); |
| 6638 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 6639 | // the other events yet. We can finish events in any order. |
| 6640 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 6641 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 6642 | mWindow->consumeMotionDown(); |
| 6643 | mWindow->consumeMotionUp(); |
| 6644 | mWindow->assertNoEvents(); |
| 6645 | } |
| 6646 | |
| 6647 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 6648 | virtual void SetUp() override { |
| 6649 | InputDispatcherTest::SetUp(); |
| 6650 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6651 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6652 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6653 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 6654 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6655 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6656 | // 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] | 6657 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6658 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6659 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 6660 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6661 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6662 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6663 | |
| 6664 | // Set focused application. |
| 6665 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6666 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6667 | |
| 6668 | // Expect one focus window exist in display. |
| 6669 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6670 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6671 | mFocusedWindow->consumeFocusEvent(true); |
| 6672 | } |
| 6673 | |
| 6674 | virtual void TearDown() override { |
| 6675 | InputDispatcherTest::TearDown(); |
| 6676 | |
| 6677 | mUnfocusedWindow.clear(); |
| 6678 | mFocusedWindow.clear(); |
| 6679 | } |
| 6680 | |
| 6681 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6682 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6683 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 6684 | sp<FakeWindowHandle> mFocusedWindow; |
| 6685 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 6686 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 6687 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 6688 | |
| 6689 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 6690 | |
| 6691 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 6692 | |
| 6693 | private: |
| 6694 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6695 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6696 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6697 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6698 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6699 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6700 | location)); |
| 6701 | } |
| 6702 | }; |
| 6703 | |
| 6704 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 6705 | // should be ANR'd first. |
| 6706 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6708 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6709 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6710 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6711 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6712 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6713 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6714 | // We consumed all events, so no ANR |
| 6715 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6716 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6717 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6718 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6719 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6720 | FOCUSED_WINDOW_LOCATION)); |
| 6721 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6722 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6723 | |
| 6724 | const std::chrono::duration timeout = |
| 6725 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6726 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6727 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 6728 | // sequence to make it consistent |
| 6729 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6730 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6731 | mFocusedWindow->consumeMotionDown(); |
| 6732 | // This cancel is generated because the connection was unresponsive |
| 6733 | mFocusedWindow->consumeMotionCancel(); |
| 6734 | mFocusedWindow->assertNoEvents(); |
| 6735 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6736 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6737 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6738 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6739 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6740 | } |
| 6741 | |
| 6742 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 6743 | // it doesn't matter which order they should have ANR. |
| 6744 | // But we should receive ANR for both. |
| 6745 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 6746 | // Set the timeout for unfocused window to match the focused window |
| 6747 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 6748 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6749 | |
| 6750 | tapOnFocusedWindow(); |
| 6751 | // 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] | 6752 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 6753 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 6754 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6755 | |
| 6756 | // 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] | 6757 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 6758 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 6759 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 6760 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6761 | |
| 6762 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6763 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6764 | |
| 6765 | mFocusedWindow->consumeMotionDown(); |
| 6766 | mFocusedWindow->consumeMotionUp(); |
| 6767 | mUnfocusedWindow->consumeMotionOutside(); |
| 6768 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6769 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 6770 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 6771 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6772 | |
| 6773 | // Both applications should be marked as responsive, in any order |
| 6774 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 6775 | mFocusedWindow->getToken() == responsiveToken2); |
| 6776 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 6777 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 6778 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6779 | } |
| 6780 | |
| 6781 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 6782 | // We should also log an error to account for the dropped event (not tested here). |
| 6783 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 6784 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 6785 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6786 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6787 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6788 | // Receive the events, but don't respond |
| 6789 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 6790 | ASSERT_TRUE(downEventSequenceNum); |
| 6791 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 6792 | ASSERT_TRUE(upEventSequenceNum); |
| 6793 | const std::chrono::duration timeout = |
| 6794 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6795 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6796 | |
| 6797 | // Tap once again |
| 6798 | // 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] | 6799 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6800 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6801 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6802 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6803 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6804 | FOCUSED_WINDOW_LOCATION)); |
| 6805 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 6806 | // valid touch target |
| 6807 | mUnfocusedWindow->assertNoEvents(); |
| 6808 | |
| 6809 | // Consume the first tap |
| 6810 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 6811 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 6812 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6813 | // The second tap did not go to the focused window |
| 6814 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6815 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6816 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6817 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6818 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6819 | } |
| 6820 | |
| 6821 | // If you tap outside of all windows, there will not be ANR |
| 6822 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6823 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6824 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6825 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 6826 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6827 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6828 | } |
| 6829 | |
| 6830 | // Since the focused window is paused, tapping on it should not produce any events |
| 6831 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 6832 | mFocusedWindow->setPaused(true); |
| 6833 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6834 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6835 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6836 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6837 | FOCUSED_WINDOW_LOCATION)); |
| 6838 | |
| 6839 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 6840 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6841 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 6842 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6843 | |
| 6844 | mFocusedWindow->assertNoEvents(); |
| 6845 | mUnfocusedWindow->assertNoEvents(); |
| 6846 | } |
| 6847 | |
| 6848 | /** |
| 6849 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6850 | * not to to the focused window until the motion is processed. |
| 6851 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 6852 | * |
| 6853 | * Warning!!! |
| 6854 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6855 | * and the injection timeout that we specify when injecting the key. |
| 6856 | * We must have the injection timeout (10ms) be smaller than |
| 6857 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6858 | * |
| 6859 | * If that value changes, this test should also change. |
| 6860 | */ |
| 6861 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 6862 | // Set a long ANR timeout to prevent it from triggering |
| 6863 | mFocusedWindow->setDispatchingTimeout(2s); |
| 6864 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6865 | |
| 6866 | tapOnUnfocusedWindow(); |
| 6867 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6868 | ASSERT_TRUE(downSequenceNum); |
| 6869 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6870 | ASSERT_TRUE(upSequenceNum); |
| 6871 | // Don't finish the events yet, and send a key |
| 6872 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 6873 | // window even if motions are still being processed. |
| 6874 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6875 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6876 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6877 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6879 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6880 | // and the key remains pending, waiting for the touch events to be processed |
| 6881 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 6882 | ASSERT_FALSE(keySequenceNum); |
| 6883 | |
| 6884 | // 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] | 6885 | mFocusedWindow->setFocusable(false); |
| 6886 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6887 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6888 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6889 | |
| 6890 | // Focus events should precede the key events |
| 6891 | mUnfocusedWindow->consumeFocusEvent(true); |
| 6892 | mFocusedWindow->consumeFocusEvent(false); |
| 6893 | |
| 6894 | // Finish the tap events, which should unblock dispatcher |
| 6895 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 6896 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 6897 | |
| 6898 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 6899 | // can finally go to the newly focused "mUnfocusedWindow". |
| 6900 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6901 | mFocusedWindow->assertNoEvents(); |
| 6902 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6903 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 6907 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 6908 | // The other window should not be affected by that. |
| 6909 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 6910 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6911 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6912 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6913 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6914 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6915 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6916 | |
| 6917 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6918 | mDispatcher->notifyMotion( |
| 6919 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6920 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6921 | |
| 6922 | const std::chrono::duration timeout = |
| 6923 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6924 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6925 | |
| 6926 | mUnfocusedWindow->consumeMotionDown(); |
| 6927 | mFocusedWindow->consumeMotionDown(); |
| 6928 | // Focused window may or may not receive ACTION_MOVE |
| 6929 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 6930 | InputEvent* event; |
| 6931 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 6932 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 6933 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 6934 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6935 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6936 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 6937 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 6938 | mFocusedWindow->consumeMotionCancel(); |
| 6939 | } else { |
| 6940 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 6941 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6942 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6943 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6944 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6945 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6946 | mUnfocusedWindow->assertNoEvents(); |
| 6947 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6948 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6949 | } |
| 6950 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6951 | /** |
| 6952 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 6953 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 6954 | * |
| 6955 | * If the user touches another application during this time, the key should be dropped. |
| 6956 | * Next, if a new focused window comes in, without toggling the focused application, |
| 6957 | * then no ANR should occur. |
| 6958 | * |
| 6959 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 6960 | * but in some cases the policy may not update the focused application. |
| 6961 | */ |
| 6962 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 6963 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 6964 | std::make_shared<FakeApplicationHandle>(); |
| 6965 | focusedApplication->setDispatchingTimeout(60ms); |
| 6966 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 6967 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 6968 | mFocusedWindow->setFocusable(false); |
| 6969 | |
| 6970 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6971 | mFocusedWindow->consumeFocusEvent(false); |
| 6972 | |
| 6973 | // Send a key. The ANR timer should start because there is no focused window. |
| 6974 | // 'focusedApplication' will get blamed if this timer completes. |
| 6975 | // 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] | 6976 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6977 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6978 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6979 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6981 | |
| 6982 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 6983 | // then the injected touches won't cause the focused event to get dropped. |
| 6984 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 6985 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 6986 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 6987 | // For this test, it means that the key would get delivered to the window once it becomes |
| 6988 | // focused. |
| 6989 | std::this_thread::sleep_for(10ms); |
| 6990 | |
| 6991 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6992 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6993 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6994 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 6995 | |
| 6996 | // We do not consume the motion right away, because that would require dispatcher to first |
| 6997 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 6998 | // Set the focused window first. |
| 6999 | mFocusedWindow->setFocusable(true); |
| 7000 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7001 | setFocusedWindow(mFocusedWindow); |
| 7002 | mFocusedWindow->consumeFocusEvent(true); |
| 7003 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 7004 | // to another application. This could be a bug / behaviour in the policy. |
| 7005 | |
| 7006 | mUnfocusedWindow->consumeMotionDown(); |
| 7007 | |
| 7008 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7009 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 7010 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 7011 | } |
| 7012 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7013 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 7014 | // that has feature NO_INPUT_CHANNEL. |
| 7015 | // Layout: |
| 7016 | // Top (closest to user) |
| 7017 | // mNoInputWindow (above all windows) |
| 7018 | // mBottomWindow |
| 7019 | // Bottom (furthest from user) |
| 7020 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 7021 | virtual void SetUp() override { |
| 7022 | InputDispatcherTest::SetUp(); |
| 7023 | |
| 7024 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7025 | mNoInputWindow = |
| 7026 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7027 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7028 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7029 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7030 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7031 | // It's perfectly valid for this window to not have an associated input channel |
| 7032 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7033 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 7034 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7035 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7036 | |
| 7037 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7038 | } |
| 7039 | |
| 7040 | protected: |
| 7041 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 7042 | sp<FakeWindowHandle> mNoInputWindow; |
| 7043 | sp<FakeWindowHandle> mBottomWindow; |
| 7044 | }; |
| 7045 | |
| 7046 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 7047 | PointF touchedPoint = {10, 10}; |
| 7048 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7049 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7050 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7051 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7052 | |
| 7053 | mNoInputWindow->assertNoEvents(); |
| 7054 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 7055 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 7056 | // and therefore should prevent mBottomWindow from receiving touches |
| 7057 | mBottomWindow->assertNoEvents(); |
| 7058 | } |
| 7059 | |
| 7060 | /** |
| 7061 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 7062 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 7063 | */ |
| 7064 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 7065 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7066 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7067 | "Window with input channel and NO_INPUT_CHANNEL", |
| 7068 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7069 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7070 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7071 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7072 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7073 | |
| 7074 | PointF touchedPoint = {10, 10}; |
| 7075 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7076 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7077 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7078 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7079 | |
| 7080 | mNoInputWindow->assertNoEvents(); |
| 7081 | mBottomWindow->assertNoEvents(); |
| 7082 | } |
| 7083 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7084 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7085 | protected: |
| 7086 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7087 | sp<FakeWindowHandle> mWindow; |
| 7088 | sp<FakeWindowHandle> mMirror; |
| 7089 | |
| 7090 | virtual void SetUp() override { |
| 7091 | InputDispatcherTest::SetUp(); |
| 7092 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7093 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7094 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7095 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7096 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7097 | mWindow->setFocusable(true); |
| 7098 | mMirror->setFocusable(true); |
| 7099 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7100 | } |
| 7101 | }; |
| 7102 | |
| 7103 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7104 | // Request focus on a mirrored window |
| 7105 | setFocusedWindow(mMirror); |
| 7106 | |
| 7107 | // window gets focused |
| 7108 | mWindow->consumeFocusEvent(true); |
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); |
| 7112 | } |
| 7113 | |
| 7114 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7115 | // focusable. |
| 7116 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7117 | setFocusedWindow(mMirror); |
| 7118 | |
| 7119 | // window gets focused |
| 7120 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7121 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7122 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7123 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7124 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7125 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7126 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7127 | |
| 7128 | mMirror->setFocusable(false); |
| 7129 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7130 | |
| 7131 | // window loses focus since one of the windows associated with the token in not focusable |
| 7132 | mWindow->consumeFocusEvent(false); |
| 7133 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7134 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7135 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7136 | mWindow->assertNoEvents(); |
| 7137 | } |
| 7138 | |
| 7139 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7140 | // invisible. |
| 7141 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 7142 | setFocusedWindow(mMirror); |
| 7143 | |
| 7144 | // window gets focused |
| 7145 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7146 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7147 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7148 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7149 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7150 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7151 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7152 | |
| 7153 | mMirror->setVisible(false); |
| 7154 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7155 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7157 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7158 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7159 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7160 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7161 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7162 | |
| 7163 | mWindow->setVisible(false); |
| 7164 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7165 | |
| 7166 | // window loses focus only after all windows associated with the token become invisible. |
| 7167 | mWindow->consumeFocusEvent(false); |
| 7168 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7169 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7170 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7171 | mWindow->assertNoEvents(); |
| 7172 | } |
| 7173 | |
| 7174 | // A focused & mirrored window remains focused until both windows are removed. |
| 7175 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7176 | setFocusedWindow(mMirror); |
| 7177 | |
| 7178 | // window gets focused |
| 7179 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7180 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7181 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7182 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7183 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7184 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7185 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7186 | |
| 7187 | // single window is removed but the window token remains focused |
| 7188 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7189 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7190 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7191 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7192 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7194 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7195 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7196 | |
| 7197 | // Both windows are removed |
| 7198 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7199 | mWindow->consumeFocusEvent(false); |
| 7200 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7201 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7202 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7203 | mWindow->assertNoEvents(); |
| 7204 | } |
| 7205 | |
| 7206 | // Focus request can be pending until one window becomes visible. |
| 7207 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7208 | // Request focus on an invisible mirror. |
| 7209 | mWindow->setVisible(false); |
| 7210 | mMirror->setVisible(false); |
| 7211 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7212 | setFocusedWindow(mMirror); |
| 7213 | |
| 7214 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7216 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7217 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7218 | |
| 7219 | mMirror->setVisible(true); |
| 7220 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7221 | |
| 7222 | // window gets focused |
| 7223 | mWindow->consumeFocusEvent(true); |
| 7224 | // window gets the pending key event |
| 7225 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7226 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7227 | |
| 7228 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7229 | protected: |
| 7230 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7231 | sp<FakeWindowHandle> mWindow; |
| 7232 | sp<FakeWindowHandle> mSecondWindow; |
| 7233 | |
| 7234 | void SetUp() override { |
| 7235 | InputDispatcherTest::SetUp(); |
| 7236 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7237 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7238 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7239 | mSecondWindow = |
| 7240 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7241 | mSecondWindow->setFocusable(true); |
| 7242 | |
| 7243 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7244 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7245 | |
| 7246 | setFocusedWindow(mWindow); |
| 7247 | mWindow->consumeFocusEvent(true); |
| 7248 | } |
| 7249 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7250 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7251 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7252 | } |
| 7253 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7254 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7255 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7256 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7257 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7258 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7259 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7260 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7261 | } |
| 7262 | }; |
| 7263 | |
| 7264 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7265 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7266 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7267 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7268 | mSecondWindow->assertNoEvents(); |
| 7269 | |
| 7270 | // Ensure that capture can be enabled from the focus window. |
| 7271 | requestAndVerifyPointerCapture(mWindow, true); |
| 7272 | |
| 7273 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7274 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7275 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7276 | |
| 7277 | // Ensure that capture can be disabled from the window with capture. |
| 7278 | requestAndVerifyPointerCapture(mWindow, false); |
| 7279 | } |
| 7280 | |
| 7281 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7282 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7283 | |
| 7284 | setFocusedWindow(mSecondWindow); |
| 7285 | |
| 7286 | // Ensure that the capture disabled event was sent first. |
| 7287 | mWindow->consumeCaptureEvent(false); |
| 7288 | mWindow->consumeFocusEvent(false); |
| 7289 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7290 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7291 | |
| 7292 | // 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] | 7293 | notifyPointerCaptureChanged({}); |
| 7294 | notifyPointerCaptureChanged(request); |
| 7295 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7296 | mWindow->assertNoEvents(); |
| 7297 | mSecondWindow->assertNoEvents(); |
| 7298 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7299 | } |
| 7300 | |
| 7301 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7302 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7303 | |
| 7304 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7305 | notifyPointerCaptureChanged({}); |
| 7306 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7307 | |
| 7308 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7309 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7310 | mWindow->consumeCaptureEvent(false); |
| 7311 | mWindow->assertNoEvents(); |
| 7312 | } |
| 7313 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7314 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7315 | requestAndVerifyPointerCapture(mWindow, true); |
| 7316 | |
| 7317 | // The first window loses focus. |
| 7318 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7319 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7320 | mWindow->consumeCaptureEvent(false); |
| 7321 | |
| 7322 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7323 | // arrives. |
| 7324 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7325 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7326 | |
| 7327 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7328 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7329 | |
| 7330 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7331 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7332 | |
| 7333 | mSecondWindow->consumeFocusEvent(true); |
| 7334 | mSecondWindow->consumeCaptureEvent(true); |
| 7335 | } |
| 7336 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7337 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7338 | // App repeatedly enables and disables capture. |
| 7339 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7340 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7341 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7342 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7343 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7344 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7345 | |
| 7346 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7347 | // first request is now stale, this should do nothing. |
| 7348 | notifyPointerCaptureChanged(firstRequest); |
| 7349 | mWindow->assertNoEvents(); |
| 7350 | |
| 7351 | // InputReader notifies that the second request was enabled. |
| 7352 | notifyPointerCaptureChanged(secondRequest); |
| 7353 | mWindow->consumeCaptureEvent(true); |
| 7354 | } |
| 7355 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7356 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7357 | requestAndVerifyPointerCapture(mWindow, true); |
| 7358 | |
| 7359 | // App toggles pointer capture off and on. |
| 7360 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7361 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7362 | |
| 7363 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7364 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7365 | |
| 7366 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7367 | // preceding "disable" request. |
| 7368 | notifyPointerCaptureChanged(enableRequest); |
| 7369 | |
| 7370 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7371 | // any notifications. |
| 7372 | mWindow->assertNoEvents(); |
| 7373 | } |
| 7374 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7375 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7376 | protected: |
| 7377 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7378 | |
| 7379 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7380 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7381 | |
| 7382 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7383 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7384 | |
| 7385 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7386 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7387 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7388 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7389 | MAXIMUM_OBSCURING_OPACITY); |
| 7390 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7391 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7392 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7393 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7394 | |
| 7395 | sp<FakeWindowHandle> mTouchWindow; |
| 7396 | |
| 7397 | virtual void SetUp() override { |
| 7398 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7399 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7400 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7401 | } |
| 7402 | |
| 7403 | virtual void TearDown() override { |
| 7404 | InputDispatcherTest::TearDown(); |
| 7405 | mTouchWindow.clear(); |
| 7406 | } |
| 7407 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7408 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 7409 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7410 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7411 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7412 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7413 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7414 | return window; |
| 7415 | } |
| 7416 | |
| 7417 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 7418 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7419 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7420 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7421 | // Generate an arbitrary PID based on the UID |
| 7422 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 7423 | return window; |
| 7424 | } |
| 7425 | |
| 7426 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7427 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7428 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7429 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7430 | } |
| 7431 | }; |
| 7432 | |
| 7433 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7434 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7435 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7436 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7437 | |
| 7438 | touch(); |
| 7439 | |
| 7440 | mTouchWindow->assertNoEvents(); |
| 7441 | } |
| 7442 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7443 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7444 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7445 | const sp<FakeWindowHandle>& w = |
| 7446 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7448 | |
| 7449 | touch(); |
| 7450 | |
| 7451 | mTouchWindow->assertNoEvents(); |
| 7452 | } |
| 7453 | |
| 7454 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7455 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7456 | const sp<FakeWindowHandle>& w = |
| 7457 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7458 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7459 | |
| 7460 | touch(); |
| 7461 | |
| 7462 | w->assertNoEvents(); |
| 7463 | } |
| 7464 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7465 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7466 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7467 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7468 | |
| 7469 | touch(); |
| 7470 | |
| 7471 | mTouchWindow->consumeAnyMotionDown(); |
| 7472 | } |
| 7473 | |
| 7474 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7475 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7476 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7477 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7478 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7479 | |
| 7480 | touch({PointF{100, 100}}); |
| 7481 | |
| 7482 | mTouchWindow->consumeAnyMotionDown(); |
| 7483 | } |
| 7484 | |
| 7485 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7486 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7487 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7488 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7489 | |
| 7490 | touch(); |
| 7491 | |
| 7492 | mTouchWindow->consumeAnyMotionDown(); |
| 7493 | } |
| 7494 | |
| 7495 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 7496 | const sp<FakeWindowHandle>& w = |
| 7497 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7498 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7499 | |
| 7500 | touch(); |
| 7501 | |
| 7502 | mTouchWindow->consumeAnyMotionDown(); |
| 7503 | } |
| 7504 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7505 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 7506 | const sp<FakeWindowHandle>& w = |
| 7507 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7508 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7509 | |
| 7510 | touch(); |
| 7511 | |
| 7512 | w->assertNoEvents(); |
| 7513 | } |
| 7514 | |
| 7515 | /** |
| 7516 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 7517 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 7518 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 7519 | */ |
| 7520 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7521 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 7522 | const sp<FakeWindowHandle>& w = |
| 7523 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7524 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7525 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7526 | |
| 7527 | touch(); |
| 7528 | |
| 7529 | w->consumeMotionOutside(); |
| 7530 | } |
| 7531 | |
| 7532 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 7533 | const sp<FakeWindowHandle>& w = |
| 7534 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7535 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7536 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7537 | |
| 7538 | touch(); |
| 7539 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7540 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7541 | } |
| 7542 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7543 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7544 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7545 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7546 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7547 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7548 | |
| 7549 | touch(); |
| 7550 | |
| 7551 | mTouchWindow->consumeAnyMotionDown(); |
| 7552 | } |
| 7553 | |
| 7554 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 7555 | const sp<FakeWindowHandle>& w = |
| 7556 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7557 | MAXIMUM_OBSCURING_OPACITY); |
| 7558 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7559 | |
| 7560 | touch(); |
| 7561 | |
| 7562 | mTouchWindow->consumeAnyMotionDown(); |
| 7563 | } |
| 7564 | |
| 7565 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7566 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7567 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7568 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7569 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7570 | |
| 7571 | touch(); |
| 7572 | |
| 7573 | mTouchWindow->assertNoEvents(); |
| 7574 | } |
| 7575 | |
| 7576 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 7577 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 7578 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7579 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7580 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7581 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7582 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7583 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7584 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7585 | |
| 7586 | touch(); |
| 7587 | |
| 7588 | mTouchWindow->assertNoEvents(); |
| 7589 | } |
| 7590 | |
| 7591 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 7592 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 7593 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7594 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7595 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7596 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7597 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7598 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7599 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7600 | |
| 7601 | touch(); |
| 7602 | |
| 7603 | mTouchWindow->consumeAnyMotionDown(); |
| 7604 | } |
| 7605 | |
| 7606 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7607 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 7608 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7609 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7610 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7611 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7612 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7613 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7614 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7615 | |
| 7616 | touch(); |
| 7617 | |
| 7618 | mTouchWindow->consumeAnyMotionDown(); |
| 7619 | } |
| 7620 | |
| 7621 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 7622 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7623 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7624 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7625 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7626 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7627 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7628 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7629 | |
| 7630 | touch(); |
| 7631 | |
| 7632 | mTouchWindow->assertNoEvents(); |
| 7633 | } |
| 7634 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7635 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7636 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 7637 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7638 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7639 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7640 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7641 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7642 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7643 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7644 | |
| 7645 | touch(); |
| 7646 | |
| 7647 | mTouchWindow->assertNoEvents(); |
| 7648 | } |
| 7649 | |
| 7650 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7651 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 7652 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7653 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7654 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7655 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7656 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7657 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7658 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7659 | |
| 7660 | touch(); |
| 7661 | |
| 7662 | mTouchWindow->consumeAnyMotionDown(); |
| 7663 | } |
| 7664 | |
| 7665 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 7666 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7667 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7668 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7669 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7670 | |
| 7671 | touch(); |
| 7672 | |
| 7673 | mTouchWindow->consumeAnyMotionDown(); |
| 7674 | } |
| 7675 | |
| 7676 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 7677 | const sp<FakeWindowHandle>& w = |
| 7678 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7679 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7680 | |
| 7681 | touch(); |
| 7682 | |
| 7683 | mTouchWindow->consumeAnyMotionDown(); |
| 7684 | } |
| 7685 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7686 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7687 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 7688 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7689 | const sp<FakeWindowHandle>& w = |
| 7690 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 7691 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7692 | |
| 7693 | touch(); |
| 7694 | |
| 7695 | mTouchWindow->assertNoEvents(); |
| 7696 | } |
| 7697 | |
| 7698 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 7699 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7700 | const sp<FakeWindowHandle>& w = |
| 7701 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 7702 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7703 | |
| 7704 | touch(); |
| 7705 | |
| 7706 | mTouchWindow->consumeAnyMotionDown(); |
| 7707 | } |
| 7708 | |
| 7709 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7710 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 7711 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 7712 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7713 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7714 | OPACITY_ABOVE_THRESHOLD); |
| 7715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7716 | |
| 7717 | touch(); |
| 7718 | |
| 7719 | mTouchWindow->consumeAnyMotionDown(); |
| 7720 | } |
| 7721 | |
| 7722 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7723 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 7724 | const sp<FakeWindowHandle>& w1 = |
| 7725 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7726 | OPACITY_BELOW_THRESHOLD); |
| 7727 | const sp<FakeWindowHandle>& w2 = |
| 7728 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7729 | OPACITY_BELOW_THRESHOLD); |
| 7730 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7731 | |
| 7732 | touch(); |
| 7733 | |
| 7734 | mTouchWindow->assertNoEvents(); |
| 7735 | } |
| 7736 | |
| 7737 | /** |
| 7738 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 7739 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 7740 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 7741 | */ |
| 7742 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7743 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 7744 | const sp<FakeWindowHandle>& wB = |
| 7745 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7746 | OPACITY_BELOW_THRESHOLD); |
| 7747 | const sp<FakeWindowHandle>& wC = |
| 7748 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7749 | OPACITY_BELOW_THRESHOLD); |
| 7750 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7751 | |
| 7752 | touch(); |
| 7753 | |
| 7754 | mTouchWindow->assertNoEvents(); |
| 7755 | } |
| 7756 | |
| 7757 | /** |
| 7758 | * This test is testing that a window from a different UID but with same application token doesn't |
| 7759 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 7760 | */ |
| 7761 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7762 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 7763 | const sp<FakeWindowHandle>& w = |
| 7764 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7765 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7766 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7767 | |
| 7768 | touch(); |
| 7769 | |
| 7770 | mTouchWindow->consumeAnyMotionDown(); |
| 7771 | } |
| 7772 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7773 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 7774 | protected: |
| 7775 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7776 | sp<FakeWindowHandle> mWindow; |
| 7777 | sp<FakeWindowHandle> mSecondWindow; |
| 7778 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7779 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7780 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 7781 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7782 | |
| 7783 | void SetUp() override { |
| 7784 | InputDispatcherTest::SetUp(); |
| 7785 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7786 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7787 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7788 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7789 | mSecondWindow = |
| 7790 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7791 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7792 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7793 | mSpyWindow = |
| 7794 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7795 | mSpyWindow->setSpy(true); |
| 7796 | mSpyWindow->setTrustedOverlay(true); |
| 7797 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 7798 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7799 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7800 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7801 | } |
| 7802 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7803 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 7804 | switch (fromSource) { |
| 7805 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 7806 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7807 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7808 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7809 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7810 | break; |
| 7811 | case AINPUT_SOURCE_STYLUS: |
| 7812 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7813 | injectMotionEvent( |
| 7814 | mDispatcher, |
| 7815 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7816 | AINPUT_SOURCE_STYLUS) |
| 7817 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7818 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7819 | .x(50) |
| 7820 | .y(50)) |
| 7821 | .build())); |
| 7822 | break; |
| 7823 | case AINPUT_SOURCE_MOUSE: |
| 7824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7825 | injectMotionEvent( |
| 7826 | mDispatcher, |
| 7827 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7828 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7829 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7830 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7831 | .x(50) |
| 7832 | .y(50)) |
| 7833 | .build())); |
| 7834 | break; |
| 7835 | default: |
| 7836 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 7837 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7838 | |
| 7839 | // Window should receive motion event. |
| 7840 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7841 | // Spy window should also receive motion event |
| 7842 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7843 | } |
| 7844 | |
| 7845 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 7846 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 7847 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7848 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7849 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7850 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7851 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7852 | |
| 7853 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7854 | mDragWindow = |
| 7855 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7856 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7857 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7858 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7859 | |
| 7860 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7861 | bool transferred = |
| 7862 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7863 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7864 | if (transferred) { |
| 7865 | mWindow->consumeMotionCancel(); |
| 7866 | mDragWindow->consumeMotionDown(); |
| 7867 | } |
| 7868 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7869 | } |
| 7870 | }; |
| 7871 | |
| 7872 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7873 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7874 | |
| 7875 | // Move on window. |
| 7876 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7877 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7878 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7879 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7880 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7881 | mWindow->consumeDragEvent(false, 50, 50); |
| 7882 | mSecondWindow->assertNoEvents(); |
| 7883 | |
| 7884 | // Move to another window. |
| 7885 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7886 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7887 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7888 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7889 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7890 | mWindow->consumeDragEvent(true, 150, 50); |
| 7891 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7892 | |
| 7893 | // Move back to original window. |
| 7894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7895 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7896 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7897 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7898 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7899 | mWindow->consumeDragEvent(false, 50, 50); |
| 7900 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 7901 | |
| 7902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7903 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7904 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7905 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7906 | mWindow->assertNoEvents(); |
| 7907 | mSecondWindow->assertNoEvents(); |
| 7908 | } |
| 7909 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7910 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7911 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7912 | |
| 7913 | // No cancel event after drag start |
| 7914 | mSpyWindow->assertNoEvents(); |
| 7915 | |
| 7916 | const MotionEvent secondFingerDownEvent = |
| 7917 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7918 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7919 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 7920 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7921 | .build(); |
| 7922 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7923 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 7924 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 7925 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7926 | |
| 7927 | // Receives cancel for first pointer after next pointer down |
| 7928 | mSpyWindow->consumeMotionCancel(); |
| 7929 | mSpyWindow->consumeMotionDown(); |
| 7930 | |
| 7931 | mSpyWindow->assertNoEvents(); |
| 7932 | } |
| 7933 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7934 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7935 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 7936 | |
| 7937 | // Move on window. |
| 7938 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7939 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7940 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7941 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7942 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7943 | mWindow->consumeDragEvent(false, 50, 50); |
| 7944 | mSecondWindow->assertNoEvents(); |
| 7945 | |
| 7946 | // Move to another window. |
| 7947 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7948 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7949 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7950 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7951 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7952 | mWindow->consumeDragEvent(true, 150, 50); |
| 7953 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7954 | |
| 7955 | // drop to another window. |
| 7956 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7957 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7958 | {150, 50})) |
| 7959 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7960 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7961 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7962 | mWindow->assertNoEvents(); |
| 7963 | mSecondWindow->assertNoEvents(); |
| 7964 | } |
| 7965 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7966 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7967 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7968 | |
| 7969 | // Move on window and keep button pressed. |
| 7970 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7971 | injectMotionEvent(mDispatcher, |
| 7972 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7973 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7974 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7975 | .build())) |
| 7976 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7977 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7978 | mWindow->consumeDragEvent(false, 50, 50); |
| 7979 | mSecondWindow->assertNoEvents(); |
| 7980 | |
| 7981 | // Move to another window and release button, expect to drop item. |
| 7982 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7983 | injectMotionEvent(mDispatcher, |
| 7984 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 7985 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7986 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 7987 | .build())) |
| 7988 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7989 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7990 | mWindow->assertNoEvents(); |
| 7991 | mSecondWindow->assertNoEvents(); |
| 7992 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 7993 | |
| 7994 | // nothing to the window. |
| 7995 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7996 | injectMotionEvent(mDispatcher, |
| 7997 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 7998 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7999 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8000 | .build())) |
| 8001 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8002 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8003 | mWindow->assertNoEvents(); |
| 8004 | mSecondWindow->assertNoEvents(); |
| 8005 | } |
| 8006 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8007 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8008 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8009 | |
| 8010 | // Set second window invisible. |
| 8011 | mSecondWindow->setVisible(false); |
| 8012 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 8013 | |
| 8014 | // Move on window. |
| 8015 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8016 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8017 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8018 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8019 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8020 | mWindow->consumeDragEvent(false, 50, 50); |
| 8021 | mSecondWindow->assertNoEvents(); |
| 8022 | |
| 8023 | // Move to another window. |
| 8024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8025 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8026 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8027 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8028 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8029 | mWindow->consumeDragEvent(true, 150, 50); |
| 8030 | mSecondWindow->assertNoEvents(); |
| 8031 | |
| 8032 | // drop to another window. |
| 8033 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8034 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8035 | {150, 50})) |
| 8036 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8037 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8038 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 8039 | mWindow->assertNoEvents(); |
| 8040 | mSecondWindow->assertNoEvents(); |
| 8041 | } |
| 8042 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8043 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8044 | // Ensure window could track pointerIds if it didn't support split touch. |
| 8045 | mWindow->setPreventSplitting(true); |
| 8046 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8048 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8049 | {50, 50})) |
| 8050 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8051 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8052 | |
| 8053 | const MotionEvent secondFingerDownEvent = |
| 8054 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8055 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8056 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8057 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8058 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8059 | .build(); |
| 8060 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8061 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8062 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8063 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8064 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8065 | |
| 8066 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8067 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8068 | } |
| 8069 | |
| 8070 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 8071 | // First down on second window. |
| 8072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8073 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8074 | {150, 50})) |
| 8075 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8076 | |
| 8077 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8078 | |
| 8079 | // Second down on first window. |
| 8080 | const MotionEvent secondFingerDownEvent = |
| 8081 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8082 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8083 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8084 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8085 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8086 | .build(); |
| 8087 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8088 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8089 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8090 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8091 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8092 | |
| 8093 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8094 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8095 | |
| 8096 | // Move on window. |
| 8097 | const MotionEvent secondFingerMoveEvent = |
| 8098 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8099 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8100 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8101 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8102 | .build(); |
| 8103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8104 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8105 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8106 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8107 | mWindow->consumeDragEvent(false, 50, 50); |
| 8108 | mSecondWindow->consumeMotionMove(); |
| 8109 | |
| 8110 | // Release the drag pointer should perform drop. |
| 8111 | const MotionEvent secondFingerUpEvent = |
| 8112 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8113 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8114 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8115 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8116 | .build(); |
| 8117 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8118 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8119 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8120 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8121 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 8122 | mWindow->assertNoEvents(); |
| 8123 | mSecondWindow->consumeMotionMove(); |
| 8124 | } |
| 8125 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8126 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8127 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8128 | |
| 8129 | // Update window of second display. |
| 8130 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8131 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8132 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8133 | |
| 8134 | // Let second display has a touch state. |
| 8135 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8136 | injectMotionEvent(mDispatcher, |
| 8137 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8138 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8139 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8140 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8141 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8142 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8143 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8144 | // Update window again. |
| 8145 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8146 | |
| 8147 | // Move on window. |
| 8148 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8149 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8150 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8151 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8152 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8153 | mWindow->consumeDragEvent(false, 50, 50); |
| 8154 | mSecondWindow->assertNoEvents(); |
| 8155 | |
| 8156 | // Move to another window. |
| 8157 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8158 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8159 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8160 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8161 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8162 | mWindow->consumeDragEvent(true, 150, 50); |
| 8163 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8164 | |
| 8165 | // drop to another window. |
| 8166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8167 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8168 | {150, 50})) |
| 8169 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8170 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8171 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8172 | mWindow->assertNoEvents(); |
| 8173 | mSecondWindow->assertNoEvents(); |
| 8174 | } |
| 8175 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8176 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8177 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8178 | // Move on window. |
| 8179 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8180 | injectMotionEvent(mDispatcher, |
| 8181 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8182 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8183 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8184 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8185 | .x(50) |
| 8186 | .y(50)) |
| 8187 | .build())) |
| 8188 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8189 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8190 | mWindow->consumeDragEvent(false, 50, 50); |
| 8191 | mSecondWindow->assertNoEvents(); |
| 8192 | |
| 8193 | // Move to another window. |
| 8194 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8195 | injectMotionEvent(mDispatcher, |
| 8196 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8197 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8198 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8199 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8200 | .x(150) |
| 8201 | .y(50)) |
| 8202 | .build())) |
| 8203 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8204 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8205 | mWindow->consumeDragEvent(true, 150, 50); |
| 8206 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8207 | |
| 8208 | // drop to another window. |
| 8209 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8210 | injectMotionEvent(mDispatcher, |
| 8211 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8212 | .buttonState(0) |
| 8213 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8214 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8215 | .x(150) |
| 8216 | .y(50)) |
| 8217 | .build())) |
| 8218 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8219 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8220 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8221 | mWindow->assertNoEvents(); |
| 8222 | mSecondWindow->assertNoEvents(); |
| 8223 | } |
| 8224 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8225 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8226 | |
| 8227 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8228 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8229 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8230 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8231 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8232 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8233 | window->setFocusable(true); |
| 8234 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8235 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8236 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8237 | |
| 8238 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8239 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8240 | window->assertNoEvents(); |
| 8241 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8242 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8243 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8244 | window->assertNoEvents(); |
| 8245 | |
| 8246 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8247 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8248 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8249 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8250 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8251 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8252 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8253 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8254 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8255 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8256 | window->assertNoEvents(); |
| 8257 | } |
| 8258 | |
| 8259 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8260 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8261 | std::make_shared<FakeApplicationHandle>(); |
| 8262 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8263 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8264 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8265 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8266 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8267 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8268 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8269 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8270 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8271 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8272 | window->setOwnerInfo(222, 222); |
| 8273 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8274 | window->setFocusable(true); |
| 8275 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8276 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8277 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8278 | |
| 8279 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8280 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8281 | window->assertNoEvents(); |
| 8282 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8283 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8284 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8285 | window->assertNoEvents(); |
| 8286 | |
| 8287 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8288 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8289 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8290 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8291 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8292 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8293 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8294 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8295 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8296 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8297 | window->assertNoEvents(); |
| 8298 | } |
| 8299 | |
| 8300 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8301 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8302 | std::make_shared<FakeApplicationHandle>(); |
| 8303 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8304 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8305 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8306 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8307 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8308 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8309 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8310 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8311 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8312 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8313 | window->setOwnerInfo(222, 222); |
| 8314 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8315 | window->setFocusable(true); |
| 8316 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8317 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8318 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8319 | |
| 8320 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8321 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8322 | window->assertNoEvents(); |
| 8323 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8324 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8325 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8326 | window->assertNoEvents(); |
| 8327 | |
| 8328 | // When the window is no longer obscured because it went on top, it should get input |
| 8329 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8330 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8331 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8332 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8333 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8334 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8335 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8336 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8337 | window->assertNoEvents(); |
| 8338 | } |
| 8339 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8340 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8341 | protected: |
| 8342 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8343 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8344 | sp<FakeWindowHandle> mWindow; |
| 8345 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8346 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8347 | |
| 8348 | void SetUp() override { |
| 8349 | InputDispatcherTest::SetUp(); |
| 8350 | |
| 8351 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8352 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8353 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8354 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8355 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8356 | mSecondWindow = |
| 8357 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8358 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8359 | mThirdWindow = |
| 8360 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8361 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8362 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8363 | |
| 8364 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8365 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8366 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8367 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8368 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8369 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8370 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8371 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8372 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8373 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8374 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8375 | mThirdWindow->assertNoEvents(); |
| 8376 | } |
| 8377 | |
| 8378 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8379 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8380 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8381 | SECOND_DISPLAY_ID)) { |
| 8382 | mWindow->assertNoEvents(); |
| 8383 | mSecondWindow->assertNoEvents(); |
| 8384 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8385 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8388 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 8389 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8390 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8391 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8392 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8393 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8394 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8395 | } |
| 8396 | }; |
| 8397 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8398 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8399 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8400 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8401 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8402 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8403 | } |
| 8404 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8405 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8406 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8407 | int32_t ownerPid = windowInfo.ownerPid; |
| 8408 | int32_t ownerUid = windowInfo.ownerUid; |
| 8409 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 8410 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8411 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8412 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8413 | mWindow->assertNoEvents(); |
| 8414 | mSecondWindow->assertNoEvents(); |
| 8415 | } |
| 8416 | |
| 8417 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8418 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8419 | int32_t ownerPid = windowInfo.ownerPid; |
| 8420 | int32_t ownerUid = windowInfo.ownerUid; |
| 8421 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8422 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8423 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8424 | } |
| 8425 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8426 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8427 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8428 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8429 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8430 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8431 | mWindow->assertNoEvents(); |
| 8432 | mSecondWindow->assertNoEvents(); |
| 8433 | } |
| 8434 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8435 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8436 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8437 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8438 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8439 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8440 | mWindow->assertNoEvents(); |
| 8441 | mSecondWindow->assertNoEvents(); |
| 8442 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8443 | } |
| 8444 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8445 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8446 | // Interact with the window first. |
| 8447 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8448 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8449 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8450 | |
| 8451 | // Then remove focus. |
| 8452 | mWindow->setFocusable(false); |
| 8453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8454 | |
| 8455 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8456 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8457 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8458 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8459 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8460 | } |
| 8461 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8462 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8463 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8464 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8465 | std::shared_ptr<FakeApplicationHandle> application = |
| 8466 | std::make_shared<FakeApplicationHandle>(); |
| 8467 | std::string name = "Fake Spy "; |
| 8468 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8469 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8470 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8471 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8472 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8473 | return spy; |
| 8474 | } |
| 8475 | |
| 8476 | sp<FakeWindowHandle> createForeground() { |
| 8477 | std::shared_ptr<FakeApplicationHandle> application = |
| 8478 | std::make_shared<FakeApplicationHandle>(); |
| 8479 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8480 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 8481 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8482 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8483 | return window; |
| 8484 | } |
| 8485 | |
| 8486 | private: |
| 8487 | int mSpyCount{0}; |
| 8488 | }; |
| 8489 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8490 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8491 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8492 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 8493 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8494 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 8495 | ScopedSilentDeath _silentDeath; |
| 8496 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8497 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8498 | spy->setTrustedOverlay(false); |
| 8499 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 8500 | ".* not a trusted overlay"); |
| 8501 | } |
| 8502 | |
| 8503 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8504 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 8505 | */ |
| 8506 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8507 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8508 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 8509 | |
| 8510 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8511 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8512 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8513 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8514 | } |
| 8515 | |
| 8516 | /** |
| 8517 | * Verify the order in which different input windows receive events. The touched foreground window |
| 8518 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 8519 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 8520 | * receive events before ones belows it. |
| 8521 | * |
| 8522 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 8523 | * spy1, spy2, window, spy3. |
| 8524 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 8525 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 8526 | * window. |
| 8527 | */ |
| 8528 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 8529 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8530 | auto spy1 = createSpy(); |
| 8531 | auto spy2 = createSpy(); |
| 8532 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8533 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 8534 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 8535 | const size_t numChannels = channels.size(); |
| 8536 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 8537 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8538 | if (!epollFd.ok()) { |
| 8539 | FAIL() << "Failed to create epoll fd"; |
| 8540 | } |
| 8541 | |
| 8542 | for (size_t i = 0; i < numChannels; i++) { |
| 8543 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 8544 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 8545 | FAIL() << "Failed to add fd to epoll"; |
| 8546 | } |
| 8547 | } |
| 8548 | |
| 8549 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8550 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8551 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8552 | |
| 8553 | std::vector<size_t> eventOrder; |
| 8554 | std::vector<struct epoll_event> events(numChannels); |
| 8555 | for (;;) { |
| 8556 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 8557 | (100ms).count()); |
| 8558 | if (nFds < 0) { |
| 8559 | FAIL() << "Failed to call epoll_wait"; |
| 8560 | } |
| 8561 | if (nFds == 0) { |
| 8562 | break; // epoll_wait timed out |
| 8563 | } |
| 8564 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 8565 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 8566 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8567 | channels[i]->consumeMotionDown(); |
| 8568 | } |
| 8569 | } |
| 8570 | |
| 8571 | // Verify the order in which the events were received. |
| 8572 | EXPECT_EQ(3u, eventOrder.size()); |
| 8573 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 8574 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 8575 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 8576 | } |
| 8577 | |
| 8578 | /** |
| 8579 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 8580 | */ |
| 8581 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 8582 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8583 | auto spy = createSpy(); |
| 8584 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8585 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8586 | |
| 8587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8588 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8589 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8590 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8591 | spy->assertNoEvents(); |
| 8592 | } |
| 8593 | |
| 8594 | /** |
| 8595 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 8596 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 8597 | * to the window. |
| 8598 | */ |
| 8599 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 8600 | auto window = createForeground(); |
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->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 8603 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8604 | |
| 8605 | // Inject an event outside the spy window's touchable region. |
| 8606 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8607 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8608 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8609 | window->consumeMotionDown(); |
| 8610 | spy->assertNoEvents(); |
| 8611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8612 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8613 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8614 | window->consumeMotionUp(); |
| 8615 | spy->assertNoEvents(); |
| 8616 | |
| 8617 | // Inject an event inside the spy window's touchable region. |
| 8618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8619 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8620 | {5, 10})) |
| 8621 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8622 | window->consumeMotionDown(); |
| 8623 | spy->consumeMotionDown(); |
| 8624 | } |
| 8625 | |
| 8626 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8627 | * 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] | 8628 | * 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] | 8629 | */ |
| 8630 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 8631 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8632 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8633 | auto spy = createSpy(); |
| 8634 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8635 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8636 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 8637 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8638 | |
| 8639 | // Inject an event outside the spy window's frame and touchable region. |
| 8640 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8641 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8642 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8643 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8644 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8645 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8646 | } |
| 8647 | |
| 8648 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8649 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 8650 | * pointers that are down within its bounds. |
| 8651 | */ |
| 8652 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 8653 | auto windowLeft = createForeground(); |
| 8654 | windowLeft->setFrame({0, 0, 100, 200}); |
| 8655 | auto windowRight = createForeground(); |
| 8656 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8657 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8658 | spy->setFrame({0, 0, 200, 200}); |
| 8659 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 8660 | |
| 8661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8662 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8663 | {50, 50})) |
| 8664 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8665 | windowLeft->consumeMotionDown(); |
| 8666 | spy->consumeMotionDown(); |
| 8667 | |
| 8668 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8669 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8670 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8671 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8672 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8673 | .build(); |
| 8674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8675 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8676 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8677 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8678 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8679 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8680 | } |
| 8681 | |
| 8682 | /** |
| 8683 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 8684 | * the spy should receive the second pointer with ACTION_DOWN. |
| 8685 | */ |
| 8686 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 8687 | auto window = createForeground(); |
| 8688 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8689 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8690 | spyRight->setFrame({100, 0, 200, 200}); |
| 8691 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 8692 | |
| 8693 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8694 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8695 | {50, 50})) |
| 8696 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8697 | window->consumeMotionDown(); |
| 8698 | spyRight->assertNoEvents(); |
| 8699 | |
| 8700 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8701 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8702 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8703 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8704 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8705 | .build(); |
| 8706 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8707 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8708 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8709 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8710 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8711 | spyRight->consumeMotionDown(); |
| 8712 | } |
| 8713 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8714 | /** |
| 8715 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 8716 | * windows should be allowed to control split touch. |
| 8717 | */ |
| 8718 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8719 | // 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] | 8720 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8721 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8722 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8723 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8724 | auto window = createForeground(); |
| 8725 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8726 | |
| 8727 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8728 | |
| 8729 | // First finger down, no window touched. |
| 8730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8731 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8732 | {100, 200})) |
| 8733 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8734 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8735 | window->assertNoEvents(); |
| 8736 | |
| 8737 | // Second finger down on window, the window should receive touch down. |
| 8738 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8739 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8740 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8741 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8742 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8743 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8744 | .build(); |
| 8745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8746 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8747 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8748 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8749 | |
| 8750 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8751 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8752 | } |
| 8753 | |
| 8754 | /** |
| 8755 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 8756 | * do not receive key events. |
| 8757 | */ |
| 8758 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8759 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8760 | spy->setFocusable(false); |
| 8761 | |
| 8762 | auto window = createForeground(); |
| 8763 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8764 | setFocusedWindow(window); |
| 8765 | window->consumeFocusEvent(true); |
| 8766 | |
| 8767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 8768 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8769 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8770 | |
| 8771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 8772 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8773 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8774 | |
| 8775 | spy->assertNoEvents(); |
| 8776 | } |
| 8777 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8778 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 8779 | |
| 8780 | /** |
| 8781 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 8782 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 8783 | */ |
| 8784 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 8785 | auto window = createForeground(); |
| 8786 | auto spy1 = createSpy(); |
| 8787 | auto spy2 = createSpy(); |
| 8788 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 8789 | |
| 8790 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8791 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8792 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8793 | window->consumeMotionDown(); |
| 8794 | spy1->consumeMotionDown(); |
| 8795 | spy2->consumeMotionDown(); |
| 8796 | |
| 8797 | // Pilfer pointers from the second spy window. |
| 8798 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 8799 | spy2->assertNoEvents(); |
| 8800 | spy1->consumeMotionCancel(); |
| 8801 | window->consumeMotionCancel(); |
| 8802 | |
| 8803 | // The rest of the gesture should only be sent to the second spy window. |
| 8804 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8805 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8806 | ADISPLAY_ID_DEFAULT)) |
| 8807 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8808 | spy2->consumeMotionMove(); |
| 8809 | spy1->assertNoEvents(); |
| 8810 | window->assertNoEvents(); |
| 8811 | } |
| 8812 | |
| 8813 | /** |
| 8814 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 8815 | * in the middle of the gesture. |
| 8816 | */ |
| 8817 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 8818 | auto window = createForeground(); |
| 8819 | auto spy = createSpy(); |
| 8820 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8821 | |
| 8822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8823 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8824 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8825 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8826 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8827 | |
| 8828 | window->releaseChannel(); |
| 8829 | |
| 8830 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8831 | |
| 8832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8833 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8834 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8835 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8836 | } |
| 8837 | |
| 8838 | /** |
| 8839 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 8840 | * the spy, but not to any other windows. |
| 8841 | */ |
| 8842 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 8843 | auto spy = createSpy(); |
| 8844 | auto window = createForeground(); |
| 8845 | |
| 8846 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8847 | |
| 8848 | // First finger down on the window and the spy. |
| 8849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8850 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8851 | {100, 200})) |
| 8852 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8853 | spy->consumeMotionDown(); |
| 8854 | window->consumeMotionDown(); |
| 8855 | |
| 8856 | // Spy window pilfers the pointers. |
| 8857 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8858 | window->consumeMotionCancel(); |
| 8859 | |
| 8860 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 8861 | const MotionEvent secondFingerDownEvent = |
| 8862 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8863 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8864 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8865 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8866 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8867 | .build(); |
| 8868 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8869 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8870 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8871 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8872 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8873 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8874 | |
| 8875 | // Third finger goes down outside all windows, so injection should fail. |
| 8876 | const MotionEvent thirdFingerDownEvent = |
| 8877 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8878 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8879 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8880 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8881 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 8882 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8883 | .build(); |
| 8884 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 8885 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8886 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8887 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8888 | |
| 8889 | spy->assertNoEvents(); |
| 8890 | window->assertNoEvents(); |
| 8891 | } |
| 8892 | |
| 8893 | /** |
| 8894 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 8895 | */ |
| 8896 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 8897 | auto spy = createSpy(); |
| 8898 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8899 | auto window = createForeground(); |
| 8900 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8901 | |
| 8902 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8903 | |
| 8904 | // First finger down on the window only |
| 8905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8906 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8907 | {150, 150})) |
| 8908 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8909 | window->consumeMotionDown(); |
| 8910 | |
| 8911 | // Second finger down on the spy and window |
| 8912 | const MotionEvent secondFingerDownEvent = |
| 8913 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8914 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8915 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8916 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 8917 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8918 | .build(); |
| 8919 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8920 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8921 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8922 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8923 | spy->consumeMotionDown(); |
| 8924 | window->consumeMotionPointerDown(1); |
| 8925 | |
| 8926 | // Third finger down on the spy and window |
| 8927 | const MotionEvent thirdFingerDownEvent = |
| 8928 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8929 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8930 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8931 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 8932 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 8933 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8934 | .build(); |
| 8935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8936 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8937 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8939 | spy->consumeMotionPointerDown(1); |
| 8940 | window->consumeMotionPointerDown(2); |
| 8941 | |
| 8942 | // Spy window pilfers the pointers. |
| 8943 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8944 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8945 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 8946 | |
| 8947 | spy->assertNoEvents(); |
| 8948 | window->assertNoEvents(); |
| 8949 | } |
| 8950 | |
| 8951 | /** |
| 8952 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 8953 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 8954 | * window, then that window should receive ACTION_CANCEL. |
| 8955 | */ |
| 8956 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 8957 | auto spy = createSpy(); |
| 8958 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 8959 | auto window = createForeground(); |
| 8960 | window->setFrame(Rect(0, 0, 200, 200)); |
| 8961 | |
| 8962 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8963 | |
| 8964 | // First finger down on both spy and window |
| 8965 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8966 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8967 | {10, 10})) |
| 8968 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8969 | window->consumeMotionDown(); |
| 8970 | spy->consumeMotionDown(); |
| 8971 | |
| 8972 | // Second finger down on the spy and window |
| 8973 | const MotionEvent secondFingerDownEvent = |
| 8974 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8975 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8976 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8977 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 8978 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8979 | .build(); |
| 8980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8981 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8982 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8983 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8984 | spy->consumeMotionPointerDown(1); |
| 8985 | window->consumeMotionPointerDown(1); |
| 8986 | |
| 8987 | // Spy window pilfers the pointers. |
| 8988 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8989 | window->consumeMotionCancel(); |
| 8990 | |
| 8991 | spy->assertNoEvents(); |
| 8992 | window->assertNoEvents(); |
| 8993 | } |
| 8994 | |
| 8995 | /** |
| 8996 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 8997 | * be sent to other windows |
| 8998 | */ |
| 8999 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 9000 | auto spy = createSpy(); |
| 9001 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9002 | auto window = createForeground(); |
| 9003 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9004 | |
| 9005 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9006 | |
| 9007 | // First finger down on both window and spy |
| 9008 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9009 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9010 | {10, 10})) |
| 9011 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9012 | window->consumeMotionDown(); |
| 9013 | spy->consumeMotionDown(); |
| 9014 | |
| 9015 | // Spy window pilfers the pointers. |
| 9016 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9017 | window->consumeMotionCancel(); |
| 9018 | |
| 9019 | // Second finger down on the window only |
| 9020 | const MotionEvent secondFingerDownEvent = |
| 9021 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9022 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9023 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9024 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9025 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9026 | .build(); |
| 9027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9028 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9029 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9030 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9031 | window->consumeMotionDown(); |
| 9032 | window->assertNoEvents(); |
| 9033 | |
| 9034 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 9035 | spy->consumeMotionMove(); |
| 9036 | spy->assertNoEvents(); |
| 9037 | } |
| 9038 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9039 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 9040 | public: |
| 9041 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 9042 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9043 | std::make_shared<FakeApplicationHandle>(); |
| 9044 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9045 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 9046 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9047 | overlay->setFocusable(false); |
| 9048 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9049 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9050 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9051 | overlay->setTrustedOverlay(true); |
| 9052 | |
| 9053 | std::shared_ptr<FakeApplicationHandle> application = |
| 9054 | std::make_shared<FakeApplicationHandle>(); |
| 9055 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9056 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9057 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9058 | window->setFocusable(true); |
| 9059 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9060 | |
| 9061 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9062 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9063 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9064 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9065 | return {std::move(overlay), std::move(window)}; |
| 9066 | } |
| 9067 | |
| 9068 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9069 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9070 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9071 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9072 | } |
| 9073 | |
| 9074 | void sendStylusEvent(int32_t action) { |
| 9075 | NotifyMotionArgs motionArgs = |
| 9076 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9077 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9078 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9079 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9080 | } |
| 9081 | }; |
| 9082 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9083 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9084 | |
| 9085 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 9086 | ScopedSilentDeath _silentDeath; |
| 9087 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9088 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9089 | overlay->setTrustedOverlay(false); |
| 9090 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9091 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9092 | ".* not a trusted overlay"); |
| 9093 | } |
| 9094 | |
| 9095 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9096 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9097 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9098 | |
| 9099 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9100 | overlay->consumeMotionDown(); |
| 9101 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9102 | overlay->consumeMotionUp(); |
| 9103 | |
| 9104 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9105 | window->consumeMotionDown(); |
| 9106 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9107 | window->consumeMotionUp(); |
| 9108 | |
| 9109 | overlay->assertNoEvents(); |
| 9110 | window->assertNoEvents(); |
| 9111 | } |
| 9112 | |
| 9113 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9114 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9115 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9116 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9117 | |
| 9118 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9119 | overlay->consumeMotionDown(); |
| 9120 | window->consumeMotionDown(); |
| 9121 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9122 | overlay->consumeMotionUp(); |
| 9123 | window->consumeMotionUp(); |
| 9124 | |
| 9125 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9126 | window->consumeMotionDown(); |
| 9127 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9128 | window->consumeMotionUp(); |
| 9129 | |
| 9130 | overlay->assertNoEvents(); |
| 9131 | window->assertNoEvents(); |
| 9132 | } |
| 9133 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9134 | /** |
| 9135 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9136 | * The scenario is as follows: |
| 9137 | * - The stylus interceptor overlay is configured as a spy window. |
| 9138 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9139 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9140 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9141 | */ |
| 9142 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9143 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9144 | overlay->setSpy(true); |
| 9145 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9146 | |
| 9147 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9148 | overlay->consumeMotionDown(); |
| 9149 | window->consumeMotionDown(); |
| 9150 | |
| 9151 | // The interceptor pilfers the pointers. |
| 9152 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9153 | window->consumeMotionCancel(); |
| 9154 | |
| 9155 | // The interceptor configures itself so that it is no longer a spy. |
| 9156 | overlay->setSpy(false); |
| 9157 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9158 | |
| 9159 | // It continues to receive the rest of the stylus gesture. |
| 9160 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9161 | overlay->consumeMotionMove(); |
| 9162 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9163 | overlay->consumeMotionUp(); |
| 9164 | |
| 9165 | window->assertNoEvents(); |
| 9166 | } |
| 9167 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9168 | struct User { |
| 9169 | int32_t mPid; |
| 9170 | int32_t mUid; |
| 9171 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9172 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9173 | |
| 9174 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 9175 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9176 | |
| 9177 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9178 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9179 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9180 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9181 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9182 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9183 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9184 | } |
| 9185 | |
| 9186 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9187 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9188 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9189 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9190 | mPolicyFlags); |
| 9191 | } |
| 9192 | |
| 9193 | sp<FakeWindowHandle> createWindow() const { |
| 9194 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9195 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9196 | sp<FakeWindowHandle> window = |
| 9197 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9198 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9199 | window->setOwnerInfo(mPid, mUid); |
| 9200 | return window; |
| 9201 | } |
| 9202 | }; |
| 9203 | |
| 9204 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9205 | |
| 9206 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 9207 | auto owner = User(mDispatcher, 10, 11); |
| 9208 | auto window = owner.createWindow(); |
| 9209 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9210 | |
| 9211 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9212 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9213 | window->consumeMotionDown(); |
| 9214 | |
| 9215 | setFocusedWindow(window); |
| 9216 | window->consumeFocusEvent(true); |
| 9217 | |
| 9218 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9219 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9220 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9221 | } |
| 9222 | |
| 9223 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 9224 | auto owner = User(mDispatcher, 10, 11); |
| 9225 | auto window = owner.createWindow(); |
| 9226 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9227 | |
| 9228 | auto rando = User(mDispatcher, 20, 21); |
| 9229 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9230 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9231 | |
| 9232 | setFocusedWindow(window); |
| 9233 | window->consumeFocusEvent(true); |
| 9234 | |
| 9235 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9236 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9237 | window->assertNoEvents(); |
| 9238 | } |
| 9239 | |
| 9240 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 9241 | auto owner = User(mDispatcher, 10, 11); |
| 9242 | auto window = owner.createWindow(); |
| 9243 | auto spy = owner.createWindow(); |
| 9244 | spy->setSpy(true); |
| 9245 | spy->setTrustedOverlay(true); |
| 9246 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9247 | |
| 9248 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9249 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9250 | spy->consumeMotionDown(); |
| 9251 | window->consumeMotionDown(); |
| 9252 | } |
| 9253 | |
| 9254 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 9255 | auto owner = User(mDispatcher, 10, 11); |
| 9256 | auto window = owner.createWindow(); |
| 9257 | |
| 9258 | auto rando = User(mDispatcher, 20, 21); |
| 9259 | auto randosSpy = rando.createWindow(); |
| 9260 | randosSpy->setSpy(true); |
| 9261 | randosSpy->setTrustedOverlay(true); |
| 9262 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9263 | |
| 9264 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9265 | // not receive the event. |
| 9266 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9267 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9268 | randosSpy->assertNoEvents(); |
| 9269 | window->consumeMotionDown(); |
| 9270 | } |
| 9271 | |
| 9272 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 9273 | auto owner = User(mDispatcher, 10, 11); |
| 9274 | auto window = owner.createWindow(); |
| 9275 | |
| 9276 | auto rando = User(mDispatcher, 20, 21); |
| 9277 | auto randosSpy = rando.createWindow(); |
| 9278 | randosSpy->setSpy(true); |
| 9279 | randosSpy->setTrustedOverlay(true); |
| 9280 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9281 | |
| 9282 | // A user that has injection permission can inject into any window. |
| 9283 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9284 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9285 | ADISPLAY_ID_DEFAULT)); |
| 9286 | randosSpy->consumeMotionDown(); |
| 9287 | window->consumeMotionDown(); |
| 9288 | |
| 9289 | setFocusedWindow(randosSpy); |
| 9290 | randosSpy->consumeFocusEvent(true); |
| 9291 | |
| 9292 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9293 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9294 | window->assertNoEvents(); |
| 9295 | } |
| 9296 | |
| 9297 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 9298 | auto owner = User(mDispatcher, 10, 11); |
| 9299 | auto window = owner.createWindow(); |
| 9300 | |
| 9301 | auto rando = User(mDispatcher, 20, 21); |
| 9302 | auto randosWindow = rando.createWindow(); |
| 9303 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9304 | randosWindow->setWatchOutsideTouch(true); |
| 9305 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9306 | |
| 9307 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 9308 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9309 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9310 | window->consumeMotionDown(); |
| 9311 | randosWindow->consumeMotionOutside(); |
| 9312 | } |
| 9313 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9314 | } // namespace android::inputdispatcher |