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; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 65 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 66 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 67 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 68 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 69 | /** |
| 70 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 71 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 72 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 73 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 74 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 75 | * pointer id=0 leaves but the pointer id=1 remains. |
| 76 | */ |
| 77 | static constexpr int32_t POINTER_0_DOWN = |
| 78 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 79 | static constexpr int32_t POINTER_1_DOWN = |
| 80 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 81 | static constexpr int32_t POINTER_2_DOWN = |
| 82 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 83 | static constexpr int32_t POINTER_3_DOWN = |
| 84 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 85 | static constexpr int32_t POINTER_0_UP = |
| 86 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 87 | static constexpr int32_t POINTER_1_UP = |
| 88 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 89 | static constexpr int32_t POINTER_2_UP = |
| 90 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 91 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 92 | // 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] | 93 | static constexpr int32_t WINDOW_PID = 999; |
| 94 | static constexpr int32_t WINDOW_UID = 1001; |
| 95 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 96 | // The default pid and uid for the windows created on the secondary display by the test. |
| 97 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; |
| 98 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; |
| 99 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 100 | // The default policy flags to use for event injection by tests. |
| 101 | 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] | 102 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 103 | // An arbitrary pid of the gesture monitor window |
| 104 | static constexpr int32_t MONITOR_PID = 2001; |
| 105 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 106 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 107 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 108 | static constexpr int expectedWallpaperFlags = |
| 109 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 110 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 111 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 112 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 113 | struct PointF { |
| 114 | float x; |
| 115 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 116 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 117 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 118 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 119 | /** |
| 120 | * Return a DOWN key event with KEYCODE_A. |
| 121 | */ |
| 122 | static KeyEvent getTestKeyEvent() { |
| 123 | KeyEvent event; |
| 124 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 125 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 126 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 127 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 128 | return event; |
| 129 | } |
| 130 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 131 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 132 | ASSERT_EQ(expectedAction, receivedAction) |
| 133 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 134 | << MotionEvent::actionToString(receivedAction); |
| 135 | } |
| 136 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 137 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 138 | bool matches = action == arg.getAction(); |
| 139 | if (!matches) { |
| 140 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 141 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 142 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 143 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 144 | if (!matches) { |
| 145 | *result_listener << "; "; |
| 146 | } |
| 147 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 148 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 149 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 150 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 151 | if (!matches) { |
| 152 | *result_listener << "; "; |
| 153 | } |
| 154 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 155 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 156 | } |
| 157 | return matches; |
| 158 | } |
| 159 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 160 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 161 | return arg.getDownTime() == downTime; |
| 162 | } |
| 163 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 164 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { |
| 165 | return arg.getDisplayId() == displayId; |
| 166 | } |
| 167 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 168 | MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") { |
| 169 | return arg.getDeviceId() == deviceId; |
| 170 | } |
| 171 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 172 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 173 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 174 | << inputEventSourceToString(arg.getSource()); |
| 175 | return arg.getSource() == source; |
| 176 | } |
| 177 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 178 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { |
| 179 | return arg.getFlags() == flags; |
| 180 | } |
| 181 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 182 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 183 | if (arg.getPointerCount() != 1) { |
| 184 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 185 | return false; |
| 186 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 187 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 190 | MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") { |
| 191 | return arg.getPointerCount() == pointerCount; |
| 192 | } |
| 193 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 194 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 195 | // Build a map for the received pointers, by pointer id |
| 196 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 197 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 198 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 199 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 200 | } |
| 201 | return pointers == actualPointers; |
| 202 | } |
| 203 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 204 | // --- FakeInputDispatcherPolicy --- |
| 205 | |
| 206 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 207 | InputDispatcherConfiguration mConfig; |
| 208 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 209 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 210 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 212 | FakeInputDispatcherPolicy() = default; |
| 213 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 214 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 215 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 216 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 217 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 218 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 219 | |
| 220 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 221 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 222 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 223 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 224 | } |
| 225 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 226 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 227 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 228 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 229 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 230 | |
| 231 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 232 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 233 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 234 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 235 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 236 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 237 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 238 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 239 | } |
| 240 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 241 | void assertFilterInputEventWasNotCalled() { |
| 242 | std::scoped_lock lock(mLock); |
| 243 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 244 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 245 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 246 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 247 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 248 | ASSERT_TRUE(mConfigurationChangedTime) |
| 249 | << "Timed out waiting for configuration changed call"; |
| 250 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 251 | mConfigurationChangedTime = std::nullopt; |
| 252 | } |
| 253 | |
| 254 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 255 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 256 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 257 | // 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] | 258 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 259 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 260 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 261 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 262 | mLastNotifySwitch = std::nullopt; |
| 263 | } |
| 264 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 265 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 266 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 267 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 268 | mOnPointerDownToken.clear(); |
| 269 | } |
| 270 | |
| 271 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 272 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 273 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 274 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 277 | // This function must be called soon after the expected ANR timer starts, |
| 278 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 279 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 280 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 281 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 282 | std::unique_lock lock(mLock); |
| 283 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 284 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 285 | ASSERT_NO_FATAL_FAILURE( |
| 286 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 287 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 290 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 291 | const sp<WindowInfoHandle>& window) { |
| 292 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 293 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 294 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 297 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 298 | const sp<IBinder>& expectedToken, |
| 299 | int32_t expectedPid) { |
| 300 | std::unique_lock lock(mLock); |
| 301 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 302 | AnrResult result; |
| 303 | ASSERT_NO_FATAL_FAILURE(result = |
| 304 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 305 | const auto& [token, pid] = result; |
| 306 | ASSERT_EQ(expectedToken, token); |
| 307 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 310 | /** 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] | 311 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 312 | std::unique_lock lock(mLock); |
| 313 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 314 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 315 | const auto& [token, _] = result; |
| 316 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 319 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 320 | int32_t expectedPid) { |
| 321 | std::unique_lock lock(mLock); |
| 322 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 323 | AnrResult result; |
| 324 | ASSERT_NO_FATAL_FAILURE( |
| 325 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 326 | const auto& [token, pid] = result; |
| 327 | ASSERT_EQ(expectedToken, token); |
| 328 | ASSERT_EQ(expectedPid, pid); |
| 329 | } |
| 330 | |
| 331 | /** 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] | 332 | sp<IBinder> getResponsiveWindowToken() { |
| 333 | std::unique_lock lock(mLock); |
| 334 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 335 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 336 | const auto& [token, _] = result; |
| 337 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void assertNotifyAnrWasNotCalled() { |
| 341 | std::scoped_lock lock(mLock); |
| 342 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 343 | ASSERT_TRUE(mAnrWindows.empty()); |
| 344 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 345 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 346 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 349 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 350 | mConfig.keyRepeatTimeout = timeout; |
| 351 | mConfig.keyRepeatDelay = delay; |
| 352 | } |
| 353 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 354 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 355 | std::unique_lock lock(mLock); |
| 356 | base::ScopedLockAssertion assumeLocked(mLock); |
| 357 | |
| 358 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 359 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 360 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 361 | enabled; |
| 362 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 363 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 364 | << ") to be called."; |
| 365 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 366 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 367 | auto request = *mPointerCaptureRequest; |
| 368 | mPointerCaptureRequest.reset(); |
| 369 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void assertSetPointerCaptureNotCalled() { |
| 373 | std::unique_lock lock(mLock); |
| 374 | base::ScopedLockAssertion assumeLocked(mLock); |
| 375 | |
| 376 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 377 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 378 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 379 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 380 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 381 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 382 | } |
| 383 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 384 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 385 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 386 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 387 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 388 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 389 | } |
| 390 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 391 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 392 | std::unique_lock lock(mLock); |
| 393 | base::ScopedLockAssertion assumeLocked(mLock); |
| 394 | std::optional<sp<IBinder>> receivedToken = |
| 395 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 396 | mNotifyInputChannelBroken); |
| 397 | ASSERT_TRUE(receivedToken.has_value()); |
| 398 | ASSERT_EQ(token, *receivedToken); |
| 399 | } |
| 400 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 401 | /** |
| 402 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 403 | */ |
| 404 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 405 | mInterceptKeyTimeout = timeout; |
| 406 | } |
| 407 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 408 | void assertUserActivityPoked() { |
| 409 | std::scoped_lock lock(mLock); |
| 410 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; |
| 411 | } |
| 412 | |
| 413 | void assertUserActivityNotPoked() { |
| 414 | std::scoped_lock lock(mLock); |
| 415 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; |
| 416 | } |
| 417 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 418 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 419 | std::mutex mLock; |
| 420 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 421 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 422 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 423 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 424 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 425 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 426 | |
| 427 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 428 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 429 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 430 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 431 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 432 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 433 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 434 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 435 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 436 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 437 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 438 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 439 | bool mPokedUserActivity GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 440 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 441 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 442 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 443 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 444 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 445 | // first entry from the container and erase it. |
| 446 | template <class T> |
| 447 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 448 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 449 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 450 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 451 | // before checking if ANR was called. |
| 452 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 453 | // to provide it some time to act. 100ms seems reasonable. |
| 454 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 455 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 456 | std::optional<T> token = |
| 457 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 458 | if (!token.has_value()) { |
| 459 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 460 | return {}; |
| 461 | } |
| 462 | |
| 463 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 464 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 465 | // the dispatcher started counting before this function was called |
| 466 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 467 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 468 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 469 | << "ms, but waited " |
| 470 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 471 | << "ms instead"; |
| 472 | } |
| 473 | return *token; |
| 474 | } |
| 475 | |
| 476 | template <class T> |
| 477 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 478 | std::queue<T>& storage, |
| 479 | std::unique_lock<std::mutex>& lock, |
| 480 | std::condition_variable& condition) |
| 481 | REQUIRES(mLock) { |
| 482 | condition.wait_for(lock, timeout, |
| 483 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 484 | if (storage.empty()) { |
| 485 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 486 | return std::nullopt; |
| 487 | } |
| 488 | T item = storage.front(); |
| 489 | storage.pop(); |
| 490 | return std::make_optional(item); |
| 491 | } |
| 492 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 493 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 494 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 495 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 498 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 499 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 500 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 501 | ASSERT_TRUE(pid.has_value()); |
| 502 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 503 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 504 | } |
| 505 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 506 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 507 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 508 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 509 | ASSERT_TRUE(pid.has_value()); |
| 510 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 511 | mNotifyAnr.notify_all(); |
| 512 | } |
| 513 | |
| 514 | void notifyNoFocusedWindowAnr( |
| 515 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 516 | std::scoped_lock lock(mLock); |
| 517 | mAnrApplications.push(applicationHandle); |
| 518 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 521 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 522 | std::scoped_lock lock(mLock); |
| 523 | mBrokenInputChannels.push(connectionToken); |
| 524 | mNotifyInputChannelBroken.notify_all(); |
| 525 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 526 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 527 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 528 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 529 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 530 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 531 | const std::vector<float>& values) override {} |
| 532 | |
| 533 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 534 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 535 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 536 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 537 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 538 | InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 539 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 540 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 541 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 542 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 543 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 544 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 545 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 549 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 550 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 551 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 552 | break; |
| 553 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 554 | default: { |
| 555 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 556 | break; |
| 557 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 558 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 559 | return true; |
| 560 | } |
| 561 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 562 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 563 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 564 | // Clear intercept state when we handled the event. |
| 565 | mInterceptKeyTimeout = 0ms; |
| 566 | } |
| 567 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 569 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 570 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 571 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 572 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 573 | // Clear intercept state so we could dispatch the event in next wake. |
| 574 | mInterceptKeyTimeout = 0ms; |
| 575 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 578 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&, |
| 579 | uint32_t) override { |
| 580 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 583 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 584 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 585 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 586 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 587 | * essentially a passthrough for notifySwitch. |
| 588 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 589 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 592 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { |
| 593 | std::scoped_lock lock(mLock); |
| 594 | mPokedUserActivity = true; |
| 595 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 597 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 598 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 599 | mOnPointerDownToken = newToken; |
| 600 | } |
| 601 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 602 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 603 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 604 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 605 | mPointerCaptureChangedCondition.notify_all(); |
| 606 | } |
| 607 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 608 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 609 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 610 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 611 | mDropTargetWindowToken = token; |
| 612 | } |
| 613 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 614 | void assertFilterInputEventWasCalledInternal( |
| 615 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 616 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 617 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 618 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 619 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 620 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 621 | }; |
| 622 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | // --- InputDispatcherTest --- |
| 624 | |
| 625 | class InputDispatcherTest : public testing::Test { |
| 626 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 627 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 628 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 629 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 630 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 631 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
| 632 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 633 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 634 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 635 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 638 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 639 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 640 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 641 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 642 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 643 | |
| 644 | /** |
| 645 | * Used for debugging when writing the test |
| 646 | */ |
| 647 | void dumpDispatcherState() { |
| 648 | std::string dump; |
| 649 | mDispatcher->dump(dump); |
| 650 | std::stringstream ss(dump); |
| 651 | std::string to; |
| 652 | |
| 653 | while (std::getline(ss, to, '\n')) { |
| 654 | ALOGE("%s", to.c_str()); |
| 655 | } |
| 656 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 657 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 658 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 659 | FocusRequest request; |
| 660 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 661 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 662 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 663 | request.displayId = window->getInfo()->displayId; |
| 664 | mDispatcher->setFocusedWindow(request); |
| 665 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 666 | }; |
| 667 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 668 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 669 | KeyEvent event; |
| 670 | |
| 671 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 672 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 673 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 674 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 675 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 676 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 677 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 678 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | << "Should reject key events with undefined action."; |
| 680 | |
| 681 | // 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] | 682 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 683 | 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] | 684 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 685 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 686 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 687 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 688 | << "Should reject key events with ACTION_MULTIPLE."; |
| 689 | } |
| 690 | |
| 691 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 692 | MotionEvent event; |
| 693 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 694 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 695 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 696 | pointerProperties[i].clear(); |
| 697 | pointerProperties[i].id = i; |
| 698 | pointerCoords[i].clear(); |
| 699 | } |
| 700 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 701 | // Some constants commonly used below |
| 702 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 703 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 704 | constexpr int32_t metaState = AMETA_NONE; |
| 705 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 706 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 707 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 708 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 709 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 710 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 711 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 712 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 713 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 714 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 715 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 716 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 717 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 | << "Should reject motion events with undefined action."; |
| 719 | |
| 720 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 721 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 722 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 723 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 724 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 725 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 726 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 727 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 728 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 729 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 730 | << "Should reject motion events with pointer down index too large."; |
| 731 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 732 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 733 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 734 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 735 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 736 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 737 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 738 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 739 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 740 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 741 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 742 | << "Should reject motion events with pointer down index too small."; |
| 743 | |
| 744 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 745 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 746 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 747 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 748 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 749 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 750 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 751 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 752 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 753 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 754 | << "Should reject motion events with pointer up index too large."; |
| 755 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 756 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 757 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 758 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 759 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 760 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 761 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 762 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 763 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 764 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 765 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 766 | << "Should reject motion events with pointer up index too small."; |
| 767 | |
| 768 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 769 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 770 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 771 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 772 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 773 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 774 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 775 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 776 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 777 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 778 | << "Should reject motion events with 0 pointers."; |
| 779 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 780 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 781 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 782 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 783 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 784 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 785 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 786 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 787 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 788 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 790 | |
| 791 | // Rejects motion events with invalid pointer ids. |
| 792 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 793 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 794 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 795 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 796 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 797 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 798 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 799 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 800 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 801 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 802 | << "Should reject motion events with pointer ids less than 0."; |
| 803 | |
| 804 | pointerProperties[0].id = MAX_POINTER_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*/ 1, 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 pointer ids greater than MAX_POINTER_ID."; |
| 815 | |
| 816 | // Rejects motion events with duplicate pointer ids. |
| 817 | pointerProperties[0].id = 1; |
| 818 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 819 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 820 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 821 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 822 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 823 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 824 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 825 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 826 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 827 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 828 | << "Should reject motion events with duplicate pointer ids."; |
| 829 | } |
| 830 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 831 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 832 | |
| 833 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 834 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 835 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 836 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 837 | |
| 838 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 839 | } |
| 840 | |
| 841 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 842 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 843 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 844 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 845 | |
| 846 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 847 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 848 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 849 | } |
| 850 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 851 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 852 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 853 | // Default input dispatching timeout if there is no focused application or paused window |
| 854 | // from which to determine an appropriate dispatching timeout. |
| 855 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 856 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 857 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 858 | |
| 859 | class FakeApplicationHandle : public InputApplicationHandle { |
| 860 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 861 | FakeApplicationHandle() { |
| 862 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 863 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 864 | mInfo.dispatchingTimeoutMillis = |
| 865 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 866 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 867 | virtual ~FakeApplicationHandle() {} |
| 868 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 869 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 870 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 871 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 872 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 873 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 874 | }; |
| 875 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 876 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 877 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 878 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 879 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 880 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 881 | } |
| 882 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 883 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 884 | InputEvent* event; |
| 885 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 886 | if (!consumeSeq) { |
| 887 | return nullptr; |
| 888 | } |
| 889 | finishEvent(*consumeSeq); |
| 890 | return event; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * Receive an event without acknowledging it. |
| 895 | * Return the sequence number that could later be used to send finished signal. |
| 896 | */ |
| 897 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 898 | uint32_t consumeSeq; |
| 899 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 900 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 901 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 902 | status_t status = WOULD_BLOCK; |
| 903 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 904 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 905 | &event); |
| 906 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 907 | if (elapsed > 100ms) { |
| 908 | break; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (status == WOULD_BLOCK) { |
| 913 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 914 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | if (status != OK) { |
| 918 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 919 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 920 | } |
| 921 | if (event == nullptr) { |
| 922 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 923 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 924 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 925 | if (outEvent != nullptr) { |
| 926 | *outEvent = event; |
| 927 | } |
| 928 | return consumeSeq; |
| 929 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 930 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 931 | /** |
| 932 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 933 | */ |
| 934 | void finishEvent(uint32_t consumeSeq) { |
| 935 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 936 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 939 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 940 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 941 | ASSERT_EQ(OK, status); |
| 942 | } |
| 943 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 944 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 945 | std::optional<int32_t> expectedDisplayId, |
| 946 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 947 | InputEvent* event = consume(); |
| 948 | |
| 949 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 950 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 951 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 952 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 953 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 954 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 955 | if (expectedDisplayId.has_value()) { |
| 956 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 957 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 958 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 959 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 960 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 961 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 962 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 963 | if (expectedFlags.has_value()) { |
| 964 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 965 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 966 | break; |
| 967 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 968 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 969 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 970 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 971 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 972 | if (expectedFlags.has_value()) { |
| 973 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 974 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 975 | break; |
| 976 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 977 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 978 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 979 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 980 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 981 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 982 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 983 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 984 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 985 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 986 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 987 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 988 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 989 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 990 | } |
| 991 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 992 | MotionEvent* consumeMotion() { |
| 993 | InputEvent* event = consume(); |
| 994 | |
| 995 | if (event == nullptr) { |
| 996 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 997 | return nullptr; |
| 998 | } |
| 999 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1000 | if (event->getType() != InputEventType::MOTION) { |
| 1001 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1002 | return nullptr; |
| 1003 | } |
| 1004 | return static_cast<MotionEvent*>(event); |
| 1005 | } |
| 1006 | |
| 1007 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1008 | MotionEvent* motionEvent = consumeMotion(); |
| 1009 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1010 | ASSERT_THAT(*motionEvent, matcher); |
| 1011 | } |
| 1012 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1013 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 1014 | 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::FOCUS, event->getType()) |
| 1018 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1019 | |
| 1020 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1021 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1022 | |
| 1023 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1024 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1027 | void consumeCaptureEvent(bool hasCapture) { |
| 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::CAPTURE, event->getType()) |
| 1032 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1033 | |
| 1034 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1035 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1036 | |
| 1037 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1038 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1039 | } |
| 1040 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1041 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1042 | const InputEvent* event = consume(); |
| 1043 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1044 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1045 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1046 | |
| 1047 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1048 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1049 | |
| 1050 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1051 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1052 | EXPECT_EQ(x, dragEvent.getX()); |
| 1053 | EXPECT_EQ(y, dragEvent.getY()); |
| 1054 | } |
| 1055 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1056 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1057 | const InputEvent* event = consume(); |
| 1058 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1059 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1060 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1061 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1062 | |
| 1063 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1064 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1065 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1066 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1067 | } |
| 1068 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1069 | void assertNoEvents() { |
| 1070 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1071 | if (event == nullptr) { |
| 1072 | return; |
| 1073 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1074 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1075 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1076 | ADD_FAILURE() << "Received key event " |
| 1077 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1078 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1079 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1080 | ADD_FAILURE() << "Received motion event " |
| 1081 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1082 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1083 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1084 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1085 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1086 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1087 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1088 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1089 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1090 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1091 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1092 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1093 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1094 | } |
| 1095 | FAIL() << mName.c_str() |
| 1096 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1100 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1101 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1102 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1103 | protected: |
| 1104 | std::unique_ptr<InputConsumer> mConsumer; |
| 1105 | PreallocatedInputEventFactory mEventFactory; |
| 1106 | |
| 1107 | std::string mName; |
| 1108 | }; |
| 1109 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1110 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1111 | public: |
| 1112 | static const int32_t WIDTH = 600; |
| 1113 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1114 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1115 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1116 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1117 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1118 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1119 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1120 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1121 | dispatcher->createInputChannel(name); |
| 1122 | token = (*channel)->getConnectionToken(); |
| 1123 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | inputApplicationHandle->updateInfo(); |
| 1127 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1128 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1129 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1130 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1131 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1132 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1133 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1134 | mInfo.frameLeft = 0; |
| 1135 | mInfo.frameTop = 0; |
| 1136 | mInfo.frameRight = WIDTH; |
| 1137 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1138 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1139 | mInfo.globalScaleFactor = 1.0; |
| 1140 | mInfo.touchableRegion.clear(); |
| 1141 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1142 | mInfo.ownerPid = WINDOW_PID; |
| 1143 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1144 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1145 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1148 | sp<FakeWindowHandle> clone( |
| 1149 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1150 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1151 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1152 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1153 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1154 | return handle; |
| 1155 | } |
| 1156 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1157 | void setTouchable(bool touchable) { |
| 1158 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1159 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1160 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1161 | void setFocusable(bool focusable) { |
| 1162 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1163 | } |
| 1164 | |
| 1165 | void setVisible(bool visible) { |
| 1166 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1167 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1168 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1169 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1170 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1173 | void setPaused(bool paused) { |
| 1174 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1175 | } |
| 1176 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1177 | void setPreventSplitting(bool preventSplitting) { |
| 1178 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | void setSlippery(bool slippery) { |
| 1182 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1183 | } |
| 1184 | |
| 1185 | void setWatchOutsideTouch(bool watchOutside) { |
| 1186 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1187 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1188 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1189 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1190 | |
| 1191 | void setInterceptsStylus(bool interceptsStylus) { |
| 1192 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1193 | } |
| 1194 | |
| 1195 | void setDropInput(bool dropInput) { |
| 1196 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1197 | } |
| 1198 | |
| 1199 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1200 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1201 | } |
| 1202 | |
| 1203 | void setNoInputChannel(bool noInputChannel) { |
| 1204 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1205 | } |
| 1206 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1207 | void setDisableUserActivity(bool disableUserActivity) { |
| 1208 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1209 | } |
| 1210 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1211 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1212 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1213 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1214 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1215 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1216 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1217 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1218 | mInfo.frameLeft = frame.left; |
| 1219 | mInfo.frameTop = frame.top; |
| 1220 | mInfo.frameRight = frame.right; |
| 1221 | mInfo.frameBottom = frame.bottom; |
| 1222 | mInfo.touchableRegion.clear(); |
| 1223 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1224 | |
| 1225 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1226 | ui::Transform translate; |
| 1227 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1228 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1231 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1232 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1233 | void setIsWallpaper(bool isWallpaper) { |
| 1234 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1235 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1236 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1237 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1238 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1239 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1240 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1241 | void setTrustedOverlay(bool trustedOverlay) { |
| 1242 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1243 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1244 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1245 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1246 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1247 | } |
| 1248 | |
| 1249 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1250 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1251 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1252 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1253 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1254 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1257 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1258 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1261 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1262 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1263 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1264 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1268 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1269 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1270 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1274 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1275 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1276 | } |
| 1277 | |
| 1278 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1279 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1280 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1281 | expectedFlags); |
| 1282 | } |
| 1283 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1284 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1285 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1286 | int32_t expectedFlags = 0) { |
| 1287 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1288 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1289 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1293 | int32_t expectedFlags = 0) { |
| 1294 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1295 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1296 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1300 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1301 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1302 | expectedFlags); |
| 1303 | } |
| 1304 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1305 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1306 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1307 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1308 | expectedFlags); |
| 1309 | } |
| 1310 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1311 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1312 | int32_t expectedFlags = 0) { |
| 1313 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1314 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1315 | ASSERT_EQ(InputEventType::MOTION, event->getType()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1316 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1317 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1318 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1319 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1320 | } |
| 1321 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1322 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1323 | ASSERT_NE(mInputReceiver, nullptr) |
| 1324 | << "Cannot consume events from a window with no receiver"; |
| 1325 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1326 | } |
| 1327 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1328 | void consumeCaptureEvent(bool hasCapture) { |
| 1329 | ASSERT_NE(mInputReceiver, nullptr) |
| 1330 | << "Cannot consume events from a window with no receiver"; |
| 1331 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1332 | } |
| 1333 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1334 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1335 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1336 | 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] | 1337 | ASSERT_THAT(*motionEvent, matcher); |
| 1338 | } |
| 1339 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1340 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1341 | std::optional<int32_t> expectedDisplayId, |
| 1342 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1343 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1344 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1345 | expectedFlags); |
| 1346 | } |
| 1347 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1348 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1349 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1350 | } |
| 1351 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1352 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1353 | ASSERT_NE(mInputReceiver, nullptr) |
| 1354 | << "Cannot consume events from a window with no receiver"; |
| 1355 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1356 | } |
| 1357 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1358 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1359 | if (mInputReceiver == nullptr) { |
| 1360 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1361 | return std::nullopt; |
| 1362 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1363 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | void finishEvent(uint32_t sequenceNum) { |
| 1367 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1368 | mInputReceiver->finishEvent(sequenceNum); |
| 1369 | } |
| 1370 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1371 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1372 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1373 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1374 | } |
| 1375 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1376 | InputEvent* consume() { |
| 1377 | if (mInputReceiver == nullptr) { |
| 1378 | return nullptr; |
| 1379 | } |
| 1380 | return mInputReceiver->consume(); |
| 1381 | } |
| 1382 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1383 | MotionEvent* consumeMotion() { |
| 1384 | InputEvent* event = consume(); |
| 1385 | if (event == nullptr) { |
| 1386 | ADD_FAILURE() << "Consume failed : no event"; |
| 1387 | return nullptr; |
| 1388 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1389 | if (event->getType() != InputEventType::MOTION) { |
| 1390 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1391 | return nullptr; |
| 1392 | } |
| 1393 | return static_cast<MotionEvent*>(event); |
| 1394 | } |
| 1395 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1396 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1397 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1398 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1399 | return; // Can't receive events if the window does not have input channel |
| 1400 | } |
| 1401 | ASSERT_NE(nullptr, mInputReceiver) |
| 1402 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1403 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1404 | } |
| 1405 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1406 | sp<IBinder> getToken() { return mInfo.token; } |
| 1407 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1408 | const std::string& getName() { return mName; } |
| 1409 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1410 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1411 | mInfo.ownerPid = ownerPid; |
| 1412 | mInfo.ownerUid = ownerUid; |
| 1413 | } |
| 1414 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1415 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1416 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1417 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1418 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1419 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1420 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1421 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1422 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1423 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1424 | 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] | 1425 | }; |
| 1426 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1427 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1428 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1429 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1430 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1431 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1432 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1433 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1434 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1435 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1436 | KeyEvent event; |
| 1437 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1438 | |
| 1439 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1440 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1441 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1442 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1443 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1444 | if (!allowKeyRepeat) { |
| 1445 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1446 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1447 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1448 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1449 | } |
| 1450 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1451 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1452 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1453 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1456 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1457 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1458 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1459 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1460 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1461 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1462 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1463 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1466 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1467 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1468 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1471 | class PointerBuilder { |
| 1472 | public: |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1473 | PointerBuilder(int32_t id, ToolType toolType) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1474 | mProperties.clear(); |
| 1475 | mProperties.id = id; |
| 1476 | mProperties.toolType = toolType; |
| 1477 | mCoords.clear(); |
| 1478 | } |
| 1479 | |
| 1480 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1481 | |
| 1482 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1483 | |
| 1484 | PointerBuilder& axis(int32_t axis, float value) { |
| 1485 | mCoords.setAxisValue(axis, value); |
| 1486 | return *this; |
| 1487 | } |
| 1488 | |
| 1489 | PointerProperties buildProperties() const { return mProperties; } |
| 1490 | |
| 1491 | PointerCoords buildCoords() const { return mCoords; } |
| 1492 | |
| 1493 | private: |
| 1494 | PointerProperties mProperties; |
| 1495 | PointerCoords mCoords; |
| 1496 | }; |
| 1497 | |
| 1498 | class MotionEventBuilder { |
| 1499 | public: |
| 1500 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1501 | mAction = action; |
| 1502 | mSource = source; |
| 1503 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1504 | mDownTime = mEventTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1507 | MotionEventBuilder& deviceId(int32_t deviceId) { |
| 1508 | mDeviceId = deviceId; |
| 1509 | return *this; |
| 1510 | } |
| 1511 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1512 | MotionEventBuilder& downTime(nsecs_t downTime) { |
| 1513 | mDownTime = downTime; |
| 1514 | return *this; |
| 1515 | } |
| 1516 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1517 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1518 | mEventTime = eventTime; |
| 1519 | return *this; |
| 1520 | } |
| 1521 | |
| 1522 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1523 | mDisplayId = displayId; |
| 1524 | return *this; |
| 1525 | } |
| 1526 | |
| 1527 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1528 | mActionButton = actionButton; |
| 1529 | return *this; |
| 1530 | } |
| 1531 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1532 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1533 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1534 | return *this; |
| 1535 | } |
| 1536 | |
| 1537 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1538 | mRawXCursorPosition = rawXCursorPosition; |
| 1539 | return *this; |
| 1540 | } |
| 1541 | |
| 1542 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1543 | mRawYCursorPosition = rawYCursorPosition; |
| 1544 | return *this; |
| 1545 | } |
| 1546 | |
| 1547 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1548 | mPointers.push_back(pointer); |
| 1549 | return *this; |
| 1550 | } |
| 1551 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1552 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1553 | mFlags |= flags; |
| 1554 | return *this; |
| 1555 | } |
| 1556 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1557 | MotionEvent build() { |
| 1558 | std::vector<PointerProperties> pointerProperties; |
| 1559 | std::vector<PointerCoords> pointerCoords; |
| 1560 | for (const PointerBuilder& pointer : mPointers) { |
| 1561 | pointerProperties.push_back(pointer.buildProperties()); |
| 1562 | pointerCoords.push_back(pointer.buildCoords()); |
| 1563 | } |
| 1564 | |
| 1565 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1566 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1567 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1568 | mPointers.size() == 1) { |
| 1569 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1570 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1571 | } |
| 1572 | |
| 1573 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1574 | ui::Transform identityTransform; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1575 | event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1576 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1577 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1578 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1579 | mRawYCursorPosition, identityTransform, mDownTime, mEventTime, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1580 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1581 | |
| 1582 | return event; |
| 1583 | } |
| 1584 | |
| 1585 | private: |
| 1586 | int32_t mAction; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1587 | int32_t mDeviceId = DEVICE_ID; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1588 | int32_t mSource; |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1589 | nsecs_t mDownTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1590 | nsecs_t mEventTime; |
| 1591 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1592 | int32_t mActionButton{0}; |
| 1593 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1594 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1595 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1596 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1597 | |
| 1598 | std::vector<PointerBuilder> mPointers; |
| 1599 | }; |
| 1600 | |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1601 | class MotionArgsBuilder { |
| 1602 | public: |
| 1603 | MotionArgsBuilder(int32_t action, int32_t source) { |
| 1604 | mAction = action; |
| 1605 | mSource = source; |
| 1606 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1607 | mDownTime = mEventTime; |
| 1608 | } |
| 1609 | |
| 1610 | MotionArgsBuilder& deviceId(int32_t deviceId) { |
| 1611 | mDeviceId = deviceId; |
| 1612 | return *this; |
| 1613 | } |
| 1614 | |
| 1615 | MotionArgsBuilder& downTime(nsecs_t downTime) { |
| 1616 | mDownTime = downTime; |
| 1617 | return *this; |
| 1618 | } |
| 1619 | |
| 1620 | MotionArgsBuilder& eventTime(nsecs_t eventTime) { |
| 1621 | mEventTime = eventTime; |
| 1622 | return *this; |
| 1623 | } |
| 1624 | |
| 1625 | MotionArgsBuilder& displayId(int32_t displayId) { |
| 1626 | mDisplayId = displayId; |
| 1627 | return *this; |
| 1628 | } |
| 1629 | |
| 1630 | MotionArgsBuilder& policyFlags(int32_t policyFlags) { |
| 1631 | mPolicyFlags = policyFlags; |
| 1632 | return *this; |
| 1633 | } |
| 1634 | |
| 1635 | MotionArgsBuilder& actionButton(int32_t actionButton) { |
| 1636 | mActionButton = actionButton; |
| 1637 | return *this; |
| 1638 | } |
| 1639 | |
| 1640 | MotionArgsBuilder& buttonState(int32_t buttonState) { |
| 1641 | mButtonState = buttonState; |
| 1642 | return *this; |
| 1643 | } |
| 1644 | |
| 1645 | MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1646 | mRawXCursorPosition = rawXCursorPosition; |
| 1647 | return *this; |
| 1648 | } |
| 1649 | |
| 1650 | MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1651 | mRawYCursorPosition = rawYCursorPosition; |
| 1652 | return *this; |
| 1653 | } |
| 1654 | |
| 1655 | MotionArgsBuilder& pointer(PointerBuilder pointer) { |
| 1656 | mPointers.push_back(pointer); |
| 1657 | return *this; |
| 1658 | } |
| 1659 | |
| 1660 | MotionArgsBuilder& addFlag(uint32_t flags) { |
| 1661 | mFlags |= flags; |
| 1662 | return *this; |
| 1663 | } |
| 1664 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 1665 | MotionArgsBuilder& classification(MotionClassification classification) { |
| 1666 | mClassification = classification; |
| 1667 | return *this; |
| 1668 | } |
| 1669 | |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1670 | NotifyMotionArgs build() { |
| 1671 | std::vector<PointerProperties> pointerProperties; |
| 1672 | std::vector<PointerCoords> pointerCoords; |
| 1673 | for (const PointerBuilder& pointer : mPointers) { |
| 1674 | pointerProperties.push_back(pointer.buildProperties()); |
| 1675 | pointerCoords.push_back(pointer.buildCoords()); |
| 1676 | } |
| 1677 | |
| 1678 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1679 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1680 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1681 | mPointers.size() == 1) { |
| 1682 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1683 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1684 | } |
| 1685 | |
| 1686 | NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId, |
| 1687 | mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags, |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 1688 | AMETA_NONE, mButtonState, mClassification, /*edgeFlags=*/0, |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1689 | mPointers.size(), pointerProperties.data(), pointerCoords.data(), |
| 1690 | /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition, |
| 1691 | mRawYCursorPosition, mDownTime, /*videoFrames=*/{}); |
| 1692 | |
| 1693 | return args; |
| 1694 | } |
| 1695 | |
| 1696 | private: |
| 1697 | int32_t mAction; |
| 1698 | int32_t mDeviceId = DEVICE_ID; |
| 1699 | int32_t mSource; |
| 1700 | nsecs_t mDownTime; |
| 1701 | nsecs_t mEventTime; |
| 1702 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1703 | int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; |
| 1704 | int32_t mActionButton{0}; |
| 1705 | int32_t mButtonState{0}; |
| 1706 | int32_t mFlags{0}; |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 1707 | MotionClassification mClassification{MotionClassification::NONE}; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1708 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1709 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1710 | |
| 1711 | std::vector<PointerBuilder> mPointers; |
| 1712 | }; |
| 1713 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1714 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1715 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1716 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1717 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1718 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1719 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1720 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1723 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1724 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1725 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1726 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1727 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1728 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1729 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1730 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1731 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame^] | 1732 | MotionEventBuilder motionBuilder = |
| 1733 | MotionEventBuilder(action, source) |
| 1734 | .displayId(displayId) |
| 1735 | .eventTime(eventTime) |
| 1736 | .rawXCursorPosition(cursorPosition.x) |
| 1737 | .rawYCursorPosition(cursorPosition.y) |
| 1738 | .pointer( |
| 1739 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1740 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1741 | motionBuilder.downTime(eventTime); |
| 1742 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1743 | |
| 1744 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame^] | 1745 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1746 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1747 | } |
| 1748 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1749 | static InputEventInjectionResult injectMotionDown( |
| 1750 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1751 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1752 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1755 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1756 | int32_t source, int32_t displayId, |
| 1757 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1758 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1761 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1762 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1763 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1764 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1765 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1766 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1767 | |
| 1768 | return args; |
| 1769 | } |
| 1770 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1771 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1772 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1773 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1774 | // Define a valid key event. |
| 1775 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1776 | displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON, |
| 1777 | currentTime); |
| 1778 | |
| 1779 | return args; |
| 1780 | } |
| 1781 | |
| 1782 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1783 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1784 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1785 | // Define a valid key event. |
| 1786 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1787 | displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT, |
| 1788 | AMETA_NONE, currentTime); |
| 1789 | |
| 1790 | return args; |
| 1791 | } |
| 1792 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1793 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1794 | int32_t displayId, |
| 1795 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1796 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1797 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1798 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1799 | } |
| 1800 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1801 | PointerProperties pointerProperties[pointerCount]; |
| 1802 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1803 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1804 | for (size_t i = 0; i < pointerCount; i++) { |
| 1805 | pointerProperties[i].clear(); |
| 1806 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1807 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1808 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1809 | pointerCoords[i].clear(); |
| 1810 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1811 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1812 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1813 | |
| 1814 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1815 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1816 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1817 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1818 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1819 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1820 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1821 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1822 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1823 | |
| 1824 | return args; |
| 1825 | } |
| 1826 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1827 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1828 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1829 | } |
| 1830 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1831 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1832 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1833 | } |
| 1834 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1835 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1836 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1837 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1840 | /** |
| 1841 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1842 | * broken channel. |
| 1843 | */ |
| 1844 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1845 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1846 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1847 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1848 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1849 | |
| 1850 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1851 | |
| 1852 | // Window closes its channel, but the window remains. |
| 1853 | window->destroyReceiver(); |
| 1854 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1855 | } |
| 1856 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1857 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1858 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1859 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1860 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1861 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1862 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1863 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1864 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1865 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1866 | |
| 1867 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1868 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1869 | } |
| 1870 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1871 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1872 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1873 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1874 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1875 | |
| 1876 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1877 | // Inject a MotionEvent to an unknown display. |
| 1878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1879 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1880 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1881 | |
| 1882 | // Window should receive motion event. |
| 1883 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1884 | } |
| 1885 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1886 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1887 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1888 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1889 | * called twice. |
| 1890 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1891 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1892 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1893 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1894 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1895 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1896 | |
| 1897 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1898 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1899 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1900 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1901 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1902 | |
| 1903 | // Window should receive motion event. |
| 1904 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1909 | */ |
| 1910 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1911 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1912 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1913 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1914 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1915 | |
| 1916 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1917 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1918 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1919 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1920 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1921 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1922 | |
| 1923 | // Window should receive motion event. |
| 1924 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1925 | } |
| 1926 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1927 | // The foreground window should receive the first touch down event. |
| 1928 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1929 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1930 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1931 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1932 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1933 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1934 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1935 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1936 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1937 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1939 | |
| 1940 | // 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] | 1941 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1942 | windowSecond->assertNoEvents(); |
| 1943 | } |
| 1944 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1945 | /** |
| 1946 | * Two windows: A top window, and a wallpaper behind the window. |
| 1947 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1948 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1949 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1950 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1951 | */ |
| 1952 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1953 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1954 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1955 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1956 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1957 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1958 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1959 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1960 | |
| 1961 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1962 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1963 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1964 | {100, 200})) |
| 1965 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1966 | |
| 1967 | // Both foreground window and its wallpaper should receive the touch down |
| 1968 | foregroundWindow->consumeMotionDown(); |
| 1969 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1970 | |
| 1971 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1972 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1973 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1974 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1975 | |
| 1976 | foregroundWindow->consumeMotionMove(); |
| 1977 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1978 | |
| 1979 | // Now the foreground window goes away, but the wallpaper stays |
| 1980 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1981 | foregroundWindow->consumeMotionCancel(); |
| 1982 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1983 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1984 | } |
| 1985 | |
| 1986 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1987 | * Two fingers down on the window, and lift off the first finger. |
| 1988 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1989 | * contains a single pointer. |
| 1990 | */ |
| 1991 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1992 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1993 | sp<FakeWindowHandle> window = |
| 1994 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1995 | |
| 1996 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1997 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1998 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1999 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2000 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2001 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2002 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2003 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2004 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 2005 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2006 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2007 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2008 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2009 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 2010 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2011 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2012 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2013 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2014 | |
| 2015 | // Remove the window. The gesture should be canceled |
| 2016 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 2017 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 2018 | window->consumeMotionEvent( |
| 2019 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 2020 | } |
| 2021 | |
| 2022 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 2023 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 2024 | * with the following differences: |
| 2025 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 2026 | * clean up the connection. |
| 2027 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 2028 | * Ensure that there's no crash in the dispatcher. |
| 2029 | */ |
| 2030 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 2031 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2032 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2033 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2034 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 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 | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 2038 | |
| 2039 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 2040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2041 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2042 | {100, 200})) |
| 2043 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2044 | |
| 2045 | // Both foreground window and its wallpaper should receive the touch down |
| 2046 | foregroundWindow->consumeMotionDown(); |
| 2047 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2048 | |
| 2049 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2050 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2051 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 2052 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2053 | |
| 2054 | foregroundWindow->consumeMotionMove(); |
| 2055 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2056 | |
| 2057 | // Wallpaper closes its channel, but the window remains. |
| 2058 | wallpaperWindow->destroyReceiver(); |
| 2059 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 2060 | |
| 2061 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 2062 | // is no longer valid. |
| 2063 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 2064 | foregroundWindow->consumeMotionCancel(); |
| 2065 | } |
| 2066 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2067 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 2068 | public ::testing::WithParamInterface<bool> {}; |
| 2069 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 2070 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 2071 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2072 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 2073 | * The top window gets a multitouch gesture. |
| 2074 | * Ensure that wallpaper gets the same gesture. |
| 2075 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2076 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2077 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2078 | sp<FakeWindowHandle> foregroundWindow = |
| 2079 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 2080 | foregroundWindow->setDupTouchToWallpaper(true); |
| 2081 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2082 | |
| 2083 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2084 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2085 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2086 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2087 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2088 | |
| 2089 | // Touch down on top window |
| 2090 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2091 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2092 | {100, 100})) |
| 2093 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2094 | |
| 2095 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2096 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2097 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2098 | |
| 2099 | // Second finger down on the top window |
| 2100 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2101 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2102 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2103 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2104 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2105 | .build(); |
| 2106 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2107 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2108 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2109 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2110 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2111 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 2112 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2113 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2114 | |
| 2115 | const MotionEvent secondFingerUpEvent = |
| 2116 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2117 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2118 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2119 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2120 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2121 | .build(); |
| 2122 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2123 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 2124 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2125 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2126 | foregroundWindow->consumeMotionPointerUp(0); |
| 2127 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2128 | |
| 2129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2130 | injectMotionEvent(mDispatcher, |
| 2131 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2132 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2133 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2134 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2135 | .pointer(PointerBuilder(/* id */ 1, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2136 | ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2137 | .x(100) |
| 2138 | .y(100)) |
| 2139 | .build(), |
| 2140 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2141 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2142 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2143 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | /** |
| 2147 | * Two windows: a window on the left and window on the right. |
| 2148 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 2149 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 2150 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 2151 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 2152 | * ACTION_POINTER_DOWN(1). |
| 2153 | */ |
| 2154 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 2155 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2156 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2157 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2158 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2159 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2160 | |
| 2161 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2162 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2163 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2164 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2165 | |
| 2166 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2167 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2168 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2169 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2170 | |
| 2171 | mDispatcher->setInputWindows( |
| 2172 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 2173 | |
| 2174 | // Touch down on left window |
| 2175 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2176 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2177 | {100, 100})) |
| 2178 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2179 | |
| 2180 | // Both foreground window and its wallpaper should receive the touch down |
| 2181 | leftWindow->consumeMotionDown(); |
| 2182 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2183 | |
| 2184 | // Second finger down on the right window |
| 2185 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2186 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2187 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2188 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2189 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2190 | .build(); |
| 2191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2192 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2193 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2194 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2195 | |
| 2196 | leftWindow->consumeMotionMove(); |
| 2197 | // Since the touch is split, right window gets ACTION_DOWN |
| 2198 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2199 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2200 | expectedWallpaperFlags); |
| 2201 | |
| 2202 | // Now, leftWindow, which received the first finger, disappears. |
| 2203 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 2204 | leftWindow->consumeMotionCancel(); |
| 2205 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2206 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2207 | |
| 2208 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2209 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2210 | const MotionEvent secondFingerMoveEvent = |
| 2211 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2212 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2213 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2214 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2215 | .build(); |
| 2216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2217 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2218 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2219 | rightWindow->consumeMotionMove(); |
| 2220 | |
| 2221 | leftWindow->assertNoEvents(); |
| 2222 | rightWindow->assertNoEvents(); |
| 2223 | wallpaperWindow->assertNoEvents(); |
| 2224 | } |
| 2225 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2226 | /** |
| 2227 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2228 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2229 | * The right window should receive ACTION_DOWN. |
| 2230 | */ |
| 2231 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2232 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2233 | sp<FakeWindowHandle> leftWindow = |
| 2234 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2235 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2236 | leftWindow->setDupTouchToWallpaper(true); |
| 2237 | leftWindow->setSlippery(true); |
| 2238 | |
| 2239 | sp<FakeWindowHandle> rightWindow = |
| 2240 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2241 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2242 | |
| 2243 | sp<FakeWindowHandle> wallpaperWindow = |
| 2244 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2245 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2246 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2247 | mDispatcher->setInputWindows( |
| 2248 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2249 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2250 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2251 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2252 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2253 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2254 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2255 | |
| 2256 | // Both foreground window and its wallpaper should receive the touch down |
| 2257 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2258 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2259 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2260 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2261 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2262 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2263 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2264 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2265 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2266 | leftWindow->consumeMotionCancel(); |
| 2267 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2268 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2271 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2272 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2273 | * interactive, it might stop sending this flag. |
| 2274 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2275 | * to have a consistent input stream. |
| 2276 | * |
| 2277 | * Test procedure: |
| 2278 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2279 | * DOWN (new gesture). |
| 2280 | * |
| 2281 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2282 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2283 | * |
| 2284 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2285 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2286 | */ |
| 2287 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2288 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2289 | sp<FakeWindowHandle> spyWindow = |
| 2290 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2291 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2292 | spyWindow->setTrustedOverlay(true); |
| 2293 | spyWindow->setSpy(true); |
| 2294 | |
| 2295 | sp<FakeWindowHandle> window = |
| 2296 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2297 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2298 | |
| 2299 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2300 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2301 | |
| 2302 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2303 | mDispatcher->notifyMotion( |
| 2304 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2305 | .deviceId(touchDeviceId) |
| 2306 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2307 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2308 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2309 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2310 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2311 | .deviceId(touchDeviceId) |
| 2312 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2313 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2314 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2315 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2316 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2317 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2318 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2319 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2320 | |
| 2321 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2322 | mDispatcher->notifyMotion( |
| 2323 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2324 | .deviceId(touchDeviceId) |
| 2325 | .policyFlags(0) |
| 2326 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2327 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2328 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2329 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2330 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2331 | |
| 2332 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2333 | // 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] | 2334 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2335 | |
| 2336 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2337 | mDispatcher->notifyMotion( |
| 2338 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2339 | .deviceId(touchDeviceId) |
| 2340 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2341 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2342 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2343 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2344 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2345 | |
| 2346 | // No more events |
| 2347 | spyWindow->assertNoEvents(); |
| 2348 | window->assertNoEvents(); |
| 2349 | } |
| 2350 | |
| 2351 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2352 | * Two windows: a window on the left and a window on the right. |
| 2353 | * Mouse is hovered from the right window into the left window. |
| 2354 | * Next, we tap on the left window, where the cursor was last seen. |
| 2355 | * The second tap is done onto the right window. |
| 2356 | * The mouse and tap are from two different devices. |
| 2357 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2358 | * explicitly helps during debugging. |
| 2359 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2360 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2361 | */ |
| 2362 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2363 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2364 | sp<FakeWindowHandle> leftWindow = |
| 2365 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2366 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2367 | |
| 2368 | sp<FakeWindowHandle> rightWindow = |
| 2369 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2370 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2371 | |
| 2372 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2373 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2374 | // stale. |
| 2375 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2376 | const int32_t mouseDeviceId = 6; |
| 2377 | const int32_t touchDeviceId = 4; |
| 2378 | // Move the cursor from right |
| 2379 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2380 | injectMotionEvent(mDispatcher, |
| 2381 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2382 | AINPUT_SOURCE_MOUSE) |
| 2383 | .deviceId(mouseDeviceId) |
| 2384 | .downTime(baseTime + 10) |
| 2385 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2386 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2387 | .x(300) |
| 2388 | .y(100)) |
| 2389 | .build())); |
| 2390 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2391 | |
| 2392 | // .. to the left window |
| 2393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2394 | injectMotionEvent(mDispatcher, |
| 2395 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2396 | AINPUT_SOURCE_MOUSE) |
| 2397 | .deviceId(mouseDeviceId) |
| 2398 | .downTime(baseTime + 10) |
| 2399 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2400 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2401 | .x(110) |
| 2402 | .y(100)) |
| 2403 | .build())); |
| 2404 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2405 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2406 | // Now tap the left window |
| 2407 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2408 | injectMotionEvent(mDispatcher, |
| 2409 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2410 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2411 | .deviceId(touchDeviceId) |
| 2412 | .downTime(baseTime + 40) |
| 2413 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2414 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2415 | .x(100) |
| 2416 | .y(100)) |
| 2417 | .build())); |
| 2418 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2419 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2420 | |
| 2421 | // release tap |
| 2422 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2423 | injectMotionEvent(mDispatcher, |
| 2424 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2425 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2426 | .deviceId(touchDeviceId) |
| 2427 | .downTime(baseTime + 40) |
| 2428 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2429 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2430 | .x(100) |
| 2431 | .y(100)) |
| 2432 | .build())); |
| 2433 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2434 | |
| 2435 | // Tap the window on the right |
| 2436 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2437 | injectMotionEvent(mDispatcher, |
| 2438 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2439 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2440 | .deviceId(touchDeviceId) |
| 2441 | .downTime(baseTime + 60) |
| 2442 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2443 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2444 | .x(300) |
| 2445 | .y(100)) |
| 2446 | .build())); |
| 2447 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2448 | |
| 2449 | // release tap |
| 2450 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2451 | injectMotionEvent(mDispatcher, |
| 2452 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2453 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2454 | .deviceId(touchDeviceId) |
| 2455 | .downTime(baseTime + 60) |
| 2456 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2457 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2458 | .x(300) |
| 2459 | .y(100)) |
| 2460 | .build())); |
| 2461 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2462 | |
| 2463 | // No more events |
| 2464 | leftWindow->assertNoEvents(); |
| 2465 | rightWindow->assertNoEvents(); |
| 2466 | } |
| 2467 | |
| 2468 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2469 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2470 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2471 | * While the top window is present, the hovering is stopped. |
| 2472 | * Later, hovering gets resumed again. |
| 2473 | * Ensure that new hover gesture is handled correctly. |
| 2474 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2475 | * to the window that's currently being hovered over. |
| 2476 | */ |
| 2477 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2478 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2479 | sp<FakeWindowHandle> window = |
| 2480 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2481 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2482 | |
| 2483 | // Only a single window is present at first |
| 2484 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2485 | |
| 2486 | // Start hovering in the window |
| 2487 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2488 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2489 | .build()); |
| 2490 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2491 | |
| 2492 | // Now, an obscuring window appears! |
| 2493 | sp<FakeWindowHandle> obscuringWindow = |
| 2494 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2495 | ADISPLAY_ID_DEFAULT, |
| 2496 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2497 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2498 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2499 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2500 | obscuringWindow->setNoInputChannel(true); |
| 2501 | obscuringWindow->setFocusable(false); |
| 2502 | obscuringWindow->setAlpha(1.0); |
| 2503 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2504 | |
| 2505 | // While this new obscuring window is present, the hovering is stopped |
| 2506 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2507 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2508 | .build()); |
| 2509 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2510 | |
| 2511 | // Now the obscuring window goes away. |
| 2512 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2513 | |
| 2514 | // And a new hover gesture starts. |
| 2515 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2516 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2517 | .build()); |
| 2518 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2519 | } |
| 2520 | |
| 2521 | /** |
| 2522 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2523 | * the obscuring window. |
| 2524 | */ |
| 2525 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2526 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2527 | sp<FakeWindowHandle> window = |
| 2528 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2529 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2530 | |
| 2531 | // Only a single window is present at first |
| 2532 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2533 | |
| 2534 | // Start hovering in the window |
| 2535 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2536 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2537 | .build()); |
| 2538 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2539 | |
| 2540 | // Now, an obscuring window appears! |
| 2541 | sp<FakeWindowHandle> obscuringWindow = |
| 2542 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2543 | ADISPLAY_ID_DEFAULT, |
| 2544 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2545 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2546 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2547 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2548 | obscuringWindow->setNoInputChannel(true); |
| 2549 | obscuringWindow->setFocusable(false); |
| 2550 | obscuringWindow->setAlpha(1.0); |
| 2551 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2552 | |
| 2553 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2554 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2555 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2556 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2557 | .build()); |
| 2558 | obscuringWindow->assertNoEvents(); |
| 2559 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2560 | |
| 2561 | // Now the obscuring window goes away. |
| 2562 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2563 | |
| 2564 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2565 | // so it should generate a HOVER_ENTER |
| 2566 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2567 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2568 | .build()); |
| 2569 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2570 | |
| 2571 | // Now the MOVE should be getting dispatched normally |
| 2572 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2573 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2574 | .build()); |
| 2575 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2576 | } |
| 2577 | |
| 2578 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2579 | * Two windows: a window on the left and a window on the right. |
| 2580 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2581 | * down. Then, on the left window, also place second touch pointer down. |
| 2582 | * This test tries to reproduce a crash. |
| 2583 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2584 | */ |
| 2585 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { |
| 2586 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2587 | sp<FakeWindowHandle> leftWindow = |
| 2588 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2589 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2590 | |
| 2591 | sp<FakeWindowHandle> rightWindow = |
| 2592 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2593 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2594 | |
| 2595 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2596 | |
| 2597 | const int32_t touchDeviceId = 4; |
| 2598 | const int32_t mouseDeviceId = 6; |
| 2599 | NotifyMotionArgs args; |
| 2600 | |
| 2601 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2602 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2603 | .deviceId(mouseDeviceId) |
| 2604 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2605 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2606 | leftWindow->consumeMotionEvent( |
| 2607 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2608 | |
| 2609 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2610 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2611 | .deviceId(mouseDeviceId) |
| 2612 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2613 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2614 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2615 | |
| 2616 | leftWindow->consumeMotionEvent( |
| 2617 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2618 | leftWindow->consumeMotionEvent( |
| 2619 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2620 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2621 | mDispatcher->notifyMotion( |
| 2622 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2623 | .deviceId(mouseDeviceId) |
| 2624 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2625 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2626 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2627 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2628 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2629 | |
| 2630 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2631 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2632 | .deviceId(touchDeviceId) |
| 2633 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2634 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2635 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2636 | |
| 2637 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2638 | |
| 2639 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2640 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2641 | .deviceId(touchDeviceId) |
| 2642 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2643 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2644 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2645 | leftWindow->consumeMotionEvent( |
| 2646 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2647 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2648 | // current implementation. |
| 2649 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2650 | rightWindow->consumeMotionEvent( |
| 2651 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2652 | |
| 2653 | leftWindow->assertNoEvents(); |
| 2654 | rightWindow->assertNoEvents(); |
| 2655 | } |
| 2656 | |
| 2657 | /** |
| 2658 | * On a single window, use two different devices: mouse and touch. |
| 2659 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2660 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2661 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2662 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2663 | * represent a new gesture. |
| 2664 | */ |
| 2665 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { |
| 2666 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2667 | sp<FakeWindowHandle> window = |
| 2668 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2669 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2670 | |
| 2671 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2672 | |
| 2673 | const int32_t touchDeviceId = 4; |
| 2674 | const int32_t mouseDeviceId = 6; |
| 2675 | NotifyMotionArgs args; |
| 2676 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2677 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2678 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2679 | .deviceId(touchDeviceId) |
| 2680 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2681 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2682 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2683 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2684 | .deviceId(touchDeviceId) |
| 2685 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2686 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2687 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2688 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2689 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2690 | .deviceId(touchDeviceId) |
| 2691 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2692 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2693 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2694 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2695 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2696 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2697 | |
| 2698 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2699 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2700 | .deviceId(mouseDeviceId) |
| 2701 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2702 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2703 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2704 | |
| 2705 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2706 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2707 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2708 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2709 | mDispatcher->notifyMotion( |
| 2710 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2711 | .deviceId(mouseDeviceId) |
| 2712 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2713 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2714 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2715 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2716 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2717 | |
| 2718 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2719 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2720 | .deviceId(touchDeviceId) |
| 2721 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2722 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2723 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2724 | // The pointer_down event should be ignored |
| 2725 | window->assertNoEvents(); |
| 2726 | } |
| 2727 | |
| 2728 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2729 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 2730 | * the injected event. |
| 2731 | */ |
| 2732 | TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) { |
| 2733 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2734 | sp<FakeWindowHandle> window = |
| 2735 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2736 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2737 | |
| 2738 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2739 | |
| 2740 | const int32_t touchDeviceId = 4; |
| 2741 | NotifyMotionArgs args; |
| 2742 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 2743 | // completion. |
| 2744 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2745 | injectMotionEvent(mDispatcher, |
| 2746 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2747 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2748 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2749 | .x(50) |
| 2750 | .y(50)) |
| 2751 | .build())); |
| 2752 | window->consumeMotionEvent( |
| 2753 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2754 | |
| 2755 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 2756 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2757 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2758 | .deviceId(touchDeviceId) |
| 2759 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2760 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2761 | |
| 2762 | window->consumeMotionEvent( |
| 2763 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2764 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2765 | } |
| 2766 | |
| 2767 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2768 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2769 | * |
| 2770 | * Two windows: a window on the left and a window on the right. |
| 2771 | * Mouse is hovered over the left window. |
| 2772 | * Next, we tap on the left window, where the cursor was last seen. |
| 2773 | * |
| 2774 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2775 | * the left window. |
| 2776 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2777 | * window (first), and then another on the left window (second). |
| 2778 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2779 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2780 | */ |
| 2781 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2782 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2783 | sp<FakeWindowHandle> leftWindow = |
| 2784 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2785 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2786 | |
| 2787 | sp<FakeWindowHandle> rightWindow = |
| 2788 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2789 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2790 | |
| 2791 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2792 | |
| 2793 | const int32_t mouseDeviceId = 6; |
| 2794 | const int32_t touchDeviceId = 4; |
| 2795 | // Hover over the left window. Keep the cursor there. |
| 2796 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2797 | injectMotionEvent(mDispatcher, |
| 2798 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2799 | AINPUT_SOURCE_MOUSE) |
| 2800 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2801 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2802 | .x(50) |
| 2803 | .y(50)) |
| 2804 | .build())); |
| 2805 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2806 | |
| 2807 | // Tap on left window |
| 2808 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2809 | injectMotionEvent(mDispatcher, |
| 2810 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2811 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2812 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2813 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2814 | .x(100) |
| 2815 | .y(100)) |
| 2816 | .build())); |
| 2817 | |
| 2818 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2819 | injectMotionEvent(mDispatcher, |
| 2820 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2821 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2822 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2823 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2824 | .x(100) |
| 2825 | .y(100)) |
| 2826 | .build())); |
| 2827 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2828 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2829 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2830 | |
| 2831 | // First finger down on right window |
| 2832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2833 | injectMotionEvent(mDispatcher, |
| 2834 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2835 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2836 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2837 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2838 | .x(300) |
| 2839 | .y(100)) |
| 2840 | .build())); |
| 2841 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2842 | |
| 2843 | // Second finger down on the left window |
| 2844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2845 | injectMotionEvent(mDispatcher, |
| 2846 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2847 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2848 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2849 | .x(300) |
| 2850 | .y(100)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2851 | .pointer(PointerBuilder(1, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2852 | .x(100) |
| 2853 | .y(100)) |
| 2854 | .build())); |
| 2855 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2856 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2857 | |
| 2858 | // No more events |
| 2859 | leftWindow->assertNoEvents(); |
| 2860 | rightWindow->assertNoEvents(); |
| 2861 | } |
| 2862 | |
| 2863 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2864 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2865 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2866 | * touch is gone, stylus hovering should start working again. |
| 2867 | */ |
| 2868 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 2869 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2870 | sp<FakeWindowHandle> window = |
| 2871 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2872 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2873 | |
| 2874 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2875 | |
| 2876 | const int32_t stylusDeviceId = 5; |
| 2877 | const int32_t touchDeviceId = 4; |
| 2878 | // Start hovering with stylus |
| 2879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2880 | injectMotionEvent(mDispatcher, |
| 2881 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2882 | AINPUT_SOURCE_STYLUS) |
| 2883 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2884 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2885 | .x(50) |
| 2886 | .y(50)) |
| 2887 | .build())); |
| 2888 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2889 | |
| 2890 | // Finger down on the window |
| 2891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2892 | injectMotionEvent(mDispatcher, |
| 2893 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2894 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2895 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2896 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2897 | .x(100) |
| 2898 | .y(100)) |
| 2899 | .build())); |
| 2900 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2901 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2902 | |
| 2903 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2904 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2905 | injectMotionEvent(mDispatcher, |
| 2906 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2907 | AINPUT_SOURCE_STYLUS) |
| 2908 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2909 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2910 | .x(50) |
| 2911 | .y(50)) |
| 2912 | .build())); |
| 2913 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2914 | // is already down. |
| 2915 | |
| 2916 | // Lift up the finger |
| 2917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2918 | injectMotionEvent(mDispatcher, |
| 2919 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2920 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2921 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2922 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2923 | .x(100) |
| 2924 | .y(100)) |
| 2925 | .build())); |
| 2926 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2927 | |
| 2928 | // Now that the touch is gone, stylus hovering should start working again |
| 2929 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2930 | injectMotionEvent(mDispatcher, |
| 2931 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2932 | AINPUT_SOURCE_STYLUS) |
| 2933 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2934 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2935 | .x(50) |
| 2936 | .y(50)) |
| 2937 | .build())); |
| 2938 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2939 | // No more events |
| 2940 | window->assertNoEvents(); |
| 2941 | } |
| 2942 | |
| 2943 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2944 | * A spy window above a window with no input channel. |
| 2945 | * Start hovering with a stylus device, and then tap with it. |
| 2946 | * Ensure spy window receives the entire sequence. |
| 2947 | */ |
| 2948 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 2949 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2950 | sp<FakeWindowHandle> spyWindow = |
| 2951 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2952 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2953 | spyWindow->setTrustedOverlay(true); |
| 2954 | spyWindow->setSpy(true); |
| 2955 | sp<FakeWindowHandle> window = |
| 2956 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2957 | window->setNoInputChannel(true); |
| 2958 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2959 | |
| 2960 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2961 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2962 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2963 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2964 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2965 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2966 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2967 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2968 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2969 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2970 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2971 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2972 | |
| 2973 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2974 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2975 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2976 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2977 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2978 | |
| 2979 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2980 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 2981 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2982 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2983 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 2984 | |
| 2985 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2986 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2987 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2988 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2989 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2990 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2991 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2992 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2993 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2994 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2995 | |
| 2996 | // No more events |
| 2997 | spyWindow->assertNoEvents(); |
| 2998 | window->assertNoEvents(); |
| 2999 | } |
| 3000 | |
| 3001 | /** |
| 3002 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3003 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3004 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3005 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3006 | */ |
| 3007 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3008 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3009 | sp<FakeWindowHandle> spyWindow = |
| 3010 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3011 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3012 | spyWindow->setTrustedOverlay(true); |
| 3013 | spyWindow->setSpy(true); |
| 3014 | sp<FakeWindowHandle> window = |
| 3015 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3016 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3017 | |
| 3018 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3019 | |
| 3020 | const int32_t mouseDeviceId = 7; |
| 3021 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3022 | |
| 3023 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3024 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3025 | .deviceId(mouseDeviceId) |
| 3026 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3027 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3028 | spyWindow->consumeMotionEvent( |
| 3029 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3030 | window->consumeMotionEvent( |
| 3031 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3032 | |
| 3033 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3034 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3035 | .deviceId(touchDeviceId) |
| 3036 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3037 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3038 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3039 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3040 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3041 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3042 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3043 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3044 | .deviceId(touchDeviceId) |
| 3045 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3046 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3047 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3048 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3049 | |
| 3050 | // Pilfer the stream |
| 3051 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3052 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3053 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3054 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3055 | .deviceId(touchDeviceId) |
| 3056 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3057 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3058 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3059 | |
| 3060 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3061 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3062 | .deviceId(mouseDeviceId) |
| 3063 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3064 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3065 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3066 | |
| 3067 | spyWindow->consumeMotionEvent( |
| 3068 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3069 | spyWindow->consumeMotionEvent( |
| 3070 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3071 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3072 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3073 | mDispatcher->notifyMotion( |
| 3074 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3075 | .deviceId(mouseDeviceId) |
| 3076 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3077 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3078 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3079 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3080 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3081 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3082 | |
| 3083 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3084 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3085 | .deviceId(mouseDeviceId) |
| 3086 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3087 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3088 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3089 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3090 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3091 | |
| 3092 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3093 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3094 | .deviceId(touchDeviceId) |
| 3095 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3096 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3097 | |
| 3098 | // No more events |
| 3099 | spyWindow->assertNoEvents(); |
| 3100 | window->assertNoEvents(); |
| 3101 | } |
| 3102 | |
| 3103 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3104 | * On the display, have a single window, and also an area where there's no window. |
| 3105 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3106 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3107 | */ |
| 3108 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3109 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3110 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3111 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3112 | |
| 3113 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3114 | |
| 3115 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3116 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3117 | |
| 3118 | mDispatcher->waitForIdle(); |
| 3119 | window->assertNoEvents(); |
| 3120 | |
| 3121 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3122 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3123 | mDispatcher->waitForIdle(); |
| 3124 | window->consumeMotionDown(); |
| 3125 | } |
| 3126 | |
| 3127 | /** |
| 3128 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3129 | * non-touchable window. |
| 3130 | */ |
| 3131 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3132 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3133 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3134 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3135 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3136 | window1->setTouchable(false); |
| 3137 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3138 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3139 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3140 | |
| 3141 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 3142 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3143 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3144 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3145 | |
| 3146 | mDispatcher->waitForIdle(); |
| 3147 | window1->assertNoEvents(); |
| 3148 | window2->assertNoEvents(); |
| 3149 | |
| 3150 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3151 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3152 | mDispatcher->waitForIdle(); |
| 3153 | window2->consumeMotionDown(); |
| 3154 | } |
| 3155 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3156 | /** |
| 3157 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3158 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3159 | */ |
| 3160 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3161 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3162 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3163 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3164 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3165 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3166 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3167 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3168 | |
| 3169 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 3170 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3171 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3172 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3173 | |
| 3174 | mDispatcher->waitForIdle(); |
| 3175 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3176 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3177 | window2->assertNoEvents(); |
| 3178 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 3179 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 3180 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 3181 | |
| 3182 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3183 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3184 | mDispatcher->waitForIdle(); |
| 3185 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3186 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3187 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 3188 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 3189 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 3190 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 3191 | |
| 3192 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3193 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3194 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3195 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3196 | |
| 3197 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3198 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3199 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3200 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3201 | |
| 3202 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3203 | window1->consumeMotionMove(); |
| 3204 | window1->assertNoEvents(); |
| 3205 | |
| 3206 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3207 | mDispatcher->notifyMotion( |
| 3208 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3209 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3210 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3211 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3212 | mDispatcher->notifyMotion( |
| 3213 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3214 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3215 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3218 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3219 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3220 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3221 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3222 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3223 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3224 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3225 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3226 | |
| 3227 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3228 | |
| 3229 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 3230 | |
| 3231 | // 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] | 3232 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3233 | injectMotionEvent(mDispatcher, |
| 3234 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3235 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3236 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3237 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3238 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3239 | |
| 3240 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3241 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3242 | injectMotionEvent(mDispatcher, |
| 3243 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3244 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3245 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3246 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3247 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3248 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3249 | |
| 3250 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3251 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3252 | injectMotionEvent(mDispatcher, |
| 3253 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3254 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3255 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3256 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3257 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3258 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3259 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3260 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3261 | injectMotionEvent(mDispatcher, |
| 3262 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3263 | AINPUT_SOURCE_MOUSE) |
| 3264 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3265 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3266 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3267 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3268 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3269 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3271 | injectMotionEvent(mDispatcher, |
| 3272 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3273 | AINPUT_SOURCE_MOUSE) |
| 3274 | .buttonState(0) |
| 3275 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3276 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3277 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3278 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3279 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3281 | injectMotionEvent(mDispatcher, |
| 3282 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3283 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3284 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3285 | .build())); |
| 3286 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3287 | |
| 3288 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3289 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3290 | injectMotionEvent(mDispatcher, |
| 3291 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3292 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3293 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3294 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3295 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3296 | |
| 3297 | // No more events |
| 3298 | windowLeft->assertNoEvents(); |
| 3299 | windowRight->assertNoEvents(); |
| 3300 | } |
| 3301 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3302 | /** |
| 3303 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3304 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3305 | * currently active gesture should be canceled, and the new one should proceed. |
| 3306 | */ |
| 3307 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3308 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3309 | sp<FakeWindowHandle> window = |
| 3310 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3311 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3312 | |
| 3313 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3314 | |
| 3315 | const int32_t touchDeviceId = 4; |
| 3316 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3317 | |
| 3318 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3319 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3320 | .deviceId(touchDeviceId) |
| 3321 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3322 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3323 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3324 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3325 | .deviceId(touchDeviceId) |
| 3326 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3327 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3328 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3329 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3330 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3331 | |
| 3332 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3333 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3334 | .deviceId(mouseDeviceId) |
| 3335 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3336 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3337 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3338 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3339 | WithPointerCount(2u))); |
| 3340 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3341 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3342 | mDispatcher->notifyMotion( |
| 3343 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3344 | .deviceId(mouseDeviceId) |
| 3345 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3346 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3347 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3348 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3349 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3350 | |
| 3351 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3352 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3353 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3354 | .deviceId(touchDeviceId) |
| 3355 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3356 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3357 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3358 | window->assertNoEvents(); |
| 3359 | } |
| 3360 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3361 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3362 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3363 | |
| 3364 | sp<FakeWindowHandle> spyWindow = |
| 3365 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3366 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3367 | spyWindow->setTrustedOverlay(true); |
| 3368 | spyWindow->setSpy(true); |
| 3369 | sp<FakeWindowHandle> window = |
| 3370 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3371 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3372 | |
| 3373 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3374 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3375 | |
| 3376 | // Send mouse cursor to the window |
| 3377 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3378 | injectMotionEvent(mDispatcher, |
| 3379 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3380 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3381 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3382 | .x(100) |
| 3383 | .y(100)) |
| 3384 | .build())); |
| 3385 | |
| 3386 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3387 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3388 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3389 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3390 | |
| 3391 | window->assertNoEvents(); |
| 3392 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3395 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3396 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3397 | |
| 3398 | sp<FakeWindowHandle> spyWindow = |
| 3399 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3400 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3401 | spyWindow->setTrustedOverlay(true); |
| 3402 | spyWindow->setSpy(true); |
| 3403 | sp<FakeWindowHandle> window = |
| 3404 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3405 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3406 | |
| 3407 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3408 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3409 | |
| 3410 | // Send mouse cursor to the window |
| 3411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3412 | injectMotionEvent(mDispatcher, |
| 3413 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3414 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3415 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3416 | .x(100) |
| 3417 | .y(100)) |
| 3418 | .build())); |
| 3419 | |
| 3420 | // Move mouse cursor |
| 3421 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3422 | injectMotionEvent(mDispatcher, |
| 3423 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3424 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3425 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3426 | .x(110) |
| 3427 | .y(110)) |
| 3428 | .build())); |
| 3429 | |
| 3430 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3431 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3432 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3433 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3434 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3435 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3436 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3437 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3438 | // Touch down on the window |
| 3439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3440 | injectMotionEvent(mDispatcher, |
| 3441 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3442 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3443 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3444 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3445 | .x(200) |
| 3446 | .y(200)) |
| 3447 | .build())); |
| 3448 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3449 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3450 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3451 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3452 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3453 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3454 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3455 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3456 | |
| 3457 | // pilfer the motion, retaining the gesture on the spy window. |
| 3458 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3459 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3460 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3461 | |
| 3462 | // Touch UP on the window |
| 3463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3464 | injectMotionEvent(mDispatcher, |
| 3465 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3466 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3467 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3468 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3469 | .x(200) |
| 3470 | .y(200)) |
| 3471 | .build())); |
| 3472 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3473 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3474 | |
| 3475 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3476 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3477 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3478 | |
| 3479 | // One more tap - DOWN |
| 3480 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3481 | injectMotionEvent(mDispatcher, |
| 3482 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3483 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3484 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3485 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3486 | .x(250) |
| 3487 | .y(250)) |
| 3488 | .build())); |
| 3489 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3490 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3491 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3492 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3493 | |
| 3494 | // Touch UP on the window |
| 3495 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3496 | injectMotionEvent(mDispatcher, |
| 3497 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3498 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3499 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3500 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3501 | .x(250) |
| 3502 | .y(250)) |
| 3503 | .build())); |
| 3504 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3505 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3506 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3507 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3508 | |
| 3509 | window->assertNoEvents(); |
| 3510 | spyWindow->assertNoEvents(); |
| 3511 | } |
| 3512 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3513 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3514 | // directly in this test. |
| 3515 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3516 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3517 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3518 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3519 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3520 | |
| 3521 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3522 | |
| 3523 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3524 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3525 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3526 | injectMotionEvent(mDispatcher, |
| 3527 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3528 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3529 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3530 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3531 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3532 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3533 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3534 | injectMotionEvent(mDispatcher, |
| 3535 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3536 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3537 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3538 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3539 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3540 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3541 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3543 | injectMotionEvent(mDispatcher, |
| 3544 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3545 | AINPUT_SOURCE_MOUSE) |
| 3546 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3547 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3548 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3549 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3550 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3551 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3552 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3553 | injectMotionEvent(mDispatcher, |
| 3554 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3555 | AINPUT_SOURCE_MOUSE) |
| 3556 | .buttonState(0) |
| 3557 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3558 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3559 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3560 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3561 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3562 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3563 | injectMotionEvent(mDispatcher, |
| 3564 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3565 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3566 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3567 | .build())); |
| 3568 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3569 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3570 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 3571 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 3572 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3573 | injectMotionEvent(mDispatcher, |
| 3574 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3575 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3576 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3577 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3578 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3579 | } |
| 3580 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3581 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3582 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3583 | * is generated. |
| 3584 | */ |
| 3585 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3586 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3587 | sp<FakeWindowHandle> window = |
| 3588 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3589 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3590 | |
| 3591 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3592 | |
| 3593 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3594 | |
| 3595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3596 | injectMotionEvent(mDispatcher, |
| 3597 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3598 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3599 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3600 | .x(300) |
| 3601 | .y(400)) |
| 3602 | .build())); |
| 3603 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3604 | |
| 3605 | // Remove the window, but keep the channel. |
| 3606 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3607 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3608 | } |
| 3609 | |
| 3610 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3611 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3612 | */ |
| 3613 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3614 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3615 | sp<FakeWindowHandle> window = |
| 3616 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3617 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3618 | |
| 3619 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3620 | |
| 3621 | const int32_t mouseDeviceId = 7; |
| 3622 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3623 | |
| 3624 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3625 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3626 | .deviceId(mouseDeviceId) |
| 3627 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 3628 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3629 | window->consumeMotionEvent( |
| 3630 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3631 | |
| 3632 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3633 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3634 | .deviceId(touchDeviceId) |
| 3635 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3636 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3637 | |
| 3638 | window->consumeMotionEvent( |
| 3639 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3640 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3641 | } |
| 3642 | |
| 3643 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3644 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3645 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3646 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3647 | */ |
| 3648 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3649 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3650 | sp<FakeWindowHandle> window = |
| 3651 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3652 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3653 | |
| 3654 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3655 | |
| 3656 | // Inject a hover_move from mouse. |
| 3657 | NotifyMotionArgs motionArgs = |
| 3658 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 3659 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3660 | motionArgs.xCursorPosition = 50; |
| 3661 | motionArgs.yCursorPosition = 50; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3662 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3663 | ASSERT_NO_FATAL_FAILURE( |
| 3664 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3665 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3666 | |
| 3667 | // Tap on the window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3668 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3669 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3670 | {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3671 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3672 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3673 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3674 | |
| 3675 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3676 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3677 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3678 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3679 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3680 | ADISPLAY_ID_DEFAULT, {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3681 | ASSERT_NO_FATAL_FAILURE( |
| 3682 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3683 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3684 | } |
| 3685 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3686 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3687 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3688 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3689 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3690 | ADISPLAY_ID_DEFAULT); |
| 3691 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3692 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3693 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3694 | SECOND_DISPLAY_ID); |
| 3695 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3696 | |
| 3697 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3698 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3699 | |
| 3700 | // Set cursor position in window in default display and check that hover enter and move |
| 3701 | // events are generated. |
| 3702 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3703 | injectMotionEvent(mDispatcher, |
| 3704 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3705 | AINPUT_SOURCE_MOUSE) |
| 3706 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3707 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3708 | .x(300) |
| 3709 | .y(600)) |
| 3710 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3711 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3712 | |
| 3713 | // Remove all windows in secondary display and check that no event happens on window in |
| 3714 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3715 | mDispatcher->setInputWindows( |
| 3716 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3717 | windowDefaultDisplay->assertNoEvents(); |
| 3718 | |
| 3719 | // Move cursor position in window in default display and check that only hover move |
| 3720 | // event is generated and not hover enter event. |
| 3721 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3722 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3723 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3724 | injectMotionEvent(mDispatcher, |
| 3725 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3726 | AINPUT_SOURCE_MOUSE) |
| 3727 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3728 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3729 | .x(400) |
| 3730 | .y(700)) |
| 3731 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3732 | windowDefaultDisplay->consumeMotionEvent( |
| 3733 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3734 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3735 | windowDefaultDisplay->assertNoEvents(); |
| 3736 | } |
| 3737 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3738 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3740 | |
| 3741 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3742 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3743 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3744 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3745 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3746 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3747 | |
| 3748 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3749 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3750 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3751 | |
| 3752 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3753 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3754 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3755 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3756 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3757 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3758 | windowRight->assertNoEvents(); |
| 3759 | } |
| 3760 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3761 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3762 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3763 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3764 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3765 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3766 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3767 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3768 | setFocusedWindow(window); |
| 3769 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3770 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3771 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3772 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3773 | |
| 3774 | // Window should receive key down event. |
| 3775 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3776 | |
| 3777 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3778 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3779 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3780 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3781 | AKEY_EVENT_FLAG_CANCELED); |
| 3782 | } |
| 3783 | |
| 3784 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3785 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3786 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3787 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3788 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3789 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3790 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3791 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3792 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3793 | |
| 3794 | // Window should receive motion down event. |
| 3795 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3796 | |
| 3797 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 3798 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3799 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3800 | window->consumeMotionEvent( |
| 3801 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3802 | } |
| 3803 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 3804 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 3805 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3806 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3807 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3808 | |
| 3809 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3810 | |
| 3811 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3812 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 3813 | .build()); |
| 3814 | |
| 3815 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3816 | |
| 3817 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 3818 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 3819 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3820 | |
| 3821 | // After the device has been reset, a new hovering stream can be sent to the window |
| 3822 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3823 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 3824 | .build()); |
| 3825 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3826 | } |
| 3827 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3828 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 3829 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3830 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3831 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3832 | window->setFocusable(true); |
| 3833 | |
| 3834 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3835 | setFocusedWindow(window); |
| 3836 | |
| 3837 | window->consumeFocusEvent(true); |
| 3838 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3839 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3840 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3841 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3842 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3843 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3844 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3845 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3846 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3847 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3848 | } |
| 3849 | |
| 3850 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3851 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3852 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3853 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3854 | window->setFocusable(true); |
| 3855 | |
| 3856 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3857 | setFocusedWindow(window); |
| 3858 | |
| 3859 | window->consumeFocusEvent(true); |
| 3860 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3861 | mFakePolicy->setInterceptKeyTimeout(150ms); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3862 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 3863 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3864 | |
| 3865 | // Window should receive key event immediately when same key up. |
| 3866 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3867 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3868 | } |
| 3869 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3870 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3871 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3872 | * WATCH_OUTSIDE_TOUCH. |
| 3873 | * Both windows are owned by the same UID. |
| 3874 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3875 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3876 | */ |
| 3877 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3878 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3879 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3880 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3881 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3882 | |
| 3883 | sp<FakeWindowHandle> outsideWindow = |
| 3884 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3885 | ADISPLAY_ID_DEFAULT); |
| 3886 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3887 | outsideWindow->setWatchOutsideTouch(true); |
| 3888 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3889 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3890 | |
| 3891 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3892 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3893 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3894 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3895 | window->consumeMotionDown(); |
| 3896 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3897 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3898 | outsideWindow->consumeMotionEvent( |
| 3899 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3900 | } |
| 3901 | |
| 3902 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3903 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3904 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3905 | * ACTION_OUTSIDE event is sent per gesture. |
| 3906 | */ |
| 3907 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3908 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 3909 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3910 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3911 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3912 | window->setWatchOutsideTouch(true); |
| 3913 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3914 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3915 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3916 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3917 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3918 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3919 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3920 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3921 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3922 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3923 | |
| 3924 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3925 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3926 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3927 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3928 | window->assertNoEvents(); |
| 3929 | secondWindow->assertNoEvents(); |
| 3930 | |
| 3931 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3932 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3933 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3934 | ADISPLAY_ID_DEFAULT, |
| 3935 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3936 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3937 | window->consumeMotionEvent( |
| 3938 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3939 | secondWindow->consumeMotionDown(); |
| 3940 | thirdWindow->assertNoEvents(); |
| 3941 | |
| 3942 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3943 | // 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] | 3944 | mDispatcher->notifyMotion( |
| 3945 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3946 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3947 | window->assertNoEvents(); |
| 3948 | secondWindow->consumeMotionMove(); |
| 3949 | thirdWindow->consumeMotionDown(); |
| 3950 | } |
| 3951 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3952 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3953 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3954 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3955 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3956 | window->setFocusable(true); |
| 3957 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3958 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3959 | setFocusedWindow(window); |
| 3960 | |
| 3961 | window->consumeFocusEvent(true); |
| 3962 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3963 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3964 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3965 | mDispatcher->notifyKey(keyDown); |
| 3966 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3967 | |
| 3968 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3969 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3970 | |
| 3971 | // 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] | 3972 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3973 | |
| 3974 | window->consumeFocusEvent(false); |
| 3975 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3976 | mDispatcher->notifyKey(keyDown); |
| 3977 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3978 | window->assertNoEvents(); |
| 3979 | } |
| 3980 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3981 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3982 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3983 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3984 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3985 | // Ensure window is non-split and have some transform. |
| 3986 | window->setPreventSplitting(true); |
| 3987 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3988 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3989 | |
| 3990 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3991 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3992 | {50, 50})) |
| 3993 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3994 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3995 | |
| 3996 | const MotionEvent secondFingerDownEvent = |
| 3997 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3998 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3999 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4000 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4001 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4002 | .build(); |
| 4003 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4004 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 4005 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4006 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4007 | |
| 4008 | const MotionEvent* event = window->consumeMotion(); |
| 4009 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 4010 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 4011 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 4012 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 4013 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 4014 | } |
| 4015 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4016 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4017 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4018 | sp<FakeWindowHandle> window = |
| 4019 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4020 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4021 | sp<FakeWindowHandle> trustedOverlay = |
| 4022 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4023 | ADISPLAY_ID_DEFAULT); |
| 4024 | trustedOverlay->setSpy(true); |
| 4025 | trustedOverlay->setTrustedOverlay(true); |
| 4026 | |
| 4027 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}}); |
| 4028 | |
| 4029 | // Start a three-finger touchpad swipe |
| 4030 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4031 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4032 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4033 | .build()); |
| 4034 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4035 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4036 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4037 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4038 | .build()); |
| 4039 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4040 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4041 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4042 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4043 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4044 | .build()); |
| 4045 | |
| 4046 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4047 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4048 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4049 | |
| 4050 | // Move the swipe a bit |
| 4051 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4052 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4053 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4054 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4055 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4056 | .build()); |
| 4057 | |
| 4058 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4059 | |
| 4060 | // End the swipe |
| 4061 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4062 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4063 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4064 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4065 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4066 | .build()); |
| 4067 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4068 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4069 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4070 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4071 | .build()); |
| 4072 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4073 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4074 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4075 | .build()); |
| 4076 | |
| 4077 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4078 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4079 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4080 | |
| 4081 | window->assertNoEvents(); |
| 4082 | } |
| 4083 | |
| 4084 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4085 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4086 | sp<FakeWindowHandle> window = |
| 4087 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4088 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4090 | |
| 4091 | // Start a three-finger touchpad swipe |
| 4092 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4093 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4094 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4095 | .build()); |
| 4096 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4097 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4098 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4099 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4100 | .build()); |
| 4101 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4102 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4103 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4104 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4105 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4106 | .build()); |
| 4107 | |
| 4108 | // Move the swipe a bit |
| 4109 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4110 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4111 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4112 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4113 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4114 | .build()); |
| 4115 | |
| 4116 | // End the swipe |
| 4117 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4118 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4119 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4120 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4121 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4122 | .build()); |
| 4123 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4124 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4125 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4126 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4127 | .build()); |
| 4128 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4129 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4130 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4131 | .build()); |
| 4132 | |
| 4133 | window->assertNoEvents(); |
| 4134 | } |
| 4135 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4136 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4137 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4138 | * |
| 4139 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4140 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4141 | * space. |
| 4142 | */ |
| 4143 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4144 | public: |
| 4145 | void SetUp() override { |
| 4146 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4147 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4151 | gui::DisplayInfo info; |
| 4152 | info.displayId = displayId; |
| 4153 | info.transform = transform; |
| 4154 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4155 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4159 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4160 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4161 | } |
| 4162 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4163 | void removeAllWindowsAndDisplays() { |
| 4164 | mDisplayInfos.clear(); |
| 4165 | mWindowInfos.clear(); |
| 4166 | } |
| 4167 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4168 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4169 | // on the display. |
| 4170 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4171 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4172 | // respectively. |
| 4173 | ui::Transform displayTransform; |
| 4174 | displayTransform.set(2, 0, 0, 4); |
| 4175 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4176 | |
| 4177 | std::shared_ptr<FakeApplicationHandle> application = |
| 4178 | std::make_shared<FakeApplicationHandle>(); |
| 4179 | |
| 4180 | // Add two windows to the display. Their frames are represented in the display space. |
| 4181 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4182 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4183 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4184 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 4185 | addWindow(firstWindow); |
| 4186 | |
| 4187 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4188 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4189 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4190 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 4191 | addWindow(secondWindow); |
| 4192 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 4193 | } |
| 4194 | |
| 4195 | private: |
| 4196 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 4197 | std::vector<gui::WindowInfo> mWindowInfos; |
| 4198 | }; |
| 4199 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4200 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4201 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4202 | // 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] | 4203 | // selected so that if the hit test was performed with the point and the bounds being in |
| 4204 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4205 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4206 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4207 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4208 | |
| 4209 | firstWindow->consumeMotionDown(); |
| 4210 | secondWindow->assertNoEvents(); |
| 4211 | } |
| 4212 | |
| 4213 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 4214 | // the event should be treated as being in the logical display space. |
| 4215 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 4216 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4217 | // Send down to the first window. The point is represented in the logical display space. The |
| 4218 | // point is selected so that if the hit test was done in logical display space, then it would |
| 4219 | // end up in the incorrect window. |
| 4220 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4221 | PointF{75 * 2, 55 * 4}); |
| 4222 | |
| 4223 | firstWindow->consumeMotionDown(); |
| 4224 | secondWindow->assertNoEvents(); |
| 4225 | } |
| 4226 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4227 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 4228 | // event should be treated as being in the logical display space. |
| 4229 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 4230 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4231 | |
| 4232 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4233 | ui::Transform injectedEventTransform; |
| 4234 | injectedEventTransform.set(matrix); |
| 4235 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 4236 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 4237 | |
| 4238 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4239 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4240 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4241 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4242 | .x(untransformedPoint.x) |
| 4243 | .y(untransformedPoint.y)) |
| 4244 | .build(); |
| 4245 | event.transform(matrix); |
| 4246 | |
| 4247 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 4248 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4249 | |
| 4250 | firstWindow->consumeMotionDown(); |
| 4251 | secondWindow->assertNoEvents(); |
| 4252 | } |
| 4253 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4254 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4255 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4256 | |
| 4257 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4258 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4259 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4260 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4261 | |
| 4262 | firstWindow->assertNoEvents(); |
| 4263 | const MotionEvent* event = secondWindow->consumeMotion(); |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 4264 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4265 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 4266 | |
| 4267 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 4268 | EXPECT_EQ(300, event->getRawX(0)); |
| 4269 | EXPECT_EQ(880, event->getRawY(0)); |
| 4270 | |
| 4271 | // Ensure that the x and y values are in the window's coordinate space. |
| 4272 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4273 | // the logical display space. This will be the origin of the window space. |
| 4274 | EXPECT_EQ(100, event->getX(0)); |
| 4275 | EXPECT_EQ(80, event->getY(0)); |
| 4276 | } |
| 4277 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4278 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4279 | class InputDispatcherDisplayOrientationFixture |
| 4280 | : public InputDispatcherDisplayProjectionTest, |
| 4281 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 4282 | |
| 4283 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 4284 | // in different locations on the display, specifically points close to the four corners of a |
| 4285 | // window. |
| 4286 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 4287 | constexpr static int32_t displayWidth = 400; |
| 4288 | constexpr static int32_t displayHeight = 800; |
| 4289 | |
| 4290 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4291 | |
| 4292 | const auto rotation = GetParam(); |
| 4293 | |
| 4294 | // Set up the display with the specified rotation. |
| 4295 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4296 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4297 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4298 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4299 | logicalDisplayWidth, logicalDisplayHeight); |
| 4300 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4301 | |
| 4302 | // Create a window with its bounds determined in the logical display. |
| 4303 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 4304 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4305 | sp<FakeWindowHandle> window = |
| 4306 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4307 | window->setFrame(frameInDisplay, displayTransform); |
| 4308 | addWindow(window); |
| 4309 | |
| 4310 | // The following points in logical display space should be inside the window. |
| 4311 | static const std::array<vec2, 4> insidePoints{ |
| 4312 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4313 | for (const auto pointInsideWindow : insidePoints) { |
| 4314 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4315 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4316 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4317 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4318 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4319 | window->consumeMotionDown(); |
| 4320 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4321 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4322 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4323 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4324 | window->consumeMotionUp(); |
| 4325 | } |
| 4326 | |
| 4327 | // The following points in logical display space should be outside the window. |
| 4328 | static const std::array<vec2, 5> outsidePoints{ |
| 4329 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4330 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4331 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4332 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4333 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4334 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4335 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4336 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4337 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4338 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4339 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4340 | } |
| 4341 | window->assertNoEvents(); |
| 4342 | } |
| 4343 | |
| 4344 | // Run the precision tests for all rotations. |
| 4345 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4346 | InputDispatcherDisplayOrientationFixture, |
| 4347 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4348 | ui::ROTATION_270), |
| 4349 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4350 | return ftl::enum_string(testParamInfo.param); |
| 4351 | }); |
| 4352 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4353 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4354 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4355 | |
| 4356 | class TransferTouchFixture : public InputDispatcherTest, |
| 4357 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4358 | |
| 4359 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4360 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4361 | |
| 4362 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4363 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4364 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4365 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4366 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4367 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4368 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4369 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4370 | sp<FakeWindowHandle> wallpaper = |
| 4371 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 4372 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4373 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4374 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4375 | |
| 4376 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4377 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4378 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4379 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4380 | // Only the first window should get the down event |
| 4381 | firstWindow->consumeMotionDown(); |
| 4382 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4383 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4384 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4385 | // Transfer touch to the second window |
| 4386 | TransferFunction f = GetParam(); |
| 4387 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4388 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4389 | // The first window gets cancel and the second gets down |
| 4390 | firstWindow->consumeMotionCancel(); |
| 4391 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4392 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4393 | |
| 4394 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4395 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4396 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4397 | // The first window gets no events and the second gets up |
| 4398 | firstWindow->assertNoEvents(); |
| 4399 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4400 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4401 | } |
| 4402 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4403 | /** |
| 4404 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 4405 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 4406 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 4407 | * natural to the user. |
| 4408 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 4409 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 4410 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 4411 | * the first window to the second. |
| 4412 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 4413 | * the other API, as well. |
| 4414 | */ |
| 4415 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 4416 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4417 | |
| 4418 | // Create a couple of windows + a spy window |
| 4419 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4420 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4421 | spyWindow->setTrustedOverlay(true); |
| 4422 | spyWindow->setSpy(true); |
| 4423 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4424 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4425 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4426 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4427 | |
| 4428 | // Add the windows to the dispatcher |
| 4429 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 4430 | |
| 4431 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4432 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4433 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4434 | // Only the first window and spy should get the down event |
| 4435 | spyWindow->consumeMotionDown(); |
| 4436 | firstWindow->consumeMotionDown(); |
| 4437 | |
| 4438 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 4439 | // if f === 'transferTouch'. |
| 4440 | TransferFunction f = GetParam(); |
| 4441 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4442 | ASSERT_TRUE(success); |
| 4443 | // The first window gets cancel and the second gets down |
| 4444 | firstWindow->consumeMotionCancel(); |
| 4445 | secondWindow->consumeMotionDown(); |
| 4446 | |
| 4447 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4448 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4449 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4450 | // The first window gets no events and the second+spy get up |
| 4451 | firstWindow->assertNoEvents(); |
| 4452 | spyWindow->consumeMotionUp(); |
| 4453 | secondWindow->consumeMotionUp(); |
| 4454 | } |
| 4455 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4456 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4457 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4458 | |
| 4459 | PointF touchPoint = {10, 10}; |
| 4460 | |
| 4461 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4462 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4463 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4464 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4465 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4466 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4467 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4468 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4469 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4470 | |
| 4471 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4472 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4473 | |
| 4474 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4475 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4476 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4477 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4478 | // Only the first window should get the down event |
| 4479 | firstWindow->consumeMotionDown(); |
| 4480 | secondWindow->assertNoEvents(); |
| 4481 | |
| 4482 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4483 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4484 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4485 | // Only the first window should get the pointer down event |
| 4486 | firstWindow->consumeMotionPointerDown(1); |
| 4487 | secondWindow->assertNoEvents(); |
| 4488 | |
| 4489 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4490 | TransferFunction f = GetParam(); |
| 4491 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4492 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4493 | // The first window gets cancel and the second gets down and pointer down |
| 4494 | firstWindow->consumeMotionCancel(); |
| 4495 | secondWindow->consumeMotionDown(); |
| 4496 | secondWindow->consumeMotionPointerDown(1); |
| 4497 | |
| 4498 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4499 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4500 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4501 | // The first window gets nothing and the second gets pointer up |
| 4502 | firstWindow->assertNoEvents(); |
| 4503 | secondWindow->consumeMotionPointerUp(1); |
| 4504 | |
| 4505 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4506 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4507 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4508 | // The first window gets nothing and the second gets up |
| 4509 | firstWindow->assertNoEvents(); |
| 4510 | secondWindow->consumeMotionUp(); |
| 4511 | } |
| 4512 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4513 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 4514 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4515 | |
| 4516 | // Create a couple of windows |
| 4517 | sp<FakeWindowHandle> firstWindow = |
| 4518 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4519 | ADISPLAY_ID_DEFAULT); |
| 4520 | firstWindow->setDupTouchToWallpaper(true); |
| 4521 | sp<FakeWindowHandle> secondWindow = |
| 4522 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4523 | ADISPLAY_ID_DEFAULT); |
| 4524 | secondWindow->setDupTouchToWallpaper(true); |
| 4525 | |
| 4526 | sp<FakeWindowHandle> wallpaper1 = |
| 4527 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 4528 | wallpaper1->setIsWallpaper(true); |
| 4529 | |
| 4530 | sp<FakeWindowHandle> wallpaper2 = |
| 4531 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 4532 | wallpaper2->setIsWallpaper(true); |
| 4533 | // Add the windows to the dispatcher |
| 4534 | mDispatcher->setInputWindows( |
| 4535 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 4536 | |
| 4537 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4538 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4539 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4540 | |
| 4541 | // Only the first window should get the down event |
| 4542 | firstWindow->consumeMotionDown(); |
| 4543 | secondWindow->assertNoEvents(); |
| 4544 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4545 | wallpaper2->assertNoEvents(); |
| 4546 | |
| 4547 | // Transfer touch focus to the second window |
| 4548 | TransferFunction f = GetParam(); |
| 4549 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4550 | ASSERT_TRUE(success); |
| 4551 | |
| 4552 | // The first window gets cancel and the second gets down |
| 4553 | firstWindow->consumeMotionCancel(); |
| 4554 | secondWindow->consumeMotionDown(); |
| 4555 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4556 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4557 | |
| 4558 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4559 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4560 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4561 | // The first window gets no events and the second gets up |
| 4562 | firstWindow->assertNoEvents(); |
| 4563 | secondWindow->consumeMotionUp(); |
| 4564 | wallpaper1->assertNoEvents(); |
| 4565 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4566 | } |
| 4567 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4568 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 4569 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 4570 | // for the case where there are multiple pointers split across several windows. |
| 4571 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 4572 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4573 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4574 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4575 | return dispatcher->transferTouch(destChannelToken, |
| 4576 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4577 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4578 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4579 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4580 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4581 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4582 | })); |
| 4583 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4584 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4585 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4586 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4587 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4588 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4589 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4590 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4591 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4592 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4593 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4594 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4595 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4596 | |
| 4597 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4598 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4599 | |
| 4600 | PointF pointInFirst = {300, 200}; |
| 4601 | PointF pointInSecond = {300, 600}; |
| 4602 | |
| 4603 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4604 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4605 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4606 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4607 | // Only the first window should get the down event |
| 4608 | firstWindow->consumeMotionDown(); |
| 4609 | secondWindow->assertNoEvents(); |
| 4610 | |
| 4611 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4612 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4613 | ADISPLAY_ID_DEFAULT, |
| 4614 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4615 | // The first window gets a move and the second a down |
| 4616 | firstWindow->consumeMotionMove(); |
| 4617 | secondWindow->consumeMotionDown(); |
| 4618 | |
| 4619 | // Transfer touch focus to the second window |
| 4620 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4621 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 4622 | firstWindow->consumeMotionCancel(); |
| 4623 | secondWindow->consumeMotionPointerDown(1); |
| 4624 | |
| 4625 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4626 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4627 | ADISPLAY_ID_DEFAULT, |
| 4628 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4629 | // The first window gets nothing and the second gets pointer up |
| 4630 | firstWindow->assertNoEvents(); |
| 4631 | secondWindow->consumeMotionPointerUp(1); |
| 4632 | |
| 4633 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4634 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4635 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4636 | // The first window gets nothing and the second gets up |
| 4637 | firstWindow->assertNoEvents(); |
| 4638 | secondWindow->consumeMotionUp(); |
| 4639 | } |
| 4640 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4641 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 4642 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 4643 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 4644 | // window should get nothing. |
| 4645 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 4646 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4647 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4648 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4649 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4650 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4651 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4652 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4653 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4654 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4655 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4656 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4657 | |
| 4658 | // Add the windows to the dispatcher |
| 4659 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4660 | |
| 4661 | PointF pointInFirst = {300, 200}; |
| 4662 | PointF pointInSecond = {300, 600}; |
| 4663 | |
| 4664 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4665 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4666 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4667 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4668 | // Only the first window should get the down event |
| 4669 | firstWindow->consumeMotionDown(); |
| 4670 | secondWindow->assertNoEvents(); |
| 4671 | |
| 4672 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4673 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4674 | ADISPLAY_ID_DEFAULT, |
| 4675 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4676 | // The first window gets a move and the second a down |
| 4677 | firstWindow->consumeMotionMove(); |
| 4678 | secondWindow->consumeMotionDown(); |
| 4679 | |
| 4680 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4681 | const bool transferred = |
| 4682 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4683 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 4684 | ASSERT_FALSE(transferred); |
| 4685 | firstWindow->assertNoEvents(); |
| 4686 | secondWindow->assertNoEvents(); |
| 4687 | |
| 4688 | // The rest of the dispatch should proceed as normal |
| 4689 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4690 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4691 | ADISPLAY_ID_DEFAULT, |
| 4692 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4693 | // The first window gets MOVE and the second gets pointer up |
| 4694 | firstWindow->consumeMotionMove(); |
| 4695 | secondWindow->consumeMotionUp(); |
| 4696 | |
| 4697 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4698 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4699 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4700 | // The first window gets nothing and the second gets up |
| 4701 | firstWindow->consumeMotionUp(); |
| 4702 | secondWindow->assertNoEvents(); |
| 4703 | } |
| 4704 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4705 | // This case will create two windows and one mirrored window on the default display and mirror |
| 4706 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 4707 | // the windows info of second display before default display. |
| 4708 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 4709 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4710 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4711 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4712 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4713 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4714 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4715 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4716 | |
| 4717 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4718 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4719 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4720 | |
| 4721 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4722 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4723 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4724 | |
| 4725 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4726 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4727 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4728 | |
| 4729 | // Update window info, let it find window handle of second display first. |
| 4730 | mDispatcher->setInputWindows( |
| 4731 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4732 | {ADISPLAY_ID_DEFAULT, |
| 4733 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4734 | |
| 4735 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4736 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4737 | {50, 50})) |
| 4738 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4739 | |
| 4740 | // Window should receive motion event. |
| 4741 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4742 | |
| 4743 | // Transfer touch focus |
| 4744 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 4745 | secondWindowInPrimary->getToken())); |
| 4746 | // The first window gets cancel. |
| 4747 | firstWindowInPrimary->consumeMotionCancel(); |
| 4748 | secondWindowInPrimary->consumeMotionDown(); |
| 4749 | |
| 4750 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4751 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4752 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4753 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4754 | firstWindowInPrimary->assertNoEvents(); |
| 4755 | secondWindowInPrimary->consumeMotionMove(); |
| 4756 | |
| 4757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4758 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4759 | {150, 50})) |
| 4760 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4761 | firstWindowInPrimary->assertNoEvents(); |
| 4762 | secondWindowInPrimary->consumeMotionUp(); |
| 4763 | } |
| 4764 | |
| 4765 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 4766 | // 'transferTouch' api. |
| 4767 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 4768 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4769 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4770 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4771 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4772 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4773 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4774 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4775 | |
| 4776 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4777 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4778 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4779 | |
| 4780 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4781 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4782 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4783 | |
| 4784 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4785 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4786 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4787 | |
| 4788 | // Update window info, let it find window handle of second display first. |
| 4789 | mDispatcher->setInputWindows( |
| 4790 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4791 | {ADISPLAY_ID_DEFAULT, |
| 4792 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4793 | |
| 4794 | // Touch on second display. |
| 4795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4796 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 4797 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4798 | |
| 4799 | // Window should receive motion event. |
| 4800 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4801 | |
| 4802 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4803 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4804 | |
| 4805 | // The first window gets cancel. |
| 4806 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4807 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4808 | |
| 4809 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4810 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4811 | SECOND_DISPLAY_ID, {150, 50})) |
| 4812 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4813 | firstWindowInPrimary->assertNoEvents(); |
| 4814 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 4815 | |
| 4816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4817 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 4818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4819 | firstWindowInPrimary->assertNoEvents(); |
| 4820 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 4821 | } |
| 4822 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4823 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4824 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4825 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4826 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4827 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4828 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4829 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4830 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4831 | |
| 4832 | window->consumeFocusEvent(true); |
| 4833 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4834 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4835 | |
| 4836 | // Window should receive key down event. |
| 4837 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4838 | |
| 4839 | // Should have poked user activity |
| 4840 | mFakePolicy->assertUserActivityPoked(); |
| 4841 | } |
| 4842 | |
| 4843 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 4844 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4845 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4846 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4847 | |
| 4848 | window->setDisableUserActivity(true); |
| 4849 | window->setFocusable(true); |
| 4850 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4851 | setFocusedWindow(window); |
| 4852 | |
| 4853 | window->consumeFocusEvent(true); |
| 4854 | |
| 4855 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4856 | |
| 4857 | // Window should receive key down event. |
| 4858 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4859 | |
| 4860 | // Should have poked user activity |
| 4861 | mFakePolicy->assertUserActivityNotPoked(); |
| 4862 | } |
| 4863 | |
| 4864 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 4865 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4866 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4867 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4868 | |
| 4869 | window->setFocusable(true); |
| 4870 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4871 | setFocusedWindow(window); |
| 4872 | |
| 4873 | window->consumeFocusEvent(true); |
| 4874 | |
| 4875 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4876 | mDispatcher->waitForIdle(); |
| 4877 | |
| 4878 | // System key is not passed down |
| 4879 | window->assertNoEvents(); |
| 4880 | |
| 4881 | // Should have poked user activity |
| 4882 | mFakePolicy->assertUserActivityPoked(); |
| 4883 | } |
| 4884 | |
| 4885 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 4886 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4887 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4888 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4889 | |
| 4890 | window->setFocusable(true); |
| 4891 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4892 | setFocusedWindow(window); |
| 4893 | |
| 4894 | window->consumeFocusEvent(true); |
| 4895 | |
| 4896 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4897 | mDispatcher->waitForIdle(); |
| 4898 | |
| 4899 | // System key is not passed down |
| 4900 | window->assertNoEvents(); |
| 4901 | |
| 4902 | // Should have poked user activity |
| 4903 | mFakePolicy->assertUserActivityPoked(); |
| 4904 | } |
| 4905 | |
| 4906 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 4907 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4908 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4909 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4910 | |
| 4911 | window->setDisableUserActivity(true); |
| 4912 | window->setFocusable(true); |
| 4913 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4914 | setFocusedWindow(window); |
| 4915 | |
| 4916 | window->consumeFocusEvent(true); |
| 4917 | |
| 4918 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4919 | mDispatcher->waitForIdle(); |
| 4920 | |
| 4921 | // System key is not passed down |
| 4922 | window->assertNoEvents(); |
| 4923 | |
| 4924 | // Should have poked user activity |
| 4925 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4926 | } |
| 4927 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame^] | 4928 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 4929 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4930 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4931 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4932 | |
| 4933 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4934 | |
| 4935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4936 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4937 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 4938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4939 | |
| 4940 | window->consumeMotionEvent( |
| 4941 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4942 | |
| 4943 | // Should have poked user activity |
| 4944 | mFakePolicy->assertUserActivityPoked(); |
| 4945 | } |
| 4946 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4947 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4948 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4949 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4950 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4951 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4952 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4953 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4954 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4955 | mDispatcher->waitForIdle(); |
| 4956 | |
| 4957 | window->assertNoEvents(); |
| 4958 | } |
| 4959 | |
| 4960 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4961 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4962 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4963 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4964 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4965 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4966 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4967 | |
| 4968 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4969 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4970 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4971 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4972 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4973 | |
| 4974 | // Window should receive only the motion event |
| 4975 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4976 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4977 | } |
| 4978 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4979 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4980 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4981 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4982 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4983 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4984 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4985 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4986 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4987 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4988 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4989 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4990 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4991 | |
| 4992 | // Add the windows to the dispatcher |
| 4993 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4994 | |
| 4995 | PointF pointInFirst = {300, 200}; |
| 4996 | PointF pointInSecond = {300, 600}; |
| 4997 | |
| 4998 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4999 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5000 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5001 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5002 | // Only the first window should get the down event |
| 5003 | firstWindow->consumeMotionDown(); |
| 5004 | secondWindow->assertNoEvents(); |
| 5005 | |
| 5006 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5007 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5008 | ADISPLAY_ID_DEFAULT, |
| 5009 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5010 | // The first window gets a move and the second a down |
| 5011 | firstWindow->consumeMotionMove(); |
| 5012 | secondWindow->consumeMotionDown(); |
| 5013 | |
| 5014 | // Send pointer cancel to the second window |
| 5015 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5016 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5017 | {pointInFirst, pointInSecond}); |
| 5018 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5019 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5020 | // The first window gets move and the second gets cancel. |
| 5021 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5022 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5023 | |
| 5024 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5025 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5026 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5027 | // The first window gets up and the second gets nothing. |
| 5028 | firstWindow->consumeMotionUp(); |
| 5029 | secondWindow->assertNoEvents(); |
| 5030 | } |
| 5031 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5032 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 5033 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5034 | |
| 5035 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5036 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5037 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5038 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 5039 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 5040 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 5041 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5042 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5043 | window->assertNoEvents(); |
| 5044 | mDispatcher->waitForIdle(); |
| 5045 | } |
| 5046 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5047 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5048 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5049 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5050 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5051 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 5052 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 5053 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5054 | } |
| 5055 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5056 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 5057 | |
| 5058 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5059 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 5060 | expectedFlags); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5061 | } |
| 5062 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 5063 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 5064 | |
| 5065 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 5066 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5067 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5068 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5069 | expectedDisplayId, expectedFlags); |
| 5070 | } |
| 5071 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5072 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5073 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5074 | expectedDisplayId, expectedFlags); |
| 5075 | } |
| 5076 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5077 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5078 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5079 | expectedDisplayId, expectedFlags); |
| 5080 | } |
| 5081 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5082 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5083 | mInputReceiver->consumeMotionEvent( |
| 5084 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 5085 | WithDisplayId(expectedDisplayId), |
| 5086 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5087 | } |
| 5088 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 5089 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 5090 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 5091 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5092 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5093 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 5094 | } |
| 5095 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5096 | MotionEvent* consumeMotion() { |
| 5097 | InputEvent* event = mInputReceiver->consume(); |
| 5098 | if (!event) { |
| 5099 | ADD_FAILURE() << "No event was produced"; |
| 5100 | return nullptr; |
| 5101 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5102 | if (event->getType() != InputEventType::MOTION) { |
| 5103 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5104 | return nullptr; |
| 5105 | } |
| 5106 | return static_cast<MotionEvent*>(event); |
| 5107 | } |
| 5108 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5109 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 5110 | |
| 5111 | private: |
| 5112 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5113 | }; |
| 5114 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5115 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 5116 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5117 | /** |
| 5118 | * Two entities that receive touch: A window, and a global monitor. |
| 5119 | * The touch goes to the window, and then the window disappears. |
| 5120 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 5121 | * for the monitor, as well. |
| 5122 | * 1. foregroundWindow |
| 5123 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 5124 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5125 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5127 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5128 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5129 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5130 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5131 | |
| 5132 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5133 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5134 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5135 | {100, 200})) |
| 5136 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5137 | |
| 5138 | // Both the foreground window and the global monitor should receive the touch down |
| 5139 | window->consumeMotionDown(); |
| 5140 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5141 | |
| 5142 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5143 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5144 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5145 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5146 | |
| 5147 | window->consumeMotionMove(); |
| 5148 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5149 | |
| 5150 | // Now the foreground window goes away |
| 5151 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5152 | window->consumeMotionCancel(); |
| 5153 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 5154 | |
| 5155 | // If more events come in, there will be no more foreground window to send them to. This will |
| 5156 | // cause a cancel for the monitor, as well. |
| 5157 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5158 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5159 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 5160 | << "Injection should fail because the window was removed"; |
| 5161 | window->assertNoEvents(); |
| 5162 | // Global monitor now gets the cancel |
| 5163 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5164 | } |
| 5165 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5166 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5167 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5168 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5169 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5170 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5171 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5172 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5173 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5175 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5176 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5177 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5178 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5181 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 5182 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5183 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5184 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5185 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5186 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5187 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5188 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5189 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5190 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5191 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5192 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5193 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5194 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5195 | // Pilfer pointers from the monitor. |
| 5196 | // This should not do anything and the window should continue to receive events. |
| 5197 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5198 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5199 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5200 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5201 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5202 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5203 | |
| 5204 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5205 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5208 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5209 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5210 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5211 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5212 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5213 | window->setWindowOffset(20, 40); |
| 5214 | window->setWindowTransform(0, 1, -1, 0); |
| 5215 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5216 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5217 | |
| 5218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5219 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5220 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5221 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5222 | MotionEvent* event = monitor.consumeMotion(); |
| 5223 | // Even though window has transform, gesture monitor must not. |
| 5224 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 5225 | } |
| 5226 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5227 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5228 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5229 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5230 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5231 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5232 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5233 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 5234 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5237 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5238 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5239 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5240 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5241 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5242 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5243 | |
| 5244 | NotifyMotionArgs motionArgs = |
| 5245 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5246 | ADISPLAY_ID_DEFAULT); |
| 5247 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5248 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5249 | // Window should receive motion down event. |
| 5250 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5251 | |
| 5252 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5253 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5254 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5255 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5256 | motionArgs.pointerCoords[0].getX() - 10); |
| 5257 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5258 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5259 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5260 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5261 | } |
| 5262 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5263 | /** |
| 5264 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 5265 | * the device default right away. In the test scenario, we check both the default value, |
| 5266 | * and the action of enabling / disabling. |
| 5267 | */ |
| 5268 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5269 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5270 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5271 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5272 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5273 | |
| 5274 | // Set focused application. |
| 5275 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5276 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5277 | |
| 5278 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5279 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5280 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5281 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5282 | |
| 5283 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5284 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5285 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5286 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5287 | |
| 5288 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5289 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5290 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5291 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5292 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5293 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5294 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5295 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5296 | |
| 5297 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5298 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5299 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5300 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5301 | |
| 5302 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5303 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5304 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5305 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5306 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5307 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5308 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5309 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5310 | |
| 5311 | window->assertNoEvents(); |
| 5312 | } |
| 5313 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5314 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5315 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5316 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5317 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5318 | |
| 5319 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5320 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5321 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5322 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5323 | setFocusedWindow(window); |
| 5324 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5325 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5326 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5327 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5328 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5329 | |
| 5330 | InputEvent* event = window->consume(); |
| 5331 | ASSERT_NE(event, nullptr); |
| 5332 | |
| 5333 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5334 | ASSERT_NE(verified, nullptr); |
| 5335 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 5336 | |
| 5337 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 5338 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 5339 | ASSERT_EQ(keyArgs.source, verified->source); |
| 5340 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 5341 | |
| 5342 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 5343 | |
| 5344 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5345 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5346 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5347 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 5348 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 5349 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 5350 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 5351 | } |
| 5352 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5353 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5354 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5355 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5356 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5357 | |
| 5358 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5359 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5360 | ui::Transform transform; |
| 5361 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5362 | |
| 5363 | gui::DisplayInfo displayInfo; |
| 5364 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5365 | displayInfo.transform = transform; |
| 5366 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5367 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5368 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5369 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5370 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5371 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5372 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5373 | |
| 5374 | InputEvent* event = window->consume(); |
| 5375 | ASSERT_NE(event, nullptr); |
| 5376 | |
| 5377 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5378 | ASSERT_NE(verified, nullptr); |
| 5379 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 5380 | |
| 5381 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 5382 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 5383 | EXPECT_EQ(motionArgs.source, verified->source); |
| 5384 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 5385 | |
| 5386 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 5387 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5388 | const vec2 rawXY = |
| 5389 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 5390 | motionArgs.pointerCoords[0].getXYValue()); |
| 5391 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 5392 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5393 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5394 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5395 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5396 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 5397 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 5398 | } |
| 5399 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5400 | /** |
| 5401 | * Ensure that separate calls to sign the same data are generating the same key. |
| 5402 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 5403 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 5404 | * tests. |
| 5405 | */ |
| 5406 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 5407 | KeyEvent event = getTestKeyEvent(); |
| 5408 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5409 | |
| 5410 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 5411 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5412 | ASSERT_EQ(hmac1, hmac2); |
| 5413 | } |
| 5414 | |
| 5415 | /** |
| 5416 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 5417 | */ |
| 5418 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 5419 | KeyEvent event = getTestKeyEvent(); |
| 5420 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5421 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 5422 | |
| 5423 | verifiedEvent.deviceId += 1; |
| 5424 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5425 | |
| 5426 | verifiedEvent.source += 1; |
| 5427 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5428 | |
| 5429 | verifiedEvent.eventTimeNanos += 1; |
| 5430 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5431 | |
| 5432 | verifiedEvent.displayId += 1; |
| 5433 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5434 | |
| 5435 | verifiedEvent.action += 1; |
| 5436 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5437 | |
| 5438 | verifiedEvent.downTimeNanos += 1; |
| 5439 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5440 | |
| 5441 | verifiedEvent.flags += 1; |
| 5442 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5443 | |
| 5444 | verifiedEvent.keyCode += 1; |
| 5445 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5446 | |
| 5447 | verifiedEvent.scanCode += 1; |
| 5448 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5449 | |
| 5450 | verifiedEvent.metaState += 1; |
| 5451 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5452 | |
| 5453 | verifiedEvent.repeatCount += 1; |
| 5454 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5455 | } |
| 5456 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5457 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 5458 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5459 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5460 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5461 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5462 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5463 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5464 | |
| 5465 | // Top window is also focusable but is not granted focus. |
| 5466 | windowTop->setFocusable(true); |
| 5467 | windowSecond->setFocusable(true); |
| 5468 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5469 | setFocusedWindow(windowSecond); |
| 5470 | |
| 5471 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5473 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5474 | |
| 5475 | // Focused window should receive event. |
| 5476 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5477 | windowTop->assertNoEvents(); |
| 5478 | } |
| 5479 | |
| 5480 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5481 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5482 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5483 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5484 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5485 | |
| 5486 | window->setFocusable(true); |
| 5487 | // Release channel for window is no longer valid. |
| 5488 | window->releaseChannel(); |
| 5489 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5490 | setFocusedWindow(window); |
| 5491 | |
| 5492 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5493 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5494 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5495 | |
| 5496 | // window channel is invalid, so it should not receive any input event. |
| 5497 | window->assertNoEvents(); |
| 5498 | } |
| 5499 | |
| 5500 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5501 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5502 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5503 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5504 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5505 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5506 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5507 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5508 | setFocusedWindow(window); |
| 5509 | |
| 5510 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5511 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5512 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5513 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5514 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5515 | window->assertNoEvents(); |
| 5516 | } |
| 5517 | |
| 5518 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5519 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5520 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5521 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5522 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5523 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5524 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5525 | |
| 5526 | windowTop->setFocusable(true); |
| 5527 | windowSecond->setFocusable(true); |
| 5528 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5529 | setFocusedWindow(windowTop); |
| 5530 | windowTop->consumeFocusEvent(true); |
| 5531 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5532 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
| 5533 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5534 | windowSecond->consumeFocusEvent(true); |
| 5535 | windowTop->consumeFocusEvent(false); |
| 5536 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5538 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5539 | |
| 5540 | // Focused window should receive event. |
| 5541 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5542 | } |
| 5543 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5544 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5545 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5546 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5547 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5548 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5549 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5550 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5551 | |
| 5552 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5553 | windowSecond->setFocusable(false); |
| 5554 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5555 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5556 | setFocusedWindow(windowTop); |
| 5557 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5558 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5560 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5561 | |
| 5562 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5563 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5564 | windowSecond->assertNoEvents(); |
| 5565 | } |
| 5566 | |
| 5567 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5568 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5569 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5570 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5571 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5572 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5573 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5574 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5575 | |
| 5576 | window->setFocusable(true); |
| 5577 | previousFocusedWindow->setFocusable(true); |
| 5578 | window->setVisible(false); |
| 5579 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5580 | setFocusedWindow(previousFocusedWindow); |
| 5581 | previousFocusedWindow->consumeFocusEvent(true); |
| 5582 | |
| 5583 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5584 | setFocusedWindow(window); |
| 5585 | previousFocusedWindow->consumeFocusEvent(false); |
| 5586 | |
| 5587 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5588 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5589 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5590 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5591 | |
| 5592 | // Window does not get focus event or key down. |
| 5593 | window->assertNoEvents(); |
| 5594 | |
| 5595 | // Window becomes visible. |
| 5596 | window->setVisible(true); |
| 5597 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5598 | |
| 5599 | // Window receives focus event. |
| 5600 | window->consumeFocusEvent(true); |
| 5601 | // Focused window receives key down. |
| 5602 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5603 | } |
| 5604 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5605 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5606 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5607 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5608 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5609 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5610 | |
| 5611 | // window is granted focus. |
| 5612 | window->setFocusable(true); |
| 5613 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5614 | setFocusedWindow(window); |
| 5615 | window->consumeFocusEvent(true); |
| 5616 | |
| 5617 | // When a display is removed window loses focus. |
| 5618 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5619 | window->consumeFocusEvent(false); |
| 5620 | } |
| 5621 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5622 | /** |
| 5623 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5624 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5625 | * of the 'slipperyEnterWindow'. |
| 5626 | * |
| 5627 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5628 | * a way so that the touched location is no longer covered by the top window. |
| 5629 | * |
| 5630 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5631 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5632 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5633 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5634 | * with ACTION_DOWN). |
| 5635 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5636 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5637 | * |
| 5638 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5639 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5640 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5641 | * |
| 5642 | * In this test, we ensure that the event received by the bottom window has |
| 5643 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5644 | */ |
| 5645 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5646 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 5647 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5648 | |
| 5649 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5650 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5651 | |
| 5652 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5653 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5654 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5655 | // Make sure this one overlaps the bottom window |
| 5656 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5657 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5658 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5659 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5660 | |
| 5661 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5662 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5663 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5664 | |
| 5665 | mDispatcher->setInputWindows( |
| 5666 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5667 | |
| 5668 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5669 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5670 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5671 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5672 | slipperyExitWindow->consumeMotionDown(); |
| 5673 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5674 | mDispatcher->setInputWindows( |
| 5675 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5676 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5677 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 5678 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5679 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5680 | |
| 5681 | slipperyExitWindow->consumeMotionCancel(); |
| 5682 | |
| 5683 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5684 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5685 | } |
| 5686 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 5687 | /** |
| 5688 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 5689 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 5690 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 5691 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 5692 | * reproduces a crash. |
| 5693 | */ |
| 5694 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 5695 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5696 | |
| 5697 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 5698 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5699 | leftSlipperyWindow->setSlippery(true); |
| 5700 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5701 | |
| 5702 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 5703 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5704 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 5705 | rightDropTouchesWindow->setDropInput(true); |
| 5706 | |
| 5707 | mDispatcher->setInputWindows( |
| 5708 | {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}}); |
| 5709 | |
| 5710 | // Start touch in the left window |
| 5711 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5712 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5713 | .build()); |
| 5714 | leftSlipperyWindow->consumeMotionDown(); |
| 5715 | |
| 5716 | // And move it into the right window |
| 5717 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5718 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5719 | .build()); |
| 5720 | |
| 5721 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 5722 | // The left window continues to receive the gesture. |
| 5723 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5724 | rightDropTouchesWindow->assertNoEvents(); |
| 5725 | } |
| 5726 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5727 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5728 | protected: |
| 5729 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5730 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5731 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5732 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5733 | sp<FakeWindowHandle> mWindow; |
| 5734 | |
| 5735 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5736 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5737 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5738 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 5739 | mDispatcher->requestRefreshConfiguration(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5740 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 5741 | ASSERT_EQ(OK, mDispatcher->start()); |
| 5742 | |
| 5743 | setUpWindow(); |
| 5744 | } |
| 5745 | |
| 5746 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5747 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5748 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5749 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5750 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5751 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5752 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5753 | mWindow->consumeFocusEvent(true); |
| 5754 | } |
| 5755 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5756 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5757 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5758 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5759 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5760 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5761 | |
| 5762 | // Window should receive key down event. |
| 5763 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5764 | } |
| 5765 | |
| 5766 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 5767 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 5768 | InputEvent* repeatEvent = mWindow->consume(); |
| 5769 | ASSERT_NE(nullptr, repeatEvent); |
| 5770 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5771 | ASSERT_EQ(InputEventType::KEY, repeatEvent->getType()); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5772 | |
| 5773 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 5774 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 5775 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 5776 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 5777 | } |
| 5778 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5779 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5780 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5781 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5782 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5783 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5784 | |
| 5785 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5786 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5787 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5788 | } |
| 5789 | }; |
| 5790 | |
| 5791 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5792 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5793 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5794 | expectKeyRepeatOnce(repeatCount); |
| 5795 | } |
| 5796 | } |
| 5797 | |
| 5798 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5799 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5800 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5801 | expectKeyRepeatOnce(repeatCount); |
| 5802 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5803 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5804 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5805 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5806 | expectKeyRepeatOnce(repeatCount); |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5811 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5812 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5813 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5814 | mWindow->assertNoEvents(); |
| 5815 | } |
| 5816 | |
| 5817 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5818 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5819 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5820 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5821 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5822 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5823 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5824 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5825 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 5826 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5827 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5828 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5829 | mWindow->assertNoEvents(); |
| 5830 | } |
| 5831 | |
| 5832 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5833 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5834 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5835 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5836 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5837 | // 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] | 5838 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5839 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5840 | mWindow->assertNoEvents(); |
| 5841 | } |
| 5842 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5843 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 5844 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5845 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5846 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5847 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 5848 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 5849 | mWindow->assertNoEvents(); |
| 5850 | } |
| 5851 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5852 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5853 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5854 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5855 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5856 | InputEvent* repeatEvent = mWindow->consume(); |
| 5857 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5858 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 5859 | IdGenerator::getSource(repeatEvent->getId())); |
| 5860 | } |
| 5861 | } |
| 5862 | |
| 5863 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5864 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5865 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5866 | |
| 5867 | std::unordered_set<int32_t> idSet; |
| 5868 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5869 | InputEvent* repeatEvent = mWindow->consume(); |
| 5870 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5871 | int32_t id = repeatEvent->getId(); |
| 5872 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 5873 | idSet.insert(id); |
| 5874 | } |
| 5875 | } |
| 5876 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5877 | /* Test InputDispatcher for MultiDisplay */ |
| 5878 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 5879 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5880 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5881 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5882 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5883 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5884 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5885 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5886 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5887 | // Set focus window for primary display, but focused display would be second one. |
| 5888 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5889 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5890 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5891 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5892 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5893 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5894 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5895 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5896 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5897 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5898 | // Set focus display to second one. |
| 5899 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 5900 | // Set focus window for second display. |
| 5901 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5902 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5903 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5904 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5905 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5906 | } |
| 5907 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5908 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5909 | InputDispatcherTest::TearDown(); |
| 5910 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5911 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5912 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5913 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5914 | windowInSecondary.clear(); |
| 5915 | } |
| 5916 | |
| 5917 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5918 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5919 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5920 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5921 | sp<FakeWindowHandle> windowInSecondary; |
| 5922 | }; |
| 5923 | |
| 5924 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 5925 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5927 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5928 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5929 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5930 | windowInSecondary->assertNoEvents(); |
| 5931 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5932 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5933 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5934 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5935 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5936 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5937 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5938 | } |
| 5939 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5940 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5941 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5942 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5943 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5944 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5945 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5946 | windowInSecondary->assertNoEvents(); |
| 5947 | |
| 5948 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5949 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5950 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5951 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5952 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5953 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5954 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5955 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5956 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5957 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5958 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5959 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5960 | |
| 5961 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5962 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5963 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5964 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5965 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5966 | windowInSecondary->assertNoEvents(); |
| 5967 | } |
| 5968 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5969 | // Test per-display input monitors for motion event. |
| 5970 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5971 | FakeMonitorReceiver monitorInPrimary = |
| 5972 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5973 | FakeMonitorReceiver monitorInSecondary = |
| 5974 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5975 | |
| 5976 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5977 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5978 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5979 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5980 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5981 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5982 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5983 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5984 | |
| 5985 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5986 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5987 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5988 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5989 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5990 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5991 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5992 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5993 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 5994 | // Lift up the touch from the second display |
| 5995 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5996 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5997 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5998 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5999 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 6000 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6001 | // Test inject a non-pointer motion event. |
| 6002 | // If specific a display, it will dispatch to the focused window of particular display, |
| 6003 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6004 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6005 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 6006 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6007 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6008 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6009 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6010 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6011 | } |
| 6012 | |
| 6013 | // Test per-display input monitors for key event. |
| 6014 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6015 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6016 | FakeMonitorReceiver monitorInPrimary = |
| 6017 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6018 | FakeMonitorReceiver monitorInSecondary = |
| 6019 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6020 | |
| 6021 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6022 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 6023 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6024 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6025 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6026 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6027 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6028 | } |
| 6029 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6030 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 6031 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6032 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6033 | secondWindowInPrimary->setFocusable(true); |
| 6034 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 6035 | setFocusedWindow(secondWindowInPrimary); |
| 6036 | windowInPrimary->consumeFocusEvent(false); |
| 6037 | secondWindowInPrimary->consumeFocusEvent(true); |
| 6038 | |
| 6039 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 6041 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6042 | windowInPrimary->assertNoEvents(); |
| 6043 | windowInSecondary->assertNoEvents(); |
| 6044 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6045 | } |
| 6046 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6047 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 6048 | FakeMonitorReceiver monitorInPrimary = |
| 6049 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6050 | FakeMonitorReceiver monitorInSecondary = |
| 6051 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6052 | |
| 6053 | // Test touch down on primary display. |
| 6054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6055 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6056 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6057 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6058 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6059 | |
| 6060 | // Test touch down on second display. |
| 6061 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6062 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 6063 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6064 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 6065 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 6066 | |
| 6067 | // Trigger cancel touch. |
| 6068 | mDispatcher->cancelCurrentTouch(); |
| 6069 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6070 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6071 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6072 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6073 | |
| 6074 | // Test inject a move motion event, no window/monitor should receive the event. |
| 6075 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6076 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6077 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6078 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 6079 | windowInPrimary->assertNoEvents(); |
| 6080 | monitorInPrimary.assertNoEvents(); |
| 6081 | |
| 6082 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6083 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6084 | SECOND_DISPLAY_ID, {110, 200})) |
| 6085 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 6086 | windowInSecondary->assertNoEvents(); |
| 6087 | monitorInSecondary.assertNoEvents(); |
| 6088 | } |
| 6089 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6090 | class InputFilterTest : public InputDispatcherTest { |
| 6091 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6092 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 6093 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6094 | NotifyMotionArgs motionArgs; |
| 6095 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6096 | motionArgs = |
| 6097 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6098 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6099 | motionArgs = |
| 6100 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6101 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6102 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6103 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6104 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 6105 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6106 | } else { |
| 6107 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 6108 | } |
| 6109 | } |
| 6110 | |
| 6111 | void testNotifyKey(bool expectToBeFiltered) { |
| 6112 | NotifyKeyArgs keyArgs; |
| 6113 | |
| 6114 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6115 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6116 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6117 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6118 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6119 | |
| 6120 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 6121 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6122 | } else { |
| 6123 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 6124 | } |
| 6125 | } |
| 6126 | }; |
| 6127 | |
| 6128 | // Test InputFilter for MotionEvent |
| 6129 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 6130 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 6131 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 6132 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 6133 | |
| 6134 | // Enable InputFilter |
| 6135 | mDispatcher->setInputFilterEnabled(true); |
| 6136 | // Test touch on both primary and second display, and check if both events are filtered. |
| 6137 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 6138 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 6139 | |
| 6140 | // Disable InputFilter |
| 6141 | mDispatcher->setInputFilterEnabled(false); |
| 6142 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 6143 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 6144 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 6145 | } |
| 6146 | |
| 6147 | // Test InputFilter for KeyEvent |
| 6148 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 6149 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 6150 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 6151 | |
| 6152 | // Enable InputFilter |
| 6153 | mDispatcher->setInputFilterEnabled(true); |
| 6154 | // Send a key event, and check if it is filtered. |
| 6155 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 6156 | |
| 6157 | // Disable InputFilter |
| 6158 | mDispatcher->setInputFilterEnabled(false); |
| 6159 | // Send a key event, and check if it isn't filtered. |
| 6160 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 6161 | } |
| 6162 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6163 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 6164 | // logical display coordinate space. |
| 6165 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 6166 | ui::Transform firstDisplayTransform; |
| 6167 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6168 | ui::Transform secondDisplayTransform; |
| 6169 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 6170 | |
| 6171 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 6172 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 6173 | displayInfos[0].transform = firstDisplayTransform; |
| 6174 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 6175 | displayInfos[1].transform = secondDisplayTransform; |
| 6176 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6177 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6178 | |
| 6179 | // Enable InputFilter |
| 6180 | mDispatcher->setInputFilterEnabled(true); |
| 6181 | |
| 6182 | // Ensure the correct transforms are used for the displays. |
| 6183 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 6184 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 6185 | } |
| 6186 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6187 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 6188 | protected: |
| 6189 | virtual void SetUp() override { |
| 6190 | InputDispatcherTest::SetUp(); |
| 6191 | |
| 6192 | /** |
| 6193 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 6194 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 6195 | * on. |
| 6196 | */ |
| 6197 | mDispatcher->setInputFilterEnabled(true); |
| 6198 | |
| 6199 | std::shared_ptr<InputApplicationHandle> application = |
| 6200 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6201 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 6202 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6203 | |
| 6204 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6205 | mWindow->setFocusable(true); |
| 6206 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6207 | setFocusedWindow(mWindow); |
| 6208 | mWindow->consumeFocusEvent(true); |
| 6209 | } |
| 6210 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6211 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 6212 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6213 | KeyEvent event; |
| 6214 | |
| 6215 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6216 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 6217 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6218 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6219 | const int32_t additionalPolicyFlags = |
| 6220 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 6221 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6222 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6223 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 6224 | policyFlags | additionalPolicyFlags)); |
| 6225 | |
| 6226 | InputEvent* received = mWindow->consume(); |
| 6227 | ASSERT_NE(nullptr, received); |
| 6228 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6229 | ASSERT_EQ(received->getType(), InputEventType::KEY); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6230 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 6231 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 6232 | } |
| 6233 | |
| 6234 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 6235 | int32_t flags) { |
| 6236 | MotionEvent event; |
| 6237 | PointerProperties pointerProperties[1]; |
| 6238 | PointerCoords pointerCoords[1]; |
| 6239 | pointerProperties[0].clear(); |
| 6240 | pointerProperties[0].id = 0; |
| 6241 | pointerCoords[0].clear(); |
| 6242 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 6243 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 6244 | |
| 6245 | ui::Transform identityTransform; |
| 6246 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6247 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 6248 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 6249 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 6250 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 6251 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 6252 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6253 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 6254 | |
| 6255 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 6256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6257 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6258 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 6259 | policyFlags | additionalPolicyFlags)); |
| 6260 | |
| 6261 | InputEvent* received = mWindow->consume(); |
| 6262 | ASSERT_NE(nullptr, received); |
| 6263 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6264 | ASSERT_EQ(received->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6265 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 6266 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6267 | } |
| 6268 | |
| 6269 | private: |
| 6270 | sp<FakeWindowHandle> mWindow; |
| 6271 | }; |
| 6272 | |
| 6273 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6274 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 6275 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 6276 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6277 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 6278 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6279 | } |
| 6280 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6281 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6282 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6283 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6284 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 6285 | } |
| 6286 | |
| 6287 | TEST_F(InputFilterInjectionPolicyTest, |
| 6288 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 6289 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6290 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6291 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6292 | } |
| 6293 | |
| 6294 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6295 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 6296 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6297 | } |
| 6298 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6299 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6300 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6301 | InputDispatcherTest::SetUp(); |
| 6302 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6303 | std::shared_ptr<FakeApplicationHandle> application = |
| 6304 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6305 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6306 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6307 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6308 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6309 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6310 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6311 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6312 | |
| 6313 | // Set focused application. |
| 6314 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6315 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6316 | |
| 6317 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6318 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6319 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6320 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6321 | } |
| 6322 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6323 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6324 | InputDispatcherTest::TearDown(); |
| 6325 | |
| 6326 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6327 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6328 | } |
| 6329 | |
| 6330 | protected: |
| 6331 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6332 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6333 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6334 | }; |
| 6335 | |
| 6336 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6337 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 6338 | // the onPointerDownOutsideFocus callback. |
| 6339 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6340 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6341 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6342 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6343 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6344 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6345 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6346 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6347 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 6348 | } |
| 6349 | |
| 6350 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 6351 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 6352 | // onPointerDownOutsideFocus callback. |
| 6353 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6354 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6355 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6356 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6357 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6358 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6359 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6360 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6361 | } |
| 6362 | |
| 6363 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 6364 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 6365 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6367 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6368 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6369 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6370 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6371 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6372 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6373 | } |
| 6374 | |
| 6375 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6376 | // DOWN on the window that already has focus. Ensure no window received the |
| 6377 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6378 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6379 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6380 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6381 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6382 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6383 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6384 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6385 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6386 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6387 | } |
| 6388 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6389 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 6390 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 6391 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 6392 | const MotionEvent event = |
| 6393 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6394 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 6395 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6396 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 6397 | .build(); |
| 6398 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 6399 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6400 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6401 | |
| 6402 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6403 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 6404 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 6405 | mUnfocusedWindow->assertNoEvents(); |
| 6406 | } |
| 6407 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6408 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 6409 | // windows that point to the same client token. |
| 6410 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 6411 | virtual void SetUp() override { |
| 6412 | InputDispatcherTest::SetUp(); |
| 6413 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6414 | std::shared_ptr<FakeApplicationHandle> application = |
| 6415 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6416 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 6417 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6418 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 6419 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6420 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 6421 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6422 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 6423 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6424 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | protected: |
| 6428 | sp<FakeWindowHandle> mWindow1; |
| 6429 | sp<FakeWindowHandle> mWindow2; |
| 6430 | |
| 6431 | // 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] | 6432 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 6433 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 6434 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6435 | } |
| 6436 | |
| 6437 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 6438 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6439 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6440 | InputEvent* event = window->consume(); |
| 6441 | |
| 6442 | ASSERT_NE(nullptr, event) << name.c_str() |
| 6443 | << ": consumer should have returned non-NULL event."; |
| 6444 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6445 | ASSERT_EQ(InputEventType::MOTION, event->getType()) |
| 6446 | << name.c_str() << ": expected MotionEvent, got " << *event; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6447 | |
| 6448 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6449 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 6450 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6451 | |
| 6452 | for (size_t i = 0; i < points.size(); i++) { |
| 6453 | float expectedX = points[i].x; |
| 6454 | float expectedY = points[i].y; |
| 6455 | |
| 6456 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 6457 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 6458 | << ", got " << motionEvent.getX(i); |
| 6459 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 6460 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 6461 | << ", got " << motionEvent.getY(i); |
| 6462 | } |
| 6463 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6464 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6465 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6466 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6467 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 6468 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6469 | |
| 6470 | // Always consume from window1 since it's the window that has the InputReceiver |
| 6471 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 6472 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6473 | }; |
| 6474 | |
| 6475 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 6476 | // Touch Window 1 |
| 6477 | PointF touchedPoint = {10, 10}; |
| 6478 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6479 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6480 | |
| 6481 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6482 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6483 | |
| 6484 | // Touch Window 2 |
| 6485 | touchedPoint = {150, 150}; |
| 6486 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6487 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6488 | } |
| 6489 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6490 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 6491 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6492 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6493 | |
| 6494 | // Touch Window 1 |
| 6495 | PointF touchedPoint = {10, 10}; |
| 6496 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6497 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6498 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6499 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6500 | |
| 6501 | // Touch Window 2 |
| 6502 | touchedPoint = {150, 150}; |
| 6503 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6504 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 6505 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6506 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6507 | // Update the transform so rotation is set |
| 6508 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6509 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 6510 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6511 | } |
| 6512 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6513 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6514 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6515 | |
| 6516 | // Touch Window 1 |
| 6517 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6518 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6519 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6520 | |
| 6521 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6522 | touchedPoints.push_back(PointF{150, 150}); |
| 6523 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6524 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6525 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6526 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6527 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6528 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6529 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6530 | // Update the transform so rotation is set for Window 2 |
| 6531 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6532 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6533 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6536 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6537 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6538 | |
| 6539 | // Touch Window 1 |
| 6540 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6541 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6542 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6543 | |
| 6544 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6545 | touchedPoints.push_back(PointF{150, 150}); |
| 6546 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6547 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6548 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6549 | |
| 6550 | // Move both windows |
| 6551 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6552 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6553 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6554 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6555 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6556 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6557 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6558 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6559 | expectedPoints.pop_back(); |
| 6560 | |
| 6561 | // Touch Window 2 |
| 6562 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6563 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6564 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6565 | |
| 6566 | // Move both windows |
| 6567 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6568 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6569 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6570 | |
| 6571 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6572 | } |
| 6573 | |
| 6574 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 6575 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 6576 | |
| 6577 | // Touch Window 1 |
| 6578 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6579 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6580 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6581 | |
| 6582 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6583 | touchedPoints.push_back(PointF{150, 150}); |
| 6584 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6585 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6586 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6587 | |
| 6588 | // Move both windows |
| 6589 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6590 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6591 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6592 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6593 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 6596 | /** |
| 6597 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 6598 | * same input channel. |
| 6599 | */ |
| 6600 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 6601 | mWindow1->setSlippery(true); |
| 6602 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6603 | |
| 6604 | // Touch down in window 1 |
| 6605 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6606 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6607 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 6608 | |
| 6609 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 6610 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 6611 | // getting generated. |
| 6612 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6613 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6614 | |
| 6615 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 6616 | } |
| 6617 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6618 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 6619 | virtual void SetUp() override { |
| 6620 | InputDispatcherTest::SetUp(); |
| 6621 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6622 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6623 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6624 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 6625 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6626 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6627 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6628 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6629 | |
| 6630 | // Set focused application. |
| 6631 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 6632 | |
| 6633 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6634 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6635 | mWindow->consumeFocusEvent(true); |
| 6636 | } |
| 6637 | |
| 6638 | virtual void TearDown() override { |
| 6639 | InputDispatcherTest::TearDown(); |
| 6640 | mWindow.clear(); |
| 6641 | } |
| 6642 | |
| 6643 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6644 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6645 | sp<FakeWindowHandle> mWindow; |
| 6646 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 6647 | |
| 6648 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6650 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6651 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6653 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6654 | WINDOW_LOCATION)); |
| 6655 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6656 | |
| 6657 | sp<FakeWindowHandle> addSpyWindow() { |
| 6658 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6659 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6660 | spy->setTrustedOverlay(true); |
| 6661 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6662 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6663 | spy->setDispatchingTimeout(30ms); |
| 6664 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 6665 | return spy; |
| 6666 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6667 | }; |
| 6668 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6669 | // Send a tap and respond, which should not cause an ANR. |
| 6670 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 6671 | tapOnWindow(); |
| 6672 | mWindow->consumeMotionDown(); |
| 6673 | mWindow->consumeMotionUp(); |
| 6674 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6675 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6676 | } |
| 6677 | |
| 6678 | // Send a regular key and respond, which should not cause an ANR. |
| 6679 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6680 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6681 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6682 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6683 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6684 | } |
| 6685 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6686 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 6687 | mWindow->setFocusable(false); |
| 6688 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6689 | mWindow->consumeFocusEvent(false); |
| 6690 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6691 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6692 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6693 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6694 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6695 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6696 | // Key will not go to window because we have no focused window. |
| 6697 | // The 'no focused window' ANR timer should start instead. |
| 6698 | |
| 6699 | // Now, the focused application goes away. |
| 6700 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 6701 | // The key should get dropped and there should be no ANR. |
| 6702 | |
| 6703 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6704 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6705 | } |
| 6706 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6707 | // 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] | 6708 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 6709 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6710 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6711 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6712 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6713 | WINDOW_LOCATION)); |
| 6714 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6715 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 6716 | ASSERT_TRUE(sequenceNum); |
| 6717 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6718 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6719 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6720 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6721 | mWindow->consumeMotionEvent( |
| 6722 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6723 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6724 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6725 | } |
| 6726 | |
| 6727 | // Send a key to the app and have the app not respond right away. |
| 6728 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 6729 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6731 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 6732 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6733 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6734 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6735 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6736 | } |
| 6737 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6738 | // We have a focused application, but no focused window |
| 6739 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6740 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6741 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6742 | mWindow->consumeFocusEvent(false); |
| 6743 | |
| 6744 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6746 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6747 | WINDOW_LOCATION)); |
| 6748 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 6749 | mDispatcher->waitForIdle(); |
| 6750 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6751 | |
| 6752 | // Once a focused event arrives, we get an ANR for this application |
| 6753 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6754 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6755 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6756 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6757 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6758 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6759 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6760 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6761 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6762 | } |
| 6763 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6764 | /** |
| 6765 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 6766 | * there will not be an ANR. |
| 6767 | */ |
| 6768 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 6769 | mWindow->setFocusable(false); |
| 6770 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6771 | mWindow->consumeFocusEvent(false); |
| 6772 | |
| 6773 | KeyEvent event; |
| 6774 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 6775 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 6776 | |
| 6777 | // Define a valid key down event that is stale (too old). |
| 6778 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 6779 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6780 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6781 | |
| 6782 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 6783 | |
| 6784 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6785 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6786 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 6787 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 6788 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 6789 | << "Injection should fail because the event is stale"; |
| 6790 | |
| 6791 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6792 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6793 | mWindow->assertNoEvents(); |
| 6794 | } |
| 6795 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6796 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6797 | // Make sure that we don't notify policy twice about the same ANR. |
| 6798 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6799 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6800 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6801 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6802 | |
| 6803 | // Once a focused event arrives, we get an ANR for this application |
| 6804 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6805 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6806 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6807 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6808 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6809 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6810 | const std::chrono::duration appTimeout = |
| 6811 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6812 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6813 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6814 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6815 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 6816 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6817 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6818 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 6819 | // '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] | 6820 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6821 | } |
| 6822 | |
| 6823 | // We have a focused application, but no focused window |
| 6824 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6825 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6826 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6827 | mWindow->consumeFocusEvent(false); |
| 6828 | |
| 6829 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6830 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6831 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6832 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6833 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6834 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6835 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6836 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6837 | |
| 6838 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6839 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6840 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6841 | mWindow->assertNoEvents(); |
| 6842 | } |
| 6843 | |
| 6844 | /** |
| 6845 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 6846 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 6847 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 6848 | * the ANR mechanism should still work. |
| 6849 | * |
| 6850 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 6851 | * DOWN event, while not responding on the second one. |
| 6852 | */ |
| 6853 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 6854 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6855 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6856 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6857 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6858 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6859 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6860 | |
| 6861 | // Now send ACTION_UP, with identical timestamp |
| 6862 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6863 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6864 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6865 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6866 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6867 | |
| 6868 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 6869 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6870 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6871 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6872 | } |
| 6873 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6874 | // A spy window can receive an ANR |
| 6875 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 6876 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 6877 | |
| 6878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6879 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6880 | WINDOW_LOCATION)); |
| 6881 | mWindow->consumeMotionDown(); |
| 6882 | |
| 6883 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 6884 | ASSERT_TRUE(sequenceNum); |
| 6885 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6886 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6887 | |
| 6888 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6889 | spy->consumeMotionEvent( |
| 6890 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6891 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6892 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | // 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] | 6896 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6897 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 6898 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6899 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6900 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6901 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6902 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6903 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6904 | |
| 6905 | // Stuck on the ACTION_UP |
| 6906 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6907 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6908 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6909 | // 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] | 6910 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6911 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6912 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6913 | |
| 6914 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6915 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6916 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6917 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6918 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6919 | } |
| 6920 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6921 | // 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] | 6922 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6923 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 6924 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6925 | |
| 6926 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6927 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6928 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6929 | |
| 6930 | mWindow->consumeMotionDown(); |
| 6931 | // Stuck on the ACTION_UP |
| 6932 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6933 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6934 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6935 | // 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] | 6936 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6937 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6938 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6939 | |
| 6940 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6941 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6942 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6943 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6944 | spy->assertNoEvents(); |
| 6945 | } |
| 6946 | |
| 6947 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 6948 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 6949 | |
| 6950 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6951 | |
| 6952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6953 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6954 | WINDOW_LOCATION)); |
| 6955 | |
| 6956 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6957 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 6958 | ASSERT_TRUE(consumeSeq); |
| 6959 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6960 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6961 | |
| 6962 | monitor.finishEvent(*consumeSeq); |
| 6963 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6964 | |
| 6965 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6966 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6967 | } |
| 6968 | |
| 6969 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 6970 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 6971 | // get an ANR again. |
| 6972 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 6973 | // 2. consume all pending events (= queue becomes healthy again) |
| 6974 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 6975 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 6976 | tapOnWindow(); |
| 6977 | |
| 6978 | mWindow->consumeMotionDown(); |
| 6979 | // Block on ACTION_UP |
| 6980 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6981 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6982 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 6983 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6984 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6985 | mWindow->assertNoEvents(); |
| 6986 | |
| 6987 | tapOnWindow(); |
| 6988 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6989 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6990 | mWindow->consumeMotionUp(); |
| 6991 | |
| 6992 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6993 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6994 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6995 | mWindow->assertNoEvents(); |
| 6996 | } |
| 6997 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6998 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 6999 | // it. |
| 7000 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7001 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7002 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7003 | WINDOW_LOCATION)); |
| 7004 | |
| 7005 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7006 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7007 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7008 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 7009 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7010 | // 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] | 7011 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7012 | mWindow->consumeMotionEvent( |
| 7013 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7014 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7015 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7016 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7017 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | /** |
| 7021 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7022 | * not to to the focused window until the motion is processed. |
| 7023 | * |
| 7024 | * Warning!!! |
| 7025 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7026 | * and the injection timeout that we specify when injecting the key. |
| 7027 | * We must have the injection timeout (10ms) be smaller than |
| 7028 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7029 | * |
| 7030 | * If that value changes, this test should also change. |
| 7031 | */ |
| 7032 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 7033 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 7034 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7035 | |
| 7036 | tapOnWindow(); |
| 7037 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 7038 | ASSERT_TRUE(downSequenceNum); |
| 7039 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 7040 | ASSERT_TRUE(upSequenceNum); |
| 7041 | // Don't finish the events yet, and send a key |
| 7042 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 7043 | // window even if motions are still being processed. But because the injection timeout is short, |
| 7044 | // we will receive INJECTION_TIMED_OUT as the result. |
| 7045 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7046 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7047 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7048 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 7049 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7050 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7051 | // and the key remains pending, waiting for the touch events to be processed |
| 7052 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 7053 | ASSERT_FALSE(keySequenceNum); |
| 7054 | |
| 7055 | std::this_thread::sleep_for(500ms); |
| 7056 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 7057 | // to the focused window, even though we have not yet finished the motion event |
| 7058 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7059 | mWindow->finishEvent(*downSequenceNum); |
| 7060 | mWindow->finishEvent(*upSequenceNum); |
| 7061 | } |
| 7062 | |
| 7063 | /** |
| 7064 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7065 | * not go to the focused window until the motion is processed. |
| 7066 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 7067 | * focused window right away. |
| 7068 | */ |
| 7069 | TEST_F(InputDispatcherSingleWindowAnr, |
| 7070 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 7071 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 7072 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7073 | |
| 7074 | tapOnWindow(); |
| 7075 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 7076 | ASSERT_TRUE(downSequenceNum); |
| 7077 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 7078 | ASSERT_TRUE(upSequenceNum); |
| 7079 | // Don't finish the events yet, and send a key |
| 7080 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7081 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7082 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7083 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7084 | // At this point, key is still pending, and should not be sent to the application yet. |
| 7085 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 7086 | ASSERT_FALSE(keySequenceNum); |
| 7087 | |
| 7088 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 7089 | tapOnWindow(); |
| 7090 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 7091 | // the other events yet. We can finish events in any order. |
| 7092 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 7093 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 7094 | mWindow->consumeMotionDown(); |
| 7095 | mWindow->consumeMotionUp(); |
| 7096 | mWindow->assertNoEvents(); |
| 7097 | } |
| 7098 | |
| 7099 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 7100 | virtual void SetUp() override { |
| 7101 | InputDispatcherTest::SetUp(); |
| 7102 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7103 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7104 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7105 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 7106 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7107 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7108 | // 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] | 7109 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7110 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7111 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 7112 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 7113 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7114 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7115 | |
| 7116 | // Set focused application. |
| 7117 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7118 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7119 | |
| 7120 | // Expect one focus window exist in display. |
| 7121 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7122 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7123 | mFocusedWindow->consumeFocusEvent(true); |
| 7124 | } |
| 7125 | |
| 7126 | virtual void TearDown() override { |
| 7127 | InputDispatcherTest::TearDown(); |
| 7128 | |
| 7129 | mUnfocusedWindow.clear(); |
| 7130 | mFocusedWindow.clear(); |
| 7131 | } |
| 7132 | |
| 7133 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7134 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7135 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 7136 | sp<FakeWindowHandle> mFocusedWindow; |
| 7137 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 7138 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 7139 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 7140 | |
| 7141 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 7142 | |
| 7143 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 7144 | |
| 7145 | private: |
| 7146 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7147 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7148 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7149 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7150 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7151 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7152 | location)); |
| 7153 | } |
| 7154 | }; |
| 7155 | |
| 7156 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 7157 | // should be ANR'd first. |
| 7158 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7159 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7160 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7161 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7162 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7163 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7164 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7165 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7166 | // We consumed all events, so no ANR |
| 7167 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7168 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7169 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7170 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7171 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7172 | FOCUSED_WINDOW_LOCATION)); |
| 7173 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7174 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7175 | |
| 7176 | const std::chrono::duration timeout = |
| 7177 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7178 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7179 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 7180 | // sequence to make it consistent |
| 7181 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7182 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7183 | mFocusedWindow->consumeMotionDown(); |
| 7184 | // This cancel is generated because the connection was unresponsive |
| 7185 | mFocusedWindow->consumeMotionCancel(); |
| 7186 | mFocusedWindow->assertNoEvents(); |
| 7187 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7188 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7189 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7190 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7191 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7192 | } |
| 7193 | |
| 7194 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 7195 | // it doesn't matter which order they should have ANR. |
| 7196 | // But we should receive ANR for both. |
| 7197 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 7198 | // Set the timeout for unfocused window to match the focused window |
| 7199 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 7200 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7201 | |
| 7202 | tapOnFocusedWindow(); |
| 7203 | // 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] | 7204 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 7205 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 7206 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7207 | |
| 7208 | // 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] | 7209 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 7210 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 7211 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 7212 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7213 | |
| 7214 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7215 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7216 | |
| 7217 | mFocusedWindow->consumeMotionDown(); |
| 7218 | mFocusedWindow->consumeMotionUp(); |
| 7219 | mUnfocusedWindow->consumeMotionOutside(); |
| 7220 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7221 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 7222 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 7223 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7224 | |
| 7225 | // Both applications should be marked as responsive, in any order |
| 7226 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 7227 | mFocusedWindow->getToken() == responsiveToken2); |
| 7228 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 7229 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 7230 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7231 | } |
| 7232 | |
| 7233 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 7234 | // We should also log an error to account for the dropped event (not tested here). |
| 7235 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 7236 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 7237 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7238 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7239 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7240 | // Receive the events, but don't respond |
| 7241 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 7242 | ASSERT_TRUE(downEventSequenceNum); |
| 7243 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 7244 | ASSERT_TRUE(upEventSequenceNum); |
| 7245 | const std::chrono::duration timeout = |
| 7246 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7247 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7248 | |
| 7249 | // Tap once again |
| 7250 | // 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] | 7251 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7252 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7253 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7254 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7255 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7256 | FOCUSED_WINDOW_LOCATION)); |
| 7257 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 7258 | // valid touch target |
| 7259 | mUnfocusedWindow->assertNoEvents(); |
| 7260 | |
| 7261 | // Consume the first tap |
| 7262 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 7263 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 7264 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7265 | // The second tap did not go to the focused window |
| 7266 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7267 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7268 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7269 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7270 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7271 | } |
| 7272 | |
| 7273 | // If you tap outside of all windows, there will not be ANR |
| 7274 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7275 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7276 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7277 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 7278 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7279 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7280 | } |
| 7281 | |
| 7282 | // Since the focused window is paused, tapping on it should not produce any events |
| 7283 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 7284 | mFocusedWindow->setPaused(true); |
| 7285 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7286 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7287 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7288 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7289 | FOCUSED_WINDOW_LOCATION)); |
| 7290 | |
| 7291 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 7292 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7293 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 7294 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7295 | |
| 7296 | mFocusedWindow->assertNoEvents(); |
| 7297 | mUnfocusedWindow->assertNoEvents(); |
| 7298 | } |
| 7299 | |
| 7300 | /** |
| 7301 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7302 | * not to to the focused window until the motion is processed. |
| 7303 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 7304 | * |
| 7305 | * Warning!!! |
| 7306 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7307 | * and the injection timeout that we specify when injecting the key. |
| 7308 | * We must have the injection timeout (10ms) be smaller than |
| 7309 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7310 | * |
| 7311 | * If that value changes, this test should also change. |
| 7312 | */ |
| 7313 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 7314 | // Set a long ANR timeout to prevent it from triggering |
| 7315 | mFocusedWindow->setDispatchingTimeout(2s); |
| 7316 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7317 | |
| 7318 | tapOnUnfocusedWindow(); |
| 7319 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7320 | ASSERT_TRUE(downSequenceNum); |
| 7321 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7322 | ASSERT_TRUE(upSequenceNum); |
| 7323 | // Don't finish the events yet, and send a key |
| 7324 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 7325 | // window even if motions are still being processed. |
| 7326 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7327 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7328 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7329 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7330 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7331 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7332 | // and the key remains pending, waiting for the touch events to be processed |
| 7333 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 7334 | ASSERT_FALSE(keySequenceNum); |
| 7335 | |
| 7336 | // 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] | 7337 | mFocusedWindow->setFocusable(false); |
| 7338 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7339 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7340 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7341 | |
| 7342 | // Focus events should precede the key events |
| 7343 | mUnfocusedWindow->consumeFocusEvent(true); |
| 7344 | mFocusedWindow->consumeFocusEvent(false); |
| 7345 | |
| 7346 | // Finish the tap events, which should unblock dispatcher |
| 7347 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 7348 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 7349 | |
| 7350 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 7351 | // can finally go to the newly focused "mUnfocusedWindow". |
| 7352 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7353 | mFocusedWindow->assertNoEvents(); |
| 7354 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7355 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7356 | } |
| 7357 | |
| 7358 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 7359 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 7360 | // The other window should not be affected by that. |
| 7361 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 7362 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7363 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7364 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7365 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7366 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7367 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7368 | |
| 7369 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7370 | mDispatcher->notifyMotion( |
| 7371 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7372 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7373 | |
| 7374 | const std::chrono::duration timeout = |
| 7375 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7376 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7377 | |
| 7378 | mUnfocusedWindow->consumeMotionDown(); |
| 7379 | mFocusedWindow->consumeMotionDown(); |
| 7380 | // Focused window may or may not receive ACTION_MOVE |
| 7381 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 7382 | InputEvent* event; |
| 7383 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 7384 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 7385 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 7386 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7387 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7388 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 7389 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 7390 | mFocusedWindow->consumeMotionCancel(); |
| 7391 | } else { |
| 7392 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 7393 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7394 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7395 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7396 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7397 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7398 | mUnfocusedWindow->assertNoEvents(); |
| 7399 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7400 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7401 | } |
| 7402 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7403 | /** |
| 7404 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 7405 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 7406 | * |
| 7407 | * If the user touches another application during this time, the key should be dropped. |
| 7408 | * Next, if a new focused window comes in, without toggling the focused application, |
| 7409 | * then no ANR should occur. |
| 7410 | * |
| 7411 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 7412 | * but in some cases the policy may not update the focused application. |
| 7413 | */ |
| 7414 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 7415 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 7416 | std::make_shared<FakeApplicationHandle>(); |
| 7417 | focusedApplication->setDispatchingTimeout(60ms); |
| 7418 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 7419 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 7420 | mFocusedWindow->setFocusable(false); |
| 7421 | |
| 7422 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7423 | mFocusedWindow->consumeFocusEvent(false); |
| 7424 | |
| 7425 | // Send a key. The ANR timer should start because there is no focused window. |
| 7426 | // 'focusedApplication' will get blamed if this timer completes. |
| 7427 | // 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] | 7428 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7429 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7430 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 7431 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7432 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7433 | |
| 7434 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 7435 | // then the injected touches won't cause the focused event to get dropped. |
| 7436 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 7437 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 7438 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 7439 | // For this test, it means that the key would get delivered to the window once it becomes |
| 7440 | // focused. |
| 7441 | std::this_thread::sleep_for(10ms); |
| 7442 | |
| 7443 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7444 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7445 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7446 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7447 | |
| 7448 | // We do not consume the motion right away, because that would require dispatcher to first |
| 7449 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 7450 | // Set the focused window first. |
| 7451 | mFocusedWindow->setFocusable(true); |
| 7452 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7453 | setFocusedWindow(mFocusedWindow); |
| 7454 | mFocusedWindow->consumeFocusEvent(true); |
| 7455 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 7456 | // to another application. This could be a bug / behaviour in the policy. |
| 7457 | |
| 7458 | mUnfocusedWindow->consumeMotionDown(); |
| 7459 | |
| 7460 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7461 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 7462 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 7463 | } |
| 7464 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7465 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 7466 | // that has feature NO_INPUT_CHANNEL. |
| 7467 | // Layout: |
| 7468 | // Top (closest to user) |
| 7469 | // mNoInputWindow (above all windows) |
| 7470 | // mBottomWindow |
| 7471 | // Bottom (furthest from user) |
| 7472 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 7473 | virtual void SetUp() override { |
| 7474 | InputDispatcherTest::SetUp(); |
| 7475 | |
| 7476 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7477 | mNoInputWindow = |
| 7478 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7479 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7480 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7481 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7482 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7483 | // It's perfectly valid for this window to not have an associated input channel |
| 7484 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7485 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 7486 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7487 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7488 | |
| 7489 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7490 | } |
| 7491 | |
| 7492 | protected: |
| 7493 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 7494 | sp<FakeWindowHandle> mNoInputWindow; |
| 7495 | sp<FakeWindowHandle> mBottomWindow; |
| 7496 | }; |
| 7497 | |
| 7498 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 7499 | PointF touchedPoint = {10, 10}; |
| 7500 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7501 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7502 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7503 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7504 | |
| 7505 | mNoInputWindow->assertNoEvents(); |
| 7506 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 7507 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 7508 | // and therefore should prevent mBottomWindow from receiving touches |
| 7509 | mBottomWindow->assertNoEvents(); |
| 7510 | } |
| 7511 | |
| 7512 | /** |
| 7513 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 7514 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 7515 | */ |
| 7516 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 7517 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7518 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7519 | "Window with input channel and NO_INPUT_CHANNEL", |
| 7520 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7521 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7522 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7523 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7524 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7525 | |
| 7526 | PointF touchedPoint = {10, 10}; |
| 7527 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7528 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7529 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7530 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7531 | |
| 7532 | mNoInputWindow->assertNoEvents(); |
| 7533 | mBottomWindow->assertNoEvents(); |
| 7534 | } |
| 7535 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7536 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7537 | protected: |
| 7538 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7539 | sp<FakeWindowHandle> mWindow; |
| 7540 | sp<FakeWindowHandle> mMirror; |
| 7541 | |
| 7542 | virtual void SetUp() override { |
| 7543 | InputDispatcherTest::SetUp(); |
| 7544 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7545 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7546 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7547 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7548 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7549 | mWindow->setFocusable(true); |
| 7550 | mMirror->setFocusable(true); |
| 7551 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7552 | } |
| 7553 | }; |
| 7554 | |
| 7555 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7556 | // Request focus on a mirrored window |
| 7557 | setFocusedWindow(mMirror); |
| 7558 | |
| 7559 | // window gets focused |
| 7560 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7561 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7562 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7563 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7564 | } |
| 7565 | |
| 7566 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7567 | // focusable. |
| 7568 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7569 | setFocusedWindow(mMirror); |
| 7570 | |
| 7571 | // window gets focused |
| 7572 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7574 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7575 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7577 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7578 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7579 | |
| 7580 | mMirror->setFocusable(false); |
| 7581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7582 | |
| 7583 | // window loses focus since one of the windows associated with the token in not focusable |
| 7584 | mWindow->consumeFocusEvent(false); |
| 7585 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7586 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7587 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7588 | mWindow->assertNoEvents(); |
| 7589 | } |
| 7590 | |
| 7591 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7592 | // invisible. |
| 7593 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 7594 | setFocusedWindow(mMirror); |
| 7595 | |
| 7596 | // window gets focused |
| 7597 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7598 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7599 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7600 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7601 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7602 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7603 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7604 | |
| 7605 | mMirror->setVisible(false); |
| 7606 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7607 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7608 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7609 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7610 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7612 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7613 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7614 | |
| 7615 | mWindow->setVisible(false); |
| 7616 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7617 | |
| 7618 | // window loses focus only after all windows associated with the token become invisible. |
| 7619 | mWindow->consumeFocusEvent(false); |
| 7620 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7621 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7622 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7623 | mWindow->assertNoEvents(); |
| 7624 | } |
| 7625 | |
| 7626 | // A focused & mirrored window remains focused until both windows are removed. |
| 7627 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7628 | setFocusedWindow(mMirror); |
| 7629 | |
| 7630 | // window gets focused |
| 7631 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7633 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7634 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7635 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7636 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7637 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7638 | |
| 7639 | // single window is removed but the window token remains focused |
| 7640 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7641 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7643 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7644 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7645 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7646 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7647 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7648 | |
| 7649 | // Both windows are removed |
| 7650 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7651 | mWindow->consumeFocusEvent(false); |
| 7652 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7653 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7654 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7655 | mWindow->assertNoEvents(); |
| 7656 | } |
| 7657 | |
| 7658 | // Focus request can be pending until one window becomes visible. |
| 7659 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7660 | // Request focus on an invisible mirror. |
| 7661 | mWindow->setVisible(false); |
| 7662 | mMirror->setVisible(false); |
| 7663 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7664 | setFocusedWindow(mMirror); |
| 7665 | |
| 7666 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7668 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7669 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7670 | |
| 7671 | mMirror->setVisible(true); |
| 7672 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7673 | |
| 7674 | // window gets focused |
| 7675 | mWindow->consumeFocusEvent(true); |
| 7676 | // window gets the pending key event |
| 7677 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7678 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7679 | |
| 7680 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7681 | protected: |
| 7682 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7683 | sp<FakeWindowHandle> mWindow; |
| 7684 | sp<FakeWindowHandle> mSecondWindow; |
| 7685 | |
| 7686 | void SetUp() override { |
| 7687 | InputDispatcherTest::SetUp(); |
| 7688 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7689 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7690 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7691 | mSecondWindow = |
| 7692 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7693 | mSecondWindow->setFocusable(true); |
| 7694 | |
| 7695 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7696 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7697 | |
| 7698 | setFocusedWindow(mWindow); |
| 7699 | mWindow->consumeFocusEvent(true); |
| 7700 | } |
| 7701 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7702 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7703 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7704 | } |
| 7705 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7706 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7707 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7708 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7709 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7710 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7711 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7712 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7713 | } |
| 7714 | }; |
| 7715 | |
| 7716 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7717 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7718 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7719 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7720 | mSecondWindow->assertNoEvents(); |
| 7721 | |
| 7722 | // Ensure that capture can be enabled from the focus window. |
| 7723 | requestAndVerifyPointerCapture(mWindow, true); |
| 7724 | |
| 7725 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7726 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7727 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7728 | |
| 7729 | // Ensure that capture can be disabled from the window with capture. |
| 7730 | requestAndVerifyPointerCapture(mWindow, false); |
| 7731 | } |
| 7732 | |
| 7733 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7734 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7735 | |
| 7736 | setFocusedWindow(mSecondWindow); |
| 7737 | |
| 7738 | // Ensure that the capture disabled event was sent first. |
| 7739 | mWindow->consumeCaptureEvent(false); |
| 7740 | mWindow->consumeFocusEvent(false); |
| 7741 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7742 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7743 | |
| 7744 | // 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] | 7745 | notifyPointerCaptureChanged({}); |
| 7746 | notifyPointerCaptureChanged(request); |
| 7747 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7748 | mWindow->assertNoEvents(); |
| 7749 | mSecondWindow->assertNoEvents(); |
| 7750 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7751 | } |
| 7752 | |
| 7753 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7754 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7755 | |
| 7756 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7757 | notifyPointerCaptureChanged({}); |
| 7758 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7759 | |
| 7760 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7761 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7762 | mWindow->consumeCaptureEvent(false); |
| 7763 | mWindow->assertNoEvents(); |
| 7764 | } |
| 7765 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7766 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7767 | requestAndVerifyPointerCapture(mWindow, true); |
| 7768 | |
| 7769 | // The first window loses focus. |
| 7770 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7771 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7772 | mWindow->consumeCaptureEvent(false); |
| 7773 | |
| 7774 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7775 | // arrives. |
| 7776 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7777 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7778 | |
| 7779 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7780 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7781 | |
| 7782 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7783 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7784 | |
| 7785 | mSecondWindow->consumeFocusEvent(true); |
| 7786 | mSecondWindow->consumeCaptureEvent(true); |
| 7787 | } |
| 7788 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7789 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7790 | // App repeatedly enables and disables capture. |
| 7791 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7792 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7793 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7794 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7795 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7796 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7797 | |
| 7798 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7799 | // first request is now stale, this should do nothing. |
| 7800 | notifyPointerCaptureChanged(firstRequest); |
| 7801 | mWindow->assertNoEvents(); |
| 7802 | |
| 7803 | // InputReader notifies that the second request was enabled. |
| 7804 | notifyPointerCaptureChanged(secondRequest); |
| 7805 | mWindow->consumeCaptureEvent(true); |
| 7806 | } |
| 7807 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7808 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7809 | requestAndVerifyPointerCapture(mWindow, true); |
| 7810 | |
| 7811 | // App toggles pointer capture off and on. |
| 7812 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7813 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7814 | |
| 7815 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7816 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7817 | |
| 7818 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7819 | // preceding "disable" request. |
| 7820 | notifyPointerCaptureChanged(enableRequest); |
| 7821 | |
| 7822 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7823 | // any notifications. |
| 7824 | mWindow->assertNoEvents(); |
| 7825 | } |
| 7826 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7827 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7828 | protected: |
| 7829 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7830 | |
| 7831 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7832 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7833 | |
| 7834 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7835 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7836 | |
| 7837 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7838 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7839 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7840 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7841 | MAXIMUM_OBSCURING_OPACITY); |
| 7842 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7843 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7844 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7845 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7846 | |
| 7847 | sp<FakeWindowHandle> mTouchWindow; |
| 7848 | |
| 7849 | virtual void SetUp() override { |
| 7850 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7851 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7852 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7853 | } |
| 7854 | |
| 7855 | virtual void TearDown() override { |
| 7856 | InputDispatcherTest::TearDown(); |
| 7857 | mTouchWindow.clear(); |
| 7858 | } |
| 7859 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7860 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 7861 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7862 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7863 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7864 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7865 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7866 | return window; |
| 7867 | } |
| 7868 | |
| 7869 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 7870 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7871 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7872 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7873 | // Generate an arbitrary PID based on the UID |
| 7874 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 7875 | return window; |
| 7876 | } |
| 7877 | |
| 7878 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7879 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7880 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7881 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7882 | } |
| 7883 | }; |
| 7884 | |
| 7885 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7886 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7887 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7889 | |
| 7890 | touch(); |
| 7891 | |
| 7892 | mTouchWindow->assertNoEvents(); |
| 7893 | } |
| 7894 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7895 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7896 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7897 | const sp<FakeWindowHandle>& w = |
| 7898 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7899 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7900 | |
| 7901 | touch(); |
| 7902 | |
| 7903 | mTouchWindow->assertNoEvents(); |
| 7904 | } |
| 7905 | |
| 7906 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7907 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7908 | const sp<FakeWindowHandle>& w = |
| 7909 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7910 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7911 | |
| 7912 | touch(); |
| 7913 | |
| 7914 | w->assertNoEvents(); |
| 7915 | } |
| 7916 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7917 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7918 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7919 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7920 | |
| 7921 | touch(); |
| 7922 | |
| 7923 | mTouchWindow->consumeAnyMotionDown(); |
| 7924 | } |
| 7925 | |
| 7926 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7927 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7928 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7929 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7930 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7931 | |
| 7932 | touch({PointF{100, 100}}); |
| 7933 | |
| 7934 | mTouchWindow->consumeAnyMotionDown(); |
| 7935 | } |
| 7936 | |
| 7937 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7938 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7939 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7940 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7941 | |
| 7942 | touch(); |
| 7943 | |
| 7944 | mTouchWindow->consumeAnyMotionDown(); |
| 7945 | } |
| 7946 | |
| 7947 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 7948 | const sp<FakeWindowHandle>& w = |
| 7949 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7950 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7951 | |
| 7952 | touch(); |
| 7953 | |
| 7954 | mTouchWindow->consumeAnyMotionDown(); |
| 7955 | } |
| 7956 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7957 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 7958 | const sp<FakeWindowHandle>& w = |
| 7959 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7960 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7961 | |
| 7962 | touch(); |
| 7963 | |
| 7964 | w->assertNoEvents(); |
| 7965 | } |
| 7966 | |
| 7967 | /** |
| 7968 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 7969 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 7970 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 7971 | */ |
| 7972 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7973 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 7974 | const sp<FakeWindowHandle>& w = |
| 7975 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7976 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7977 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7978 | |
| 7979 | touch(); |
| 7980 | |
| 7981 | w->consumeMotionOutside(); |
| 7982 | } |
| 7983 | |
| 7984 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 7985 | const sp<FakeWindowHandle>& w = |
| 7986 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7987 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7988 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7989 | |
| 7990 | touch(); |
| 7991 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7992 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7993 | } |
| 7994 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7995 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7996 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7997 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7998 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7999 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8000 | |
| 8001 | touch(); |
| 8002 | |
| 8003 | mTouchWindow->consumeAnyMotionDown(); |
| 8004 | } |
| 8005 | |
| 8006 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 8007 | const sp<FakeWindowHandle>& w = |
| 8008 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8009 | MAXIMUM_OBSCURING_OPACITY); |
| 8010 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8011 | |
| 8012 | touch(); |
| 8013 | |
| 8014 | mTouchWindow->consumeAnyMotionDown(); |
| 8015 | } |
| 8016 | |
| 8017 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8018 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8019 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8020 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8021 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8022 | |
| 8023 | touch(); |
| 8024 | |
| 8025 | mTouchWindow->assertNoEvents(); |
| 8026 | } |
| 8027 | |
| 8028 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 8029 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 8030 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8031 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8032 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8033 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8034 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8035 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8036 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8037 | |
| 8038 | touch(); |
| 8039 | |
| 8040 | mTouchWindow->assertNoEvents(); |
| 8041 | } |
| 8042 | |
| 8043 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 8044 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 8045 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8046 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8047 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8048 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8049 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8050 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8051 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8052 | |
| 8053 | touch(); |
| 8054 | |
| 8055 | mTouchWindow->consumeAnyMotionDown(); |
| 8056 | } |
| 8057 | |
| 8058 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8059 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 8060 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8061 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8062 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8063 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8064 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8065 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8066 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 8067 | |
| 8068 | touch(); |
| 8069 | |
| 8070 | mTouchWindow->consumeAnyMotionDown(); |
| 8071 | } |
| 8072 | |
| 8073 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 8074 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8075 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8076 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8077 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8078 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8079 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8080 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8081 | |
| 8082 | touch(); |
| 8083 | |
| 8084 | mTouchWindow->assertNoEvents(); |
| 8085 | } |
| 8086 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8087 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8088 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 8089 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8090 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8091 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8092 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8093 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8094 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8095 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 8096 | |
| 8097 | touch(); |
| 8098 | |
| 8099 | mTouchWindow->assertNoEvents(); |
| 8100 | } |
| 8101 | |
| 8102 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8103 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 8104 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8105 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8106 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8107 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8108 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8109 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8110 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 8111 | |
| 8112 | touch(); |
| 8113 | |
| 8114 | mTouchWindow->consumeAnyMotionDown(); |
| 8115 | } |
| 8116 | |
| 8117 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 8118 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8119 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8120 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8121 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8122 | |
| 8123 | touch(); |
| 8124 | |
| 8125 | mTouchWindow->consumeAnyMotionDown(); |
| 8126 | } |
| 8127 | |
| 8128 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 8129 | const sp<FakeWindowHandle>& w = |
| 8130 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 8131 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8132 | |
| 8133 | touch(); |
| 8134 | |
| 8135 | mTouchWindow->consumeAnyMotionDown(); |
| 8136 | } |
| 8137 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8138 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8139 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 8140 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 8141 | const sp<FakeWindowHandle>& w = |
| 8142 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 8143 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8144 | |
| 8145 | touch(); |
| 8146 | |
| 8147 | mTouchWindow->assertNoEvents(); |
| 8148 | } |
| 8149 | |
| 8150 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 8151 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 8152 | const sp<FakeWindowHandle>& w = |
| 8153 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 8154 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8155 | |
| 8156 | touch(); |
| 8157 | |
| 8158 | mTouchWindow->consumeAnyMotionDown(); |
| 8159 | } |
| 8160 | |
| 8161 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8162 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 8163 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 8164 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8165 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8166 | OPACITY_ABOVE_THRESHOLD); |
| 8167 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8168 | |
| 8169 | touch(); |
| 8170 | |
| 8171 | mTouchWindow->consumeAnyMotionDown(); |
| 8172 | } |
| 8173 | |
| 8174 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8175 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 8176 | const sp<FakeWindowHandle>& w1 = |
| 8177 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 8178 | OPACITY_BELOW_THRESHOLD); |
| 8179 | const sp<FakeWindowHandle>& w2 = |
| 8180 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8181 | OPACITY_BELOW_THRESHOLD); |
| 8182 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8183 | |
| 8184 | touch(); |
| 8185 | |
| 8186 | mTouchWindow->assertNoEvents(); |
| 8187 | } |
| 8188 | |
| 8189 | /** |
| 8190 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 8191 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 8192 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 8193 | */ |
| 8194 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8195 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 8196 | const sp<FakeWindowHandle>& wB = |
| 8197 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 8198 | OPACITY_BELOW_THRESHOLD); |
| 8199 | const sp<FakeWindowHandle>& wC = |
| 8200 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8201 | OPACITY_BELOW_THRESHOLD); |
| 8202 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 8203 | |
| 8204 | touch(); |
| 8205 | |
| 8206 | mTouchWindow->assertNoEvents(); |
| 8207 | } |
| 8208 | |
| 8209 | /** |
| 8210 | * This test is testing that a window from a different UID but with same application token doesn't |
| 8211 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 8212 | */ |
| 8213 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8214 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 8215 | const sp<FakeWindowHandle>& w = |
| 8216 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 8217 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8218 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8219 | |
| 8220 | touch(); |
| 8221 | |
| 8222 | mTouchWindow->consumeAnyMotionDown(); |
| 8223 | } |
| 8224 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8225 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 8226 | protected: |
| 8227 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8228 | sp<FakeWindowHandle> mWindow; |
| 8229 | sp<FakeWindowHandle> mSecondWindow; |
| 8230 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8231 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8232 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 8233 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8234 | |
| 8235 | void SetUp() override { |
| 8236 | InputDispatcherTest::SetUp(); |
| 8237 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8238 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8239 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8240 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8241 | mSecondWindow = |
| 8242 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8243 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8244 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8245 | mSpyWindow = |
| 8246 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8247 | mSpyWindow->setSpy(true); |
| 8248 | mSpyWindow->setTrustedOverlay(true); |
| 8249 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 8250 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8251 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8252 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8253 | } |
| 8254 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8255 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 8256 | switch (fromSource) { |
| 8257 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 8258 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8259 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8260 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8261 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8262 | break; |
| 8263 | case AINPUT_SOURCE_STYLUS: |
| 8264 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8265 | injectMotionEvent( |
| 8266 | mDispatcher, |
| 8267 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8268 | AINPUT_SOURCE_STYLUS) |
| 8269 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8270 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8271 | .x(50) |
| 8272 | .y(50)) |
| 8273 | .build())); |
| 8274 | break; |
| 8275 | case AINPUT_SOURCE_MOUSE: |
| 8276 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8277 | injectMotionEvent( |
| 8278 | mDispatcher, |
| 8279 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8280 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8281 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8282 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8283 | .x(50) |
| 8284 | .y(50)) |
| 8285 | .build())); |
| 8286 | break; |
| 8287 | default: |
| 8288 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 8289 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8290 | |
| 8291 | // Window should receive motion event. |
| 8292 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8293 | // Spy window should also receive motion event |
| 8294 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
| 8297 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 8298 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 8299 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8300 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8301 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8302 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8303 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8304 | |
| 8305 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8306 | mDragWindow = |
| 8307 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8308 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8309 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8310 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8311 | |
| 8312 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8313 | bool transferred = |
| 8314 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8315 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8316 | if (transferred) { |
| 8317 | mWindow->consumeMotionCancel(); |
| 8318 | mDragWindow->consumeMotionDown(); |
| 8319 | } |
| 8320 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8321 | } |
| 8322 | }; |
| 8323 | |
| 8324 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8325 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8326 | |
| 8327 | // Move on window. |
| 8328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8329 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8330 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8331 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8332 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8333 | mWindow->consumeDragEvent(false, 50, 50); |
| 8334 | mSecondWindow->assertNoEvents(); |
| 8335 | |
| 8336 | // Move to another window. |
| 8337 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8338 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8339 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8340 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8341 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8342 | mWindow->consumeDragEvent(true, 150, 50); |
| 8343 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8344 | |
| 8345 | // Move back to original window. |
| 8346 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8347 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8348 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8349 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8350 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8351 | mWindow->consumeDragEvent(false, 50, 50); |
| 8352 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 8353 | |
| 8354 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8355 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8356 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8357 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8358 | mWindow->assertNoEvents(); |
| 8359 | mSecondWindow->assertNoEvents(); |
| 8360 | } |
| 8361 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8362 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8363 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8364 | |
| 8365 | // No cancel event after drag start |
| 8366 | mSpyWindow->assertNoEvents(); |
| 8367 | |
| 8368 | const MotionEvent secondFingerDownEvent = |
| 8369 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8370 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8371 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8372 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8373 | .build(); |
| 8374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8375 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8376 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8377 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8378 | |
| 8379 | // Receives cancel for first pointer after next pointer down |
| 8380 | mSpyWindow->consumeMotionCancel(); |
| 8381 | mSpyWindow->consumeMotionDown(); |
| 8382 | |
| 8383 | mSpyWindow->assertNoEvents(); |
| 8384 | } |
| 8385 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8386 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8387 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8388 | |
| 8389 | // Move on window. |
| 8390 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8391 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8392 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8393 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8394 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8395 | mWindow->consumeDragEvent(false, 50, 50); |
| 8396 | mSecondWindow->assertNoEvents(); |
| 8397 | |
| 8398 | // Move to another window. |
| 8399 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8400 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8401 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8402 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8403 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8404 | mWindow->consumeDragEvent(true, 150, 50); |
| 8405 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8406 | |
| 8407 | // drop to another window. |
| 8408 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8409 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8410 | {150, 50})) |
| 8411 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8412 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8413 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8414 | mWindow->assertNoEvents(); |
| 8415 | mSecondWindow->assertNoEvents(); |
| 8416 | } |
| 8417 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8418 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8419 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8420 | |
| 8421 | // Move on window and keep button pressed. |
| 8422 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8423 | injectMotionEvent(mDispatcher, |
| 8424 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8425 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8426 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8427 | .build())) |
| 8428 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8429 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8430 | mWindow->consumeDragEvent(false, 50, 50); |
| 8431 | mSecondWindow->assertNoEvents(); |
| 8432 | |
| 8433 | // Move to another window and release button, expect to drop item. |
| 8434 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8435 | injectMotionEvent(mDispatcher, |
| 8436 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8437 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8438 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8439 | .build())) |
| 8440 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8441 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8442 | mWindow->assertNoEvents(); |
| 8443 | mSecondWindow->assertNoEvents(); |
| 8444 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8445 | |
| 8446 | // nothing to the window. |
| 8447 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8448 | injectMotionEvent(mDispatcher, |
| 8449 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 8450 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8451 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8452 | .build())) |
| 8453 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8454 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8455 | mWindow->assertNoEvents(); |
| 8456 | mSecondWindow->assertNoEvents(); |
| 8457 | } |
| 8458 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8459 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8460 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8461 | |
| 8462 | // Set second window invisible. |
| 8463 | mSecondWindow->setVisible(false); |
| 8464 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 8465 | |
| 8466 | // Move on window. |
| 8467 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8468 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8469 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8470 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8471 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8472 | mWindow->consumeDragEvent(false, 50, 50); |
| 8473 | mSecondWindow->assertNoEvents(); |
| 8474 | |
| 8475 | // Move to another window. |
| 8476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8477 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8478 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8479 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8480 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8481 | mWindow->consumeDragEvent(true, 150, 50); |
| 8482 | mSecondWindow->assertNoEvents(); |
| 8483 | |
| 8484 | // drop to another window. |
| 8485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8486 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8487 | {150, 50})) |
| 8488 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8489 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8490 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 8491 | mWindow->assertNoEvents(); |
| 8492 | mSecondWindow->assertNoEvents(); |
| 8493 | } |
| 8494 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8495 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8496 | // Ensure window could track pointerIds if it didn't support split touch. |
| 8497 | mWindow->setPreventSplitting(true); |
| 8498 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8500 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8501 | {50, 50})) |
| 8502 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8503 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8504 | |
| 8505 | const MotionEvent secondFingerDownEvent = |
| 8506 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8507 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8508 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8509 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8510 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8511 | .build(); |
| 8512 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8513 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8514 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8515 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8516 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8517 | |
| 8518 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8519 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8520 | } |
| 8521 | |
| 8522 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 8523 | // First down on second window. |
| 8524 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8525 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8526 | {150, 50})) |
| 8527 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8528 | |
| 8529 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8530 | |
| 8531 | // Second down on first window. |
| 8532 | const MotionEvent secondFingerDownEvent = |
| 8533 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8534 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8535 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8536 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8537 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8538 | .build(); |
| 8539 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8540 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8541 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8542 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8543 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8544 | |
| 8545 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8546 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8547 | |
| 8548 | // Move on window. |
| 8549 | const MotionEvent secondFingerMoveEvent = |
| 8550 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8551 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8552 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8553 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8554 | .build(); |
| 8555 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8556 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8557 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8558 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8559 | mWindow->consumeDragEvent(false, 50, 50); |
| 8560 | mSecondWindow->consumeMotionMove(); |
| 8561 | |
| 8562 | // Release the drag pointer should perform drop. |
| 8563 | const MotionEvent secondFingerUpEvent = |
| 8564 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8565 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8566 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8567 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8568 | .build(); |
| 8569 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8570 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8571 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8572 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8573 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 8574 | mWindow->assertNoEvents(); |
| 8575 | mSecondWindow->consumeMotionMove(); |
| 8576 | } |
| 8577 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8578 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8579 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8580 | |
| 8581 | // Update window of second display. |
| 8582 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8583 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8584 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8585 | |
| 8586 | // Let second display has a touch state. |
| 8587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8588 | injectMotionEvent(mDispatcher, |
| 8589 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8590 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8591 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8592 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8593 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8594 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8595 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8596 | // Update window again. |
| 8597 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8598 | |
| 8599 | // Move on window. |
| 8600 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8601 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8602 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8603 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8604 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8605 | mWindow->consumeDragEvent(false, 50, 50); |
| 8606 | mSecondWindow->assertNoEvents(); |
| 8607 | |
| 8608 | // Move to another window. |
| 8609 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8610 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8611 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8612 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8613 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8614 | mWindow->consumeDragEvent(true, 150, 50); |
| 8615 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8616 | |
| 8617 | // drop to another window. |
| 8618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8619 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8620 | {150, 50})) |
| 8621 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8622 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8623 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8624 | mWindow->assertNoEvents(); |
| 8625 | mSecondWindow->assertNoEvents(); |
| 8626 | } |
| 8627 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8628 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8629 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8630 | // Move on window. |
| 8631 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8632 | injectMotionEvent(mDispatcher, |
| 8633 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8634 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8635 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8636 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8637 | .x(50) |
| 8638 | .y(50)) |
| 8639 | .build())) |
| 8640 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8641 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8642 | mWindow->consumeDragEvent(false, 50, 50); |
| 8643 | mSecondWindow->assertNoEvents(); |
| 8644 | |
| 8645 | // Move to another window. |
| 8646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8647 | injectMotionEvent(mDispatcher, |
| 8648 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8649 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8650 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8651 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8652 | .x(150) |
| 8653 | .y(50)) |
| 8654 | .build())) |
| 8655 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8656 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8657 | mWindow->consumeDragEvent(true, 150, 50); |
| 8658 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8659 | |
| 8660 | // drop to another window. |
| 8661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8662 | injectMotionEvent(mDispatcher, |
| 8663 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8664 | .buttonState(0) |
| 8665 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8666 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8667 | .x(150) |
| 8668 | .y(50)) |
| 8669 | .build())) |
| 8670 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8671 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8672 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8673 | mWindow->assertNoEvents(); |
| 8674 | mSecondWindow->assertNoEvents(); |
| 8675 | } |
| 8676 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8677 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8678 | |
| 8679 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8680 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8681 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8682 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8683 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8684 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8685 | window->setFocusable(true); |
| 8686 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8687 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8688 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8689 | |
| 8690 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8691 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8692 | window->assertNoEvents(); |
| 8693 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8694 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8695 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8696 | window->assertNoEvents(); |
| 8697 | |
| 8698 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8699 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8700 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8701 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8702 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8703 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8704 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8705 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8706 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8707 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8708 | window->assertNoEvents(); |
| 8709 | } |
| 8710 | |
| 8711 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8712 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8713 | std::make_shared<FakeApplicationHandle>(); |
| 8714 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8715 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8716 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8717 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8718 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8719 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8720 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8721 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8722 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8723 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8724 | window->setOwnerInfo(222, 222); |
| 8725 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8726 | window->setFocusable(true); |
| 8727 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8728 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8729 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8730 | |
| 8731 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8732 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8733 | window->assertNoEvents(); |
| 8734 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8735 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8736 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8737 | window->assertNoEvents(); |
| 8738 | |
| 8739 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8740 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8741 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8742 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8743 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8744 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8745 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8746 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8747 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8748 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8749 | window->assertNoEvents(); |
| 8750 | } |
| 8751 | |
| 8752 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8753 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8754 | std::make_shared<FakeApplicationHandle>(); |
| 8755 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8756 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8757 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8758 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8759 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8760 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8761 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8762 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8763 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8764 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8765 | window->setOwnerInfo(222, 222); |
| 8766 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8767 | window->setFocusable(true); |
| 8768 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8769 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8770 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8771 | |
| 8772 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8773 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8774 | window->assertNoEvents(); |
| 8775 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8776 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8777 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8778 | window->assertNoEvents(); |
| 8779 | |
| 8780 | // When the window is no longer obscured because it went on top, it should get input |
| 8781 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8782 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8783 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8784 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8785 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8786 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8787 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8788 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8789 | window->assertNoEvents(); |
| 8790 | } |
| 8791 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8792 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8793 | protected: |
| 8794 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8795 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8796 | sp<FakeWindowHandle> mWindow; |
| 8797 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8798 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8799 | |
| 8800 | void SetUp() override { |
| 8801 | InputDispatcherTest::SetUp(); |
| 8802 | |
| 8803 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8804 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8805 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8806 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8807 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8808 | mSecondWindow = |
| 8809 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8810 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8811 | mThirdWindow = |
| 8812 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8813 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8814 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8815 | |
| 8816 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8817 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8818 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8819 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8820 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8821 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8822 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8823 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8824 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8825 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8826 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8827 | mThirdWindow->assertNoEvents(); |
| 8828 | } |
| 8829 | |
| 8830 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8831 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8832 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8833 | SECOND_DISPLAY_ID)) { |
| 8834 | mWindow->assertNoEvents(); |
| 8835 | mSecondWindow->assertNoEvents(); |
| 8836 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8837 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8838 | } |
| 8839 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8840 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 8841 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8842 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8843 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8844 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8845 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8846 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8847 | } |
| 8848 | }; |
| 8849 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8850 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8851 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8852 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8853 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8854 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8855 | } |
| 8856 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8857 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8858 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8859 | int32_t ownerPid = windowInfo.ownerPid; |
| 8860 | int32_t ownerUid = windowInfo.ownerUid; |
| 8861 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 8862 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8863 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8864 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8865 | mWindow->assertNoEvents(); |
| 8866 | mSecondWindow->assertNoEvents(); |
| 8867 | } |
| 8868 | |
| 8869 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8870 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8871 | int32_t ownerPid = windowInfo.ownerPid; |
| 8872 | int32_t ownerUid = windowInfo.ownerUid; |
| 8873 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8874 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8875 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8876 | } |
| 8877 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8878 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8879 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8880 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8881 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8882 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8883 | mWindow->assertNoEvents(); |
| 8884 | mSecondWindow->assertNoEvents(); |
| 8885 | } |
| 8886 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8887 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8888 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8889 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8890 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8891 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8892 | mWindow->assertNoEvents(); |
| 8893 | mSecondWindow->assertNoEvents(); |
| 8894 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8895 | } |
| 8896 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8897 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8898 | // Interact with the window first. |
| 8899 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8900 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8901 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8902 | |
| 8903 | // Then remove focus. |
| 8904 | mWindow->setFocusable(false); |
| 8905 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8906 | |
| 8907 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8908 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8909 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8910 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8911 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8912 | } |
| 8913 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8914 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8915 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8916 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8917 | std::shared_ptr<FakeApplicationHandle> application = |
| 8918 | std::make_shared<FakeApplicationHandle>(); |
| 8919 | std::string name = "Fake Spy "; |
| 8920 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8921 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8922 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8923 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8924 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8925 | return spy; |
| 8926 | } |
| 8927 | |
| 8928 | sp<FakeWindowHandle> createForeground() { |
| 8929 | std::shared_ptr<FakeApplicationHandle> application = |
| 8930 | std::make_shared<FakeApplicationHandle>(); |
| 8931 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8932 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 8933 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8934 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8935 | return window; |
| 8936 | } |
| 8937 | |
| 8938 | private: |
| 8939 | int mSpyCount{0}; |
| 8940 | }; |
| 8941 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8942 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8943 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8944 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 8945 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8946 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 8947 | ScopedSilentDeath _silentDeath; |
| 8948 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8949 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8950 | spy->setTrustedOverlay(false); |
| 8951 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 8952 | ".* not a trusted overlay"); |
| 8953 | } |
| 8954 | |
| 8955 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8956 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 8957 | */ |
| 8958 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8959 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8960 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 8961 | |
| 8962 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8963 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8964 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8965 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8966 | } |
| 8967 | |
| 8968 | /** |
| 8969 | * Verify the order in which different input windows receive events. The touched foreground window |
| 8970 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 8971 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 8972 | * receive events before ones belows it. |
| 8973 | * |
| 8974 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 8975 | * spy1, spy2, window, spy3. |
| 8976 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 8977 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 8978 | * window. |
| 8979 | */ |
| 8980 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 8981 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8982 | auto spy1 = createSpy(); |
| 8983 | auto spy2 = createSpy(); |
| 8984 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8985 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 8986 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 8987 | const size_t numChannels = channels.size(); |
| 8988 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 8989 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8990 | if (!epollFd.ok()) { |
| 8991 | FAIL() << "Failed to create epoll fd"; |
| 8992 | } |
| 8993 | |
| 8994 | for (size_t i = 0; i < numChannels; i++) { |
| 8995 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 8996 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 8997 | FAIL() << "Failed to add fd to epoll"; |
| 8998 | } |
| 8999 | } |
| 9000 | |
| 9001 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9002 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9003 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9004 | |
| 9005 | std::vector<size_t> eventOrder; |
| 9006 | std::vector<struct epoll_event> events(numChannels); |
| 9007 | for (;;) { |
| 9008 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 9009 | (100ms).count()); |
| 9010 | if (nFds < 0) { |
| 9011 | FAIL() << "Failed to call epoll_wait"; |
| 9012 | } |
| 9013 | if (nFds == 0) { |
| 9014 | break; // epoll_wait timed out |
| 9015 | } |
| 9016 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 9017 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 9018 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9019 | channels[i]->consumeMotionDown(); |
| 9020 | } |
| 9021 | } |
| 9022 | |
| 9023 | // Verify the order in which the events were received. |
| 9024 | EXPECT_EQ(3u, eventOrder.size()); |
| 9025 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 9026 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 9027 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 9028 | } |
| 9029 | |
| 9030 | /** |
| 9031 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 9032 | */ |
| 9033 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 9034 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9035 | auto spy = createSpy(); |
| 9036 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9037 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9038 | |
| 9039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9040 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9041 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9042 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9043 | spy->assertNoEvents(); |
| 9044 | } |
| 9045 | |
| 9046 | /** |
| 9047 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 9048 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 9049 | * to the window. |
| 9050 | */ |
| 9051 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 9052 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9053 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9054 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 9055 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9056 | |
| 9057 | // Inject an event outside the spy window's touchable region. |
| 9058 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9059 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9060 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9061 | window->consumeMotionDown(); |
| 9062 | spy->assertNoEvents(); |
| 9063 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9064 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9065 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9066 | window->consumeMotionUp(); |
| 9067 | spy->assertNoEvents(); |
| 9068 | |
| 9069 | // Inject an event inside the spy window's touchable region. |
| 9070 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9071 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9072 | {5, 10})) |
| 9073 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9074 | window->consumeMotionDown(); |
| 9075 | spy->consumeMotionDown(); |
| 9076 | } |
| 9077 | |
| 9078 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9079 | * 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] | 9080 | * 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] | 9081 | */ |
| 9082 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 9083 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9084 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9085 | auto spy = createSpy(); |
| 9086 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9087 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9088 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 9089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9090 | |
| 9091 | // Inject an event outside the spy window's frame and touchable region. |
| 9092 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9093 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9094 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9095 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9096 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9097 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9098 | } |
| 9099 | |
| 9100 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9101 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 9102 | * pointers that are down within its bounds. |
| 9103 | */ |
| 9104 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 9105 | auto windowLeft = createForeground(); |
| 9106 | windowLeft->setFrame({0, 0, 100, 200}); |
| 9107 | auto windowRight = createForeground(); |
| 9108 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9109 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9110 | spy->setFrame({0, 0, 200, 200}); |
| 9111 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 9112 | |
| 9113 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9114 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9115 | {50, 50})) |
| 9116 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9117 | windowLeft->consumeMotionDown(); |
| 9118 | spy->consumeMotionDown(); |
| 9119 | |
| 9120 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9121 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9122 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9123 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9124 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9125 | .build(); |
| 9126 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9127 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9128 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9129 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9130 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9131 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9132 | } |
| 9133 | |
| 9134 | /** |
| 9135 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 9136 | * the spy should receive the second pointer with ACTION_DOWN. |
| 9137 | */ |
| 9138 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 9139 | auto window = createForeground(); |
| 9140 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9141 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9142 | spyRight->setFrame({100, 0, 200, 200}); |
| 9143 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 9144 | |
| 9145 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9146 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9147 | {50, 50})) |
| 9148 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9149 | window->consumeMotionDown(); |
| 9150 | spyRight->assertNoEvents(); |
| 9151 | |
| 9152 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9153 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9154 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9155 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9156 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9157 | .build(); |
| 9158 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9159 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9160 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9161 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9162 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9163 | spyRight->consumeMotionDown(); |
| 9164 | } |
| 9165 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9166 | /** |
| 9167 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 9168 | * windows should be allowed to control split touch. |
| 9169 | */ |
| 9170 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9171 | // 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] | 9172 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9173 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9174 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9175 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9176 | auto window = createForeground(); |
| 9177 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9178 | |
| 9179 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9180 | |
| 9181 | // First finger down, no window touched. |
| 9182 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9183 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9184 | {100, 200})) |
| 9185 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9186 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9187 | window->assertNoEvents(); |
| 9188 | |
| 9189 | // Second finger down on window, the window should receive touch down. |
| 9190 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9191 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9192 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9193 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9194 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9195 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9196 | .build(); |
| 9197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9198 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9199 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9200 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9201 | |
| 9202 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9203 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9204 | } |
| 9205 | |
| 9206 | /** |
| 9207 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 9208 | * do not receive key events. |
| 9209 | */ |
| 9210 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9211 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9212 | spy->setFocusable(false); |
| 9213 | |
| 9214 | auto window = createForeground(); |
| 9215 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9216 | setFocusedWindow(window); |
| 9217 | window->consumeFocusEvent(true); |
| 9218 | |
| 9219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 9220 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9221 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9222 | |
| 9223 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 9224 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9225 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9226 | |
| 9227 | spy->assertNoEvents(); |
| 9228 | } |
| 9229 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9230 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 9231 | |
| 9232 | /** |
| 9233 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 9234 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 9235 | */ |
| 9236 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 9237 | auto window = createForeground(); |
| 9238 | auto spy1 = createSpy(); |
| 9239 | auto spy2 = createSpy(); |
| 9240 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 9241 | |
| 9242 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9243 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9244 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9245 | window->consumeMotionDown(); |
| 9246 | spy1->consumeMotionDown(); |
| 9247 | spy2->consumeMotionDown(); |
| 9248 | |
| 9249 | // Pilfer pointers from the second spy window. |
| 9250 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 9251 | spy2->assertNoEvents(); |
| 9252 | spy1->consumeMotionCancel(); |
| 9253 | window->consumeMotionCancel(); |
| 9254 | |
| 9255 | // The rest of the gesture should only be sent to the second spy window. |
| 9256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9257 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 9258 | ADISPLAY_ID_DEFAULT)) |
| 9259 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9260 | spy2->consumeMotionMove(); |
| 9261 | spy1->assertNoEvents(); |
| 9262 | window->assertNoEvents(); |
| 9263 | } |
| 9264 | |
| 9265 | /** |
| 9266 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 9267 | * in the middle of the gesture. |
| 9268 | */ |
| 9269 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 9270 | auto window = createForeground(); |
| 9271 | auto spy = createSpy(); |
| 9272 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9273 | |
| 9274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9275 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9276 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9277 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9278 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9279 | |
| 9280 | window->releaseChannel(); |
| 9281 | |
| 9282 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9283 | |
| 9284 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9285 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9286 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9287 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9288 | } |
| 9289 | |
| 9290 | /** |
| 9291 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 9292 | * the spy, but not to any other windows. |
| 9293 | */ |
| 9294 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 9295 | auto spy = createSpy(); |
| 9296 | auto window = createForeground(); |
| 9297 | |
| 9298 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9299 | |
| 9300 | // First finger down on the window and the spy. |
| 9301 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9302 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9303 | {100, 200})) |
| 9304 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9305 | spy->consumeMotionDown(); |
| 9306 | window->consumeMotionDown(); |
| 9307 | |
| 9308 | // Spy window pilfers the pointers. |
| 9309 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9310 | window->consumeMotionCancel(); |
| 9311 | |
| 9312 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 9313 | const MotionEvent secondFingerDownEvent = |
| 9314 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9315 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9316 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9317 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9318 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9319 | .build(); |
| 9320 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9321 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9322 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9323 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9324 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9325 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9326 | |
| 9327 | // Third finger goes down outside all windows, so injection should fail. |
| 9328 | const MotionEvent thirdFingerDownEvent = |
| 9329 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9330 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9331 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9332 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9333 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 9334 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9335 | .build(); |
| 9336 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 9337 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9338 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9339 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9340 | |
| 9341 | spy->assertNoEvents(); |
| 9342 | window->assertNoEvents(); |
| 9343 | } |
| 9344 | |
| 9345 | /** |
| 9346 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 9347 | */ |
| 9348 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 9349 | auto spy = createSpy(); |
| 9350 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9351 | auto window = createForeground(); |
| 9352 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9353 | |
| 9354 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9355 | |
| 9356 | // First finger down on the window only |
| 9357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9358 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9359 | {150, 150})) |
| 9360 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9361 | window->consumeMotionDown(); |
| 9362 | |
| 9363 | // Second finger down on the spy and window |
| 9364 | const MotionEvent secondFingerDownEvent = |
| 9365 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9366 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9367 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9368 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9369 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9370 | .build(); |
| 9371 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9372 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9373 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9374 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9375 | spy->consumeMotionDown(); |
| 9376 | window->consumeMotionPointerDown(1); |
| 9377 | |
| 9378 | // Third finger down on the spy and window |
| 9379 | const MotionEvent thirdFingerDownEvent = |
| 9380 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9381 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9382 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9383 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9384 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 9385 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9386 | .build(); |
| 9387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9388 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9389 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9390 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9391 | spy->consumeMotionPointerDown(1); |
| 9392 | window->consumeMotionPointerDown(2); |
| 9393 | |
| 9394 | // Spy window pilfers the pointers. |
| 9395 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9396 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9397 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9398 | |
| 9399 | spy->assertNoEvents(); |
| 9400 | window->assertNoEvents(); |
| 9401 | } |
| 9402 | |
| 9403 | /** |
| 9404 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 9405 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 9406 | * window, then that window should receive ACTION_CANCEL. |
| 9407 | */ |
| 9408 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 9409 | auto spy = createSpy(); |
| 9410 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9411 | auto window = createForeground(); |
| 9412 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9413 | |
| 9414 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9415 | |
| 9416 | // First finger down on both spy and window |
| 9417 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9418 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9419 | {10, 10})) |
| 9420 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9421 | window->consumeMotionDown(); |
| 9422 | spy->consumeMotionDown(); |
| 9423 | |
| 9424 | // Second finger down on the spy and window |
| 9425 | const MotionEvent secondFingerDownEvent = |
| 9426 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9427 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9428 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9429 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9430 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9431 | .build(); |
| 9432 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9433 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9434 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9435 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9436 | spy->consumeMotionPointerDown(1); |
| 9437 | window->consumeMotionPointerDown(1); |
| 9438 | |
| 9439 | // Spy window pilfers the pointers. |
| 9440 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9441 | window->consumeMotionCancel(); |
| 9442 | |
| 9443 | spy->assertNoEvents(); |
| 9444 | window->assertNoEvents(); |
| 9445 | } |
| 9446 | |
| 9447 | /** |
| 9448 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 9449 | * be sent to other windows |
| 9450 | */ |
| 9451 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 9452 | auto spy = createSpy(); |
| 9453 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9454 | auto window = createForeground(); |
| 9455 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9456 | |
| 9457 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9458 | |
| 9459 | // First finger down on both window and spy |
| 9460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9461 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9462 | {10, 10})) |
| 9463 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9464 | window->consumeMotionDown(); |
| 9465 | spy->consumeMotionDown(); |
| 9466 | |
| 9467 | // Spy window pilfers the pointers. |
| 9468 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9469 | window->consumeMotionCancel(); |
| 9470 | |
| 9471 | // Second finger down on the window only |
| 9472 | const MotionEvent secondFingerDownEvent = |
| 9473 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9474 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9475 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9476 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9477 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9478 | .build(); |
| 9479 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9480 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9481 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9482 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9483 | window->consumeMotionDown(); |
| 9484 | window->assertNoEvents(); |
| 9485 | |
| 9486 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 9487 | spy->consumeMotionMove(); |
| 9488 | spy->assertNoEvents(); |
| 9489 | } |
| 9490 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9491 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 9492 | public: |
| 9493 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 9494 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9495 | std::make_shared<FakeApplicationHandle>(); |
| 9496 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9497 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 9498 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9499 | overlay->setFocusable(false); |
| 9500 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9501 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9502 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9503 | overlay->setTrustedOverlay(true); |
| 9504 | |
| 9505 | std::shared_ptr<FakeApplicationHandle> application = |
| 9506 | std::make_shared<FakeApplicationHandle>(); |
| 9507 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9508 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9509 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9510 | window->setFocusable(true); |
| 9511 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9512 | |
| 9513 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9514 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9515 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9516 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9517 | return {std::move(overlay), std::move(window)}; |
| 9518 | } |
| 9519 | |
| 9520 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9521 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9522 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9523 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9524 | } |
| 9525 | |
| 9526 | void sendStylusEvent(int32_t action) { |
| 9527 | NotifyMotionArgs motionArgs = |
| 9528 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9529 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9530 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9531 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9532 | } |
| 9533 | }; |
| 9534 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9535 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9536 | |
| 9537 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 9538 | ScopedSilentDeath _silentDeath; |
| 9539 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9540 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9541 | overlay->setTrustedOverlay(false); |
| 9542 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9543 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9544 | ".* not a trusted overlay"); |
| 9545 | } |
| 9546 | |
| 9547 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9548 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9549 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9550 | |
| 9551 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9552 | overlay->consumeMotionDown(); |
| 9553 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9554 | overlay->consumeMotionUp(); |
| 9555 | |
| 9556 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9557 | window->consumeMotionDown(); |
| 9558 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9559 | window->consumeMotionUp(); |
| 9560 | |
| 9561 | overlay->assertNoEvents(); |
| 9562 | window->assertNoEvents(); |
| 9563 | } |
| 9564 | |
| 9565 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9566 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9567 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9568 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9569 | |
| 9570 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9571 | overlay->consumeMotionDown(); |
| 9572 | window->consumeMotionDown(); |
| 9573 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9574 | overlay->consumeMotionUp(); |
| 9575 | window->consumeMotionUp(); |
| 9576 | |
| 9577 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9578 | window->consumeMotionDown(); |
| 9579 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9580 | window->consumeMotionUp(); |
| 9581 | |
| 9582 | overlay->assertNoEvents(); |
| 9583 | window->assertNoEvents(); |
| 9584 | } |
| 9585 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9586 | /** |
| 9587 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9588 | * The scenario is as follows: |
| 9589 | * - The stylus interceptor overlay is configured as a spy window. |
| 9590 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9591 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9592 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9593 | */ |
| 9594 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9595 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9596 | overlay->setSpy(true); |
| 9597 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9598 | |
| 9599 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9600 | overlay->consumeMotionDown(); |
| 9601 | window->consumeMotionDown(); |
| 9602 | |
| 9603 | // The interceptor pilfers the pointers. |
| 9604 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9605 | window->consumeMotionCancel(); |
| 9606 | |
| 9607 | // The interceptor configures itself so that it is no longer a spy. |
| 9608 | overlay->setSpy(false); |
| 9609 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9610 | |
| 9611 | // It continues to receive the rest of the stylus gesture. |
| 9612 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9613 | overlay->consumeMotionMove(); |
| 9614 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9615 | overlay->consumeMotionUp(); |
| 9616 | |
| 9617 | window->assertNoEvents(); |
| 9618 | } |
| 9619 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9620 | struct User { |
| 9621 | int32_t mPid; |
| 9622 | int32_t mUid; |
| 9623 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9624 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9625 | |
| 9626 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 9627 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9628 | |
| 9629 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9630 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9631 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9632 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9633 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9634 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9635 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9636 | } |
| 9637 | |
| 9638 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9639 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9640 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9641 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9642 | mPolicyFlags); |
| 9643 | } |
| 9644 | |
| 9645 | sp<FakeWindowHandle> createWindow() const { |
| 9646 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9647 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9648 | sp<FakeWindowHandle> window = |
| 9649 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9650 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9651 | window->setOwnerInfo(mPid, mUid); |
| 9652 | return window; |
| 9653 | } |
| 9654 | }; |
| 9655 | |
| 9656 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9657 | |
| 9658 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 9659 | auto owner = User(mDispatcher, 10, 11); |
| 9660 | auto window = owner.createWindow(); |
| 9661 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9662 | |
| 9663 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9664 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9665 | window->consumeMotionDown(); |
| 9666 | |
| 9667 | setFocusedWindow(window); |
| 9668 | window->consumeFocusEvent(true); |
| 9669 | |
| 9670 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9671 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9672 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9673 | } |
| 9674 | |
| 9675 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 9676 | auto owner = User(mDispatcher, 10, 11); |
| 9677 | auto window = owner.createWindow(); |
| 9678 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9679 | |
| 9680 | auto rando = User(mDispatcher, 20, 21); |
| 9681 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9682 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9683 | |
| 9684 | setFocusedWindow(window); |
| 9685 | window->consumeFocusEvent(true); |
| 9686 | |
| 9687 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9688 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9689 | window->assertNoEvents(); |
| 9690 | } |
| 9691 | |
| 9692 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 9693 | auto owner = User(mDispatcher, 10, 11); |
| 9694 | auto window = owner.createWindow(); |
| 9695 | auto spy = owner.createWindow(); |
| 9696 | spy->setSpy(true); |
| 9697 | spy->setTrustedOverlay(true); |
| 9698 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9699 | |
| 9700 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9701 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9702 | spy->consumeMotionDown(); |
| 9703 | window->consumeMotionDown(); |
| 9704 | } |
| 9705 | |
| 9706 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 9707 | auto owner = User(mDispatcher, 10, 11); |
| 9708 | auto window = owner.createWindow(); |
| 9709 | |
| 9710 | auto rando = User(mDispatcher, 20, 21); |
| 9711 | auto randosSpy = rando.createWindow(); |
| 9712 | randosSpy->setSpy(true); |
| 9713 | randosSpy->setTrustedOverlay(true); |
| 9714 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9715 | |
| 9716 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9717 | // not receive the event. |
| 9718 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9719 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9720 | randosSpy->assertNoEvents(); |
| 9721 | window->consumeMotionDown(); |
| 9722 | } |
| 9723 | |
| 9724 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 9725 | auto owner = User(mDispatcher, 10, 11); |
| 9726 | auto window = owner.createWindow(); |
| 9727 | |
| 9728 | auto rando = User(mDispatcher, 20, 21); |
| 9729 | auto randosSpy = rando.createWindow(); |
| 9730 | randosSpy->setSpy(true); |
| 9731 | randosSpy->setTrustedOverlay(true); |
| 9732 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9733 | |
| 9734 | // A user that has injection permission can inject into any window. |
| 9735 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9736 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9737 | ADISPLAY_ID_DEFAULT)); |
| 9738 | randosSpy->consumeMotionDown(); |
| 9739 | window->consumeMotionDown(); |
| 9740 | |
| 9741 | setFocusedWindow(randosSpy); |
| 9742 | randosSpy->consumeFocusEvent(true); |
| 9743 | |
| 9744 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9745 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9746 | window->assertNoEvents(); |
| 9747 | } |
| 9748 | |
| 9749 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 9750 | auto owner = User(mDispatcher, 10, 11); |
| 9751 | auto window = owner.createWindow(); |
| 9752 | |
| 9753 | auto rando = User(mDispatcher, 20, 21); |
| 9754 | auto randosWindow = rando.createWindow(); |
| 9755 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9756 | randosWindow->setWatchOutsideTouch(true); |
| 9757 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9758 | |
| 9759 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 9760 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9761 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9762 | window->consumeMotionDown(); |
| 9763 | randosWindow->consumeMotionOutside(); |
| 9764 | } |
| 9765 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9766 | } // namespace android::inputdispatcher |