Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 | |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 19 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 20 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 22 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 23 | #include <binder/Binder.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 25 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 27 | #include <input/Input.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 28 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 29 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 30 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 31 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 32 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 33 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 34 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 35 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 36 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 37 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 38 | using android::gui::FocusRequest; |
| 39 | using android::gui::TouchOcclusionMode; |
| 40 | using android::gui::WindowInfo; |
| 41 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 42 | using android::os::InputEventInjectionResult; |
| 43 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 44 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 45 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 46 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 47 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 48 | using testing::AllOf; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 49 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 50 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 51 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | |
| 53 | // An arbitrary device id. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 54 | static constexpr int32_t DEVICE_ID = 1; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 55 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 56 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 57 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 58 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 59 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 60 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 61 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 62 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 63 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 64 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 65 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 66 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 67 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 68 | /** |
| 69 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 70 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 71 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 72 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 73 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 74 | * pointer id=0 leaves but the pointer id=1 remains. |
| 75 | */ |
| 76 | static constexpr int32_t POINTER_0_DOWN = |
| 77 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 78 | static constexpr int32_t POINTER_1_DOWN = |
| 79 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 80 | static constexpr int32_t POINTER_2_DOWN = |
| 81 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 82 | static constexpr int32_t POINTER_3_DOWN = |
| 83 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 84 | static constexpr int32_t POINTER_0_UP = |
| 85 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 86 | static constexpr int32_t POINTER_1_UP = |
| 87 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 88 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 89 | // The default pid and uid for windows created on the primary display by the test. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 90 | static constexpr int32_t WINDOW_PID = 999; |
| 91 | static constexpr int32_t WINDOW_UID = 1001; |
| 92 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 93 | // The default pid and uid for the windows created on the secondary display by the test. |
| 94 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; |
| 95 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; |
| 96 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 97 | // The default policy flags to use for event injection by tests. |
| 98 | static constexpr uint32_t DEFAULT_POLICY_FLAGS = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 99 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 100 | // An arbitrary pid of the gesture monitor window |
| 101 | static constexpr int32_t MONITOR_PID = 2001; |
| 102 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 103 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 104 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 105 | static constexpr int expectedWallpaperFlags = |
| 106 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 107 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 108 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 109 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 110 | struct PointF { |
| 111 | float x; |
| 112 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 113 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 114 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 116 | /** |
| 117 | * Return a DOWN key event with KEYCODE_A. |
| 118 | */ |
| 119 | static KeyEvent getTestKeyEvent() { |
| 120 | KeyEvent event; |
| 121 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 122 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 123 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 124 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 125 | return event; |
| 126 | } |
| 127 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 128 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 129 | ASSERT_EQ(expectedAction, receivedAction) |
| 130 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 131 | << MotionEvent::actionToString(receivedAction); |
| 132 | } |
| 133 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 134 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 135 | bool matches = action == arg.getAction(); |
| 136 | if (!matches) { |
| 137 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 138 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 139 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 140 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 141 | if (!matches) { |
| 142 | *result_listener << "; "; |
| 143 | } |
| 144 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 145 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 146 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 147 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 148 | if (!matches) { |
| 149 | *result_listener << "; "; |
| 150 | } |
| 151 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 152 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 153 | } |
| 154 | return matches; |
| 155 | } |
| 156 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 157 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 158 | return arg.getDownTime() == downTime; |
| 159 | } |
| 160 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 161 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { |
| 162 | return arg.getDisplayId() == displayId; |
| 163 | } |
| 164 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 165 | MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") { |
| 166 | return arg.getDeviceId() == deviceId; |
| 167 | } |
| 168 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 169 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 170 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 171 | << inputEventSourceToString(arg.getSource()); |
| 172 | return arg.getSource() == source; |
| 173 | } |
| 174 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 175 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { |
| 176 | return arg.getFlags() == flags; |
| 177 | } |
| 178 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 179 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 180 | if (arg.getPointerCount() != 1) { |
| 181 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 182 | return false; |
| 183 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 184 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 187 | MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") { |
| 188 | return arg.getPointerCount() == pointerCount; |
| 189 | } |
| 190 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 191 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 192 | // Build a map for the received pointers, by pointer id |
| 193 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 194 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 195 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 196 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 197 | } |
| 198 | return pointers == actualPointers; |
| 199 | } |
| 200 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 201 | // --- FakeInputDispatcherPolicy --- |
| 202 | |
| 203 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 204 | InputDispatcherConfiguration mConfig; |
| 205 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 206 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 207 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | protected: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 209 | virtual ~FakeInputDispatcherPolicy() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | |
| 211 | public: |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 212 | FakeInputDispatcherPolicy() {} |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 213 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 214 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 215 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 216 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 217 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 218 | |
| 219 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 220 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 221 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 222 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 223 | } |
| 224 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 225 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 226 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 227 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 228 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 229 | |
| 230 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 231 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 232 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 233 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 234 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 235 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 236 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 237 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 238 | } |
| 239 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 240 | void assertFilterInputEventWasNotCalled() { |
| 241 | std::scoped_lock lock(mLock); |
| 242 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 243 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 245 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 246 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 247 | ASSERT_TRUE(mConfigurationChangedTime) |
| 248 | << "Timed out waiting for configuration changed call"; |
| 249 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 250 | mConfigurationChangedTime = std::nullopt; |
| 251 | } |
| 252 | |
| 253 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 254 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 255 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 256 | // We do not check id because it is not exposed to the policy |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 257 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 258 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 259 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 260 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 261 | mLastNotifySwitch = std::nullopt; |
| 262 | } |
| 263 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 264 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 265 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 266 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 267 | mOnPointerDownToken.clear(); |
| 268 | } |
| 269 | |
| 270 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 271 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 272 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 273 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 276 | // This function must be called soon after the expected ANR timer starts, |
| 277 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 278 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 279 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 280 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 281 | std::unique_lock lock(mLock); |
| 282 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 283 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 284 | ASSERT_NO_FATAL_FAILURE( |
| 285 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 286 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 289 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 290 | const sp<WindowInfoHandle>& window) { |
| 291 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 292 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 293 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 294 | } |
| 295 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 296 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 297 | const sp<IBinder>& expectedToken, |
| 298 | int32_t expectedPid) { |
| 299 | std::unique_lock lock(mLock); |
| 300 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 301 | AnrResult result; |
| 302 | ASSERT_NO_FATAL_FAILURE(result = |
| 303 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 304 | const auto& [token, pid] = result; |
| 305 | ASSERT_EQ(expectedToken, token); |
| 306 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 307 | } |
| 308 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 309 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 310 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 311 | std::unique_lock lock(mLock); |
| 312 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 313 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 314 | const auto& [token, _] = result; |
| 315 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 318 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 319 | int32_t expectedPid) { |
| 320 | std::unique_lock lock(mLock); |
| 321 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 322 | AnrResult result; |
| 323 | ASSERT_NO_FATAL_FAILURE( |
| 324 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 325 | const auto& [token, pid] = result; |
| 326 | ASSERT_EQ(expectedToken, token); |
| 327 | ASSERT_EQ(expectedPid, pid); |
| 328 | } |
| 329 | |
| 330 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 331 | sp<IBinder> getResponsiveWindowToken() { |
| 332 | std::unique_lock lock(mLock); |
| 333 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 334 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 335 | const auto& [token, _] = result; |
| 336 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void assertNotifyAnrWasNotCalled() { |
| 340 | std::scoped_lock lock(mLock); |
| 341 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 342 | ASSERT_TRUE(mAnrWindows.empty()); |
| 343 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 344 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 345 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 348 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 349 | mConfig.keyRepeatTimeout = timeout; |
| 350 | mConfig.keyRepeatDelay = delay; |
| 351 | } |
| 352 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 353 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 354 | std::unique_lock lock(mLock); |
| 355 | base::ScopedLockAssertion assumeLocked(mLock); |
| 356 | |
| 357 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 358 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 359 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 360 | enabled; |
| 361 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 362 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 363 | << ") to be called."; |
| 364 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 365 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 366 | auto request = *mPointerCaptureRequest; |
| 367 | mPointerCaptureRequest.reset(); |
| 368 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void assertSetPointerCaptureNotCalled() { |
| 372 | std::unique_lock lock(mLock); |
| 373 | base::ScopedLockAssertion assumeLocked(mLock); |
| 374 | |
| 375 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 376 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 377 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 378 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 379 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 380 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 381 | } |
| 382 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 383 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 384 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 385 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 386 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 387 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 388 | } |
| 389 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 390 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 391 | std::unique_lock lock(mLock); |
| 392 | base::ScopedLockAssertion assumeLocked(mLock); |
| 393 | std::optional<sp<IBinder>> receivedToken = |
| 394 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 395 | mNotifyInputChannelBroken); |
| 396 | ASSERT_TRUE(receivedToken.has_value()); |
| 397 | ASSERT_EQ(token, *receivedToken); |
| 398 | } |
| 399 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 400 | /** |
| 401 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 402 | */ |
| 403 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 404 | mInterceptKeyTimeout = timeout; |
| 405 | } |
| 406 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 407 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 408 | std::mutex mLock; |
| 409 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 410 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 411 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 412 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 413 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 414 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 415 | |
| 416 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 417 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 418 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 419 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 420 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 421 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 422 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 423 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 424 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 425 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 426 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 427 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 428 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 429 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 430 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 431 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 432 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 433 | // first entry from the container and erase it. |
| 434 | template <class T> |
| 435 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 436 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 437 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 438 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 439 | // before checking if ANR was called. |
| 440 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 441 | // to provide it some time to act. 100ms seems reasonable. |
| 442 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 443 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 444 | std::optional<T> token = |
| 445 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 446 | if (!token.has_value()) { |
| 447 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 448 | return {}; |
| 449 | } |
| 450 | |
| 451 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 452 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 453 | // the dispatcher started counting before this function was called |
| 454 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 455 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 456 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 457 | << "ms, but waited " |
| 458 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 459 | << "ms instead"; |
| 460 | } |
| 461 | return *token; |
| 462 | } |
| 463 | |
| 464 | template <class T> |
| 465 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 466 | std::queue<T>& storage, |
| 467 | std::unique_lock<std::mutex>& lock, |
| 468 | std::condition_variable& condition) |
| 469 | REQUIRES(mLock) { |
| 470 | condition.wait_for(lock, timeout, |
| 471 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 472 | if (storage.empty()) { |
| 473 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 474 | return std::nullopt; |
| 475 | } |
| 476 | T item = storage.front(); |
| 477 | storage.pop(); |
| 478 | return std::make_optional(item); |
| 479 | } |
| 480 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 481 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 482 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 483 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 486 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 487 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 488 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 489 | ASSERT_TRUE(pid.has_value()); |
| 490 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 491 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 492 | } |
| 493 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 494 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 495 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 496 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 497 | ASSERT_TRUE(pid.has_value()); |
| 498 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 499 | mNotifyAnr.notify_all(); |
| 500 | } |
| 501 | |
| 502 | void notifyNoFocusedWindowAnr( |
| 503 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 504 | std::scoped_lock lock(mLock); |
| 505 | mAnrApplications.push(applicationHandle); |
| 506 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 509 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 510 | std::scoped_lock lock(mLock); |
| 511 | mBrokenInputChannels.push(connectionToken); |
| 512 | mNotifyInputChannelBroken.notify_all(); |
| 513 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 514 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 515 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 516 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 517 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 518 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 519 | const std::vector<float>& values) override {} |
| 520 | |
| 521 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 522 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 523 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 524 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 525 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 526 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 527 | *outConfig = mConfig; |
| 528 | } |
| 529 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 530 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 531 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 532 | switch (inputEvent->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 533 | case InputEventType::KEY: { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 534 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 535 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 539 | case InputEventType::MOTION: { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 540 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 541 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 542 | break; |
| 543 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 544 | default: { |
| 545 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 546 | break; |
| 547 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 548 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 549 | return true; |
| 550 | } |
| 551 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 552 | void interceptKeyBeforeQueueing(const KeyEvent* inputEvent, uint32_t&) override { |
| 553 | if (inputEvent->getAction() == AKEY_EVENT_ACTION_UP) { |
| 554 | // Clear intercept state when we handled the event. |
| 555 | mInterceptKeyTimeout = 0ms; |
| 556 | } |
| 557 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 558 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 559 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 560 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 561 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 562 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 563 | // Clear intercept state so we could dispatch the event in next wake. |
| 564 | mInterceptKeyTimeout = 0ms; |
| 565 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 568 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 572 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 573 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 574 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 575 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 576 | * essentially a passthrough for notifySwitch. |
| 577 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 578 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 581 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 583 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 584 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 585 | mOnPointerDownToken = newToken; |
| 586 | } |
| 587 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 588 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 589 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 590 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 591 | mPointerCaptureChangedCondition.notify_all(); |
| 592 | } |
| 593 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 594 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 595 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 596 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 597 | mDropTargetWindowToken = token; |
| 598 | } |
| 599 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 600 | void assertFilterInputEventWasCalledInternal( |
| 601 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 602 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 603 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 604 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 605 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 606 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 607 | }; |
| 608 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 609 | // --- InputDispatcherTest --- |
| 610 | |
| 611 | class InputDispatcherTest : public testing::Test { |
| 612 | protected: |
| 613 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 614 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 615 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 616 | void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 617 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 618 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 619 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 620 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 621 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 624 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 625 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 626 | mFakePolicy.clear(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 627 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 628 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 629 | |
| 630 | /** |
| 631 | * Used for debugging when writing the test |
| 632 | */ |
| 633 | void dumpDispatcherState() { |
| 634 | std::string dump; |
| 635 | mDispatcher->dump(dump); |
| 636 | std::stringstream ss(dump); |
| 637 | std::string to; |
| 638 | |
| 639 | while (std::getline(ss, to, '\n')) { |
| 640 | ALOGE("%s", to.c_str()); |
| 641 | } |
| 642 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 643 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 644 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 645 | FocusRequest request; |
| 646 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 647 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 648 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 649 | request.displayId = window->getInfo()->displayId; |
| 650 | mDispatcher->setFocusedWindow(request); |
| 651 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 652 | }; |
| 653 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 654 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 655 | KeyEvent event; |
| 656 | |
| 657 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 658 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 659 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 660 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 661 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 662 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 663 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 664 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 665 | << "Should reject key events with undefined action."; |
| 666 | |
| 667 | // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 668 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 669 | INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 670 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 671 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 672 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 673 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 674 | << "Should reject key events with ACTION_MULTIPLE."; |
| 675 | } |
| 676 | |
| 677 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 678 | MotionEvent event; |
| 679 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 680 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 681 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 682 | pointerProperties[i].clear(); |
| 683 | pointerProperties[i].id = i; |
| 684 | pointerCoords[i].clear(); |
| 685 | } |
| 686 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 687 | // Some constants commonly used below |
| 688 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 689 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 690 | constexpr int32_t metaState = AMETA_NONE; |
| 691 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 692 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 693 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 694 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 695 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 696 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 697 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 698 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 699 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 700 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 701 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 702 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 703 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 704 | << "Should reject motion events with undefined action."; |
| 705 | |
| 706 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 707 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 708 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 709 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 710 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 711 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 712 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 713 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 714 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 715 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | << "Should reject motion events with pointer down index too large."; |
| 717 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 718 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 719 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 720 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 721 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 722 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 723 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 724 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 725 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 726 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 727 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | << "Should reject motion events with pointer down index too small."; |
| 729 | |
| 730 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 731 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 732 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 733 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 734 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 735 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 736 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 737 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 738 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 739 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 740 | << "Should reject motion events with pointer up index too large."; |
| 741 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 742 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 743 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 744 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 745 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 746 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 747 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 748 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 749 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 750 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 751 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | << "Should reject motion events with pointer up index too small."; |
| 753 | |
| 754 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 755 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 756 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 757 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 758 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 759 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 760 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 761 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 762 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 763 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 764 | << "Should reject motion events with 0 pointers."; |
| 765 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 766 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 767 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 768 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 769 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 770 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 771 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 772 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 773 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 774 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 776 | |
| 777 | // Rejects motion events with invalid pointer ids. |
| 778 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 779 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 780 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 781 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 782 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 783 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 784 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 785 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 786 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 787 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 788 | << "Should reject motion events with pointer ids less than 0."; |
| 789 | |
| 790 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 791 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 792 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 793 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 794 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 795 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 796 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 797 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 798 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 799 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 800 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 801 | |
| 802 | // Rejects motion events with duplicate pointer ids. |
| 803 | pointerProperties[0].id = 1; |
| 804 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 805 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 806 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 807 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 808 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 809 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 810 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 811 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 812 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 813 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 814 | << "Should reject motion events with duplicate pointer ids."; |
| 815 | } |
| 816 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 817 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 818 | |
| 819 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 820 | constexpr nsecs_t eventTime = 20; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 821 | NotifyConfigurationChangedArgs args(/*id=*/10, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 822 | mDispatcher->notifyConfigurationChanged(&args); |
| 823 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 824 | |
| 825 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 826 | } |
| 827 | |
| 828 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 829 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 830 | /*switchMask=*/2); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 831 | mDispatcher->notifySwitch(&args); |
| 832 | |
| 833 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 834 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 835 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 836 | } |
| 837 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 838 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 839 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 840 | // Default input dispatching timeout if there is no focused application or paused window |
| 841 | // from which to determine an appropriate dispatching timeout. |
| 842 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 843 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 844 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 845 | |
| 846 | class FakeApplicationHandle : public InputApplicationHandle { |
| 847 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 848 | FakeApplicationHandle() { |
| 849 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 850 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 851 | mInfo.dispatchingTimeoutMillis = |
| 852 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 853 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 854 | virtual ~FakeApplicationHandle() {} |
| 855 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 856 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 857 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 858 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 859 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 860 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 861 | }; |
| 862 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 863 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 864 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 865 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 866 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 867 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 870 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 871 | InputEvent* event; |
| 872 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 873 | if (!consumeSeq) { |
| 874 | return nullptr; |
| 875 | } |
| 876 | finishEvent(*consumeSeq); |
| 877 | return event; |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * Receive an event without acknowledging it. |
| 882 | * Return the sequence number that could later be used to send finished signal. |
| 883 | */ |
| 884 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 885 | uint32_t consumeSeq; |
| 886 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 887 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 888 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 889 | status_t status = WOULD_BLOCK; |
| 890 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 891 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 892 | &event); |
| 893 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 894 | if (elapsed > 100ms) { |
| 895 | break; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | if (status == WOULD_BLOCK) { |
| 900 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 901 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | if (status != OK) { |
| 905 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 906 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 907 | } |
| 908 | if (event == nullptr) { |
| 909 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 910 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 911 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 912 | if (outEvent != nullptr) { |
| 913 | *outEvent = event; |
| 914 | } |
| 915 | return consumeSeq; |
| 916 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 917 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 918 | /** |
| 919 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 920 | */ |
| 921 | void finishEvent(uint32_t consumeSeq) { |
| 922 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 923 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 924 | } |
| 925 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 926 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 927 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 928 | ASSERT_EQ(OK, status); |
| 929 | } |
| 930 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 931 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 932 | std::optional<int32_t> expectedDisplayId, |
| 933 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 934 | InputEvent* event = consume(); |
| 935 | |
| 936 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 937 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 938 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 939 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 940 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 941 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 942 | if (expectedDisplayId.has_value()) { |
| 943 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 944 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 945 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 946 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 947 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 948 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 949 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 950 | if (expectedFlags.has_value()) { |
| 951 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 952 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 953 | break; |
| 954 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 955 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 956 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 957 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 958 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 959 | if (expectedFlags.has_value()) { |
| 960 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 961 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 962 | break; |
| 963 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 964 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 965 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 966 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 967 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 968 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 969 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 970 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 971 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 972 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 973 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 974 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 975 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 976 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 977 | } |
| 978 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 979 | MotionEvent* consumeMotion() { |
| 980 | InputEvent* event = consume(); |
| 981 | |
| 982 | if (event == nullptr) { |
| 983 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 984 | return nullptr; |
| 985 | } |
| 986 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 987 | if (event->getType() != InputEventType::MOTION) { |
| 988 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 989 | return nullptr; |
| 990 | } |
| 991 | return static_cast<MotionEvent*>(event); |
| 992 | } |
| 993 | |
| 994 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 995 | MotionEvent* motionEvent = consumeMotion(); |
| 996 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 997 | ASSERT_THAT(*motionEvent, matcher); |
| 998 | } |
| 999 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1000 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 1001 | InputEvent* event = consume(); |
| 1002 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1003 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1004 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1005 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1006 | |
| 1007 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1008 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1009 | |
| 1010 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1011 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1014 | void consumeCaptureEvent(bool hasCapture) { |
| 1015 | const InputEvent* event = consume(); |
| 1016 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1017 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1018 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1019 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1020 | |
| 1021 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1022 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1023 | |
| 1024 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1025 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1026 | } |
| 1027 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1028 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1029 | const InputEvent* event = consume(); |
| 1030 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1031 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1032 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1033 | |
| 1034 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1035 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1036 | |
| 1037 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1038 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1039 | EXPECT_EQ(x, dragEvent.getX()); |
| 1040 | EXPECT_EQ(y, dragEvent.getY()); |
| 1041 | } |
| 1042 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1043 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1044 | const InputEvent* event = consume(); |
| 1045 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1046 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1047 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1048 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1049 | |
| 1050 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1051 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1052 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1053 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1054 | } |
| 1055 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1056 | void assertNoEvents() { |
| 1057 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1058 | if (event == nullptr) { |
| 1059 | return; |
| 1060 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1061 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1062 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1063 | ADD_FAILURE() << "Received key event " |
| 1064 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1065 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1066 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1067 | ADD_FAILURE() << "Received motion event " |
| 1068 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1069 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1070 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1071 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1072 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1073 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1074 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1075 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1076 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1077 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1078 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1079 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1080 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1081 | } |
| 1082 | FAIL() << mName.c_str() |
| 1083 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1087 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1088 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1089 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1090 | protected: |
| 1091 | std::unique_ptr<InputConsumer> mConsumer; |
| 1092 | PreallocatedInputEventFactory mEventFactory; |
| 1093 | |
| 1094 | std::string mName; |
| 1095 | }; |
| 1096 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1097 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1098 | public: |
| 1099 | static const int32_t WIDTH = 600; |
| 1100 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1101 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1102 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1103 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1104 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1105 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1106 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1107 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1108 | dispatcher->createInputChannel(name); |
| 1109 | token = (*channel)->getConnectionToken(); |
| 1110 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | inputApplicationHandle->updateInfo(); |
| 1114 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1115 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1116 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1117 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1118 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1119 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1120 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1121 | mInfo.frameLeft = 0; |
| 1122 | mInfo.frameTop = 0; |
| 1123 | mInfo.frameRight = WIDTH; |
| 1124 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1125 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1126 | mInfo.globalScaleFactor = 1.0; |
| 1127 | mInfo.touchableRegion.clear(); |
| 1128 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1129 | mInfo.ownerPid = WINDOW_PID; |
| 1130 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1131 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1132 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1135 | sp<FakeWindowHandle> clone( |
| 1136 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1137 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1138 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1139 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1140 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1141 | return handle; |
| 1142 | } |
| 1143 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1144 | void setTouchable(bool touchable) { |
| 1145 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1146 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1147 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1148 | void setFocusable(bool focusable) { |
| 1149 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1150 | } |
| 1151 | |
| 1152 | void setVisible(bool visible) { |
| 1153 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1154 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1155 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1156 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1157 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1160 | void setPaused(bool paused) { |
| 1161 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1162 | } |
| 1163 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1164 | void setPreventSplitting(bool preventSplitting) { |
| 1165 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | void setSlippery(bool slippery) { |
| 1169 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1170 | } |
| 1171 | |
| 1172 | void setWatchOutsideTouch(bool watchOutside) { |
| 1173 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1174 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1175 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1176 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1177 | |
| 1178 | void setInterceptsStylus(bool interceptsStylus) { |
| 1179 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1180 | } |
| 1181 | |
| 1182 | void setDropInput(bool dropInput) { |
| 1183 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1184 | } |
| 1185 | |
| 1186 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1187 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1188 | } |
| 1189 | |
| 1190 | void setNoInputChannel(bool noInputChannel) { |
| 1191 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1192 | } |
| 1193 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1194 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1195 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1196 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1197 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1198 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1199 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1200 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1201 | mInfo.frameLeft = frame.left; |
| 1202 | mInfo.frameTop = frame.top; |
| 1203 | mInfo.frameRight = frame.right; |
| 1204 | mInfo.frameBottom = frame.bottom; |
| 1205 | mInfo.touchableRegion.clear(); |
| 1206 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1207 | |
| 1208 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1209 | ui::Transform translate; |
| 1210 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1211 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1214 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1215 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1216 | void setIsWallpaper(bool isWallpaper) { |
| 1217 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1218 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1219 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1220 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1221 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1222 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1223 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1224 | void setTrustedOverlay(bool trustedOverlay) { |
| 1225 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1226 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1227 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1228 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1229 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1230 | } |
| 1231 | |
| 1232 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1233 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1234 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1235 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1236 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1237 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1240 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1241 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1244 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1245 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1246 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1247 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1251 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1252 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1253 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1257 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1258 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1259 | } |
| 1260 | |
| 1261 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1262 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1263 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1264 | expectedFlags); |
| 1265 | } |
| 1266 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1267 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1268 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1269 | int32_t expectedFlags = 0) { |
| 1270 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1271 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1272 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1276 | int32_t expectedFlags = 0) { |
| 1277 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1278 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1279 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1283 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1284 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1285 | expectedFlags); |
| 1286 | } |
| 1287 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1288 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1289 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1290 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1291 | expectedFlags); |
| 1292 | } |
| 1293 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1294 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1295 | int32_t expectedFlags = 0) { |
| 1296 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1297 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1298 | ASSERT_EQ(InputEventType::MOTION, event->getType()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1299 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1300 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1301 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1302 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1303 | } |
| 1304 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1305 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1306 | ASSERT_NE(mInputReceiver, nullptr) |
| 1307 | << "Cannot consume events from a window with no receiver"; |
| 1308 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1309 | } |
| 1310 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1311 | void consumeCaptureEvent(bool hasCapture) { |
| 1312 | ASSERT_NE(mInputReceiver, nullptr) |
| 1313 | << "Cannot consume events from a window with no receiver"; |
| 1314 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1315 | } |
| 1316 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1317 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1318 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1319 | 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] | 1320 | ASSERT_THAT(*motionEvent, matcher); |
| 1321 | } |
| 1322 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1323 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1324 | std::optional<int32_t> expectedDisplayId, |
| 1325 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1326 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1327 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1328 | expectedFlags); |
| 1329 | } |
| 1330 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1331 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1332 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1333 | } |
| 1334 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1335 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1336 | ASSERT_NE(mInputReceiver, nullptr) |
| 1337 | << "Cannot consume events from a window with no receiver"; |
| 1338 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1339 | } |
| 1340 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1341 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1342 | if (mInputReceiver == nullptr) { |
| 1343 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1344 | return std::nullopt; |
| 1345 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1346 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | void finishEvent(uint32_t sequenceNum) { |
| 1350 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1351 | mInputReceiver->finishEvent(sequenceNum); |
| 1352 | } |
| 1353 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1354 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1355 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1356 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1357 | } |
| 1358 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1359 | InputEvent* consume() { |
| 1360 | if (mInputReceiver == nullptr) { |
| 1361 | return nullptr; |
| 1362 | } |
| 1363 | return mInputReceiver->consume(); |
| 1364 | } |
| 1365 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1366 | MotionEvent* consumeMotion() { |
| 1367 | InputEvent* event = consume(); |
| 1368 | if (event == nullptr) { |
| 1369 | ADD_FAILURE() << "Consume failed : no event"; |
| 1370 | return nullptr; |
| 1371 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 1372 | if (event->getType() != InputEventType::MOTION) { |
| 1373 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1374 | return nullptr; |
| 1375 | } |
| 1376 | return static_cast<MotionEvent*>(event); |
| 1377 | } |
| 1378 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1379 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1380 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1381 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1382 | return; // Can't receive events if the window does not have input channel |
| 1383 | } |
| 1384 | ASSERT_NE(nullptr, mInputReceiver) |
| 1385 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1386 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1387 | } |
| 1388 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1389 | sp<IBinder> getToken() { return mInfo.token; } |
| 1390 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1391 | const std::string& getName() { return mName; } |
| 1392 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1393 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1394 | mInfo.ownerPid = ownerPid; |
| 1395 | mInfo.ownerUid = ownerUid; |
| 1396 | } |
| 1397 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1398 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1399 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1400 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1401 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1402 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1403 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1404 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1405 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1406 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1407 | 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] | 1408 | }; |
| 1409 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1410 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1411 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1412 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1413 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1414 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1415 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1416 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1417 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1418 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1419 | KeyEvent event; |
| 1420 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1421 | |
| 1422 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1423 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1424 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1425 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1426 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1427 | if (!allowKeyRepeat) { |
| 1428 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1429 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1430 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1431 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1434 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1435 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1436 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1439 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1440 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1441 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1442 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1443 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1444 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1445 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1446 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1447 | } |
| 1448 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1449 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1450 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1451 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1454 | class PointerBuilder { |
| 1455 | public: |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1456 | PointerBuilder(int32_t id, ToolType toolType) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1457 | mProperties.clear(); |
| 1458 | mProperties.id = id; |
| 1459 | mProperties.toolType = toolType; |
| 1460 | mCoords.clear(); |
| 1461 | } |
| 1462 | |
| 1463 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 1464 | |
| 1465 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 1466 | |
| 1467 | PointerBuilder& axis(int32_t axis, float value) { |
| 1468 | mCoords.setAxisValue(axis, value); |
| 1469 | return *this; |
| 1470 | } |
| 1471 | |
| 1472 | PointerProperties buildProperties() const { return mProperties; } |
| 1473 | |
| 1474 | PointerCoords buildCoords() const { return mCoords; } |
| 1475 | |
| 1476 | private: |
| 1477 | PointerProperties mProperties; |
| 1478 | PointerCoords mCoords; |
| 1479 | }; |
| 1480 | |
| 1481 | class MotionEventBuilder { |
| 1482 | public: |
| 1483 | MotionEventBuilder(int32_t action, int32_t source) { |
| 1484 | mAction = action; |
| 1485 | mSource = source; |
| 1486 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1487 | mDownTime = mEventTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1490 | MotionEventBuilder& deviceId(int32_t deviceId) { |
| 1491 | mDeviceId = deviceId; |
| 1492 | return *this; |
| 1493 | } |
| 1494 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1495 | MotionEventBuilder& downTime(nsecs_t downTime) { |
| 1496 | mDownTime = downTime; |
| 1497 | return *this; |
| 1498 | } |
| 1499 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1500 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 1501 | mEventTime = eventTime; |
| 1502 | return *this; |
| 1503 | } |
| 1504 | |
| 1505 | MotionEventBuilder& displayId(int32_t displayId) { |
| 1506 | mDisplayId = displayId; |
| 1507 | return *this; |
| 1508 | } |
| 1509 | |
| 1510 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 1511 | mActionButton = actionButton; |
| 1512 | return *this; |
| 1513 | } |
| 1514 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 1515 | MotionEventBuilder& buttonState(int32_t buttonState) { |
| 1516 | mButtonState = buttonState; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1517 | return *this; |
| 1518 | } |
| 1519 | |
| 1520 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1521 | mRawXCursorPosition = rawXCursorPosition; |
| 1522 | return *this; |
| 1523 | } |
| 1524 | |
| 1525 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1526 | mRawYCursorPosition = rawYCursorPosition; |
| 1527 | return *this; |
| 1528 | } |
| 1529 | |
| 1530 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 1531 | mPointers.push_back(pointer); |
| 1532 | return *this; |
| 1533 | } |
| 1534 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1535 | MotionEventBuilder& addFlag(uint32_t flags) { |
| 1536 | mFlags |= flags; |
| 1537 | return *this; |
| 1538 | } |
| 1539 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1540 | MotionEvent build() { |
| 1541 | std::vector<PointerProperties> pointerProperties; |
| 1542 | std::vector<PointerCoords> pointerCoords; |
| 1543 | for (const PointerBuilder& pointer : mPointers) { |
| 1544 | pointerProperties.push_back(pointer.buildProperties()); |
| 1545 | pointerCoords.push_back(pointer.buildCoords()); |
| 1546 | } |
| 1547 | |
| 1548 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1549 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1550 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1551 | mPointers.size() == 1) { |
| 1552 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1553 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1554 | } |
| 1555 | |
| 1556 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1557 | ui::Transform identityTransform; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1558 | event.initialize(InputEvent::nextId(), mDeviceId, mSource, mDisplayId, INVALID_HMAC, |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1559 | mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1560 | mButtonState, MotionClassification::NONE, identityTransform, |
| 1561 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1562 | mRawYCursorPosition, identityTransform, mDownTime, mEventTime, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 1563 | mPointers.size(), pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1564 | |
| 1565 | return event; |
| 1566 | } |
| 1567 | |
| 1568 | private: |
| 1569 | int32_t mAction; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 1570 | int32_t mDeviceId = DEVICE_ID; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1571 | int32_t mSource; |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1572 | nsecs_t mDownTime; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1573 | nsecs_t mEventTime; |
| 1574 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1575 | int32_t mActionButton{0}; |
| 1576 | int32_t mButtonState{0}; |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 1577 | int32_t mFlags{0}; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1578 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1579 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1580 | |
| 1581 | std::vector<PointerBuilder> mPointers; |
| 1582 | }; |
| 1583 | |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1584 | class MotionArgsBuilder { |
| 1585 | public: |
| 1586 | MotionArgsBuilder(int32_t action, int32_t source) { |
| 1587 | mAction = action; |
| 1588 | mSource = source; |
| 1589 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1590 | mDownTime = mEventTime; |
| 1591 | } |
| 1592 | |
| 1593 | MotionArgsBuilder& deviceId(int32_t deviceId) { |
| 1594 | mDeviceId = deviceId; |
| 1595 | return *this; |
| 1596 | } |
| 1597 | |
| 1598 | MotionArgsBuilder& downTime(nsecs_t downTime) { |
| 1599 | mDownTime = downTime; |
| 1600 | return *this; |
| 1601 | } |
| 1602 | |
| 1603 | MotionArgsBuilder& eventTime(nsecs_t eventTime) { |
| 1604 | mEventTime = eventTime; |
| 1605 | return *this; |
| 1606 | } |
| 1607 | |
| 1608 | MotionArgsBuilder& displayId(int32_t displayId) { |
| 1609 | mDisplayId = displayId; |
| 1610 | return *this; |
| 1611 | } |
| 1612 | |
| 1613 | MotionArgsBuilder& policyFlags(int32_t policyFlags) { |
| 1614 | mPolicyFlags = policyFlags; |
| 1615 | return *this; |
| 1616 | } |
| 1617 | |
| 1618 | MotionArgsBuilder& actionButton(int32_t actionButton) { |
| 1619 | mActionButton = actionButton; |
| 1620 | return *this; |
| 1621 | } |
| 1622 | |
| 1623 | MotionArgsBuilder& buttonState(int32_t buttonState) { |
| 1624 | mButtonState = buttonState; |
| 1625 | return *this; |
| 1626 | } |
| 1627 | |
| 1628 | MotionArgsBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 1629 | mRawXCursorPosition = rawXCursorPosition; |
| 1630 | return *this; |
| 1631 | } |
| 1632 | |
| 1633 | MotionArgsBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 1634 | mRawYCursorPosition = rawYCursorPosition; |
| 1635 | return *this; |
| 1636 | } |
| 1637 | |
| 1638 | MotionArgsBuilder& pointer(PointerBuilder pointer) { |
| 1639 | mPointers.push_back(pointer); |
| 1640 | return *this; |
| 1641 | } |
| 1642 | |
| 1643 | MotionArgsBuilder& addFlag(uint32_t flags) { |
| 1644 | mFlags |= flags; |
| 1645 | return *this; |
| 1646 | } |
| 1647 | |
| 1648 | NotifyMotionArgs build() { |
| 1649 | std::vector<PointerProperties> pointerProperties; |
| 1650 | std::vector<PointerCoords> pointerCoords; |
| 1651 | for (const PointerBuilder& pointer : mPointers) { |
| 1652 | pointerProperties.push_back(pointer.buildProperties()); |
| 1653 | pointerCoords.push_back(pointer.buildCoords()); |
| 1654 | } |
| 1655 | |
| 1656 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 1657 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 1658 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 1659 | mPointers.size() == 1) { |
| 1660 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 1661 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 1662 | } |
| 1663 | |
| 1664 | NotifyMotionArgs args(InputEvent::nextId(), mEventTime, /*readTime=*/mEventTime, mDeviceId, |
| 1665 | mSource, mDisplayId, mPolicyFlags, mAction, mActionButton, mFlags, |
| 1666 | AMETA_NONE, mButtonState, MotionClassification::NONE, /*edgeFlags=*/0, |
| 1667 | mPointers.size(), pointerProperties.data(), pointerCoords.data(), |
| 1668 | /*xPrecision=*/0, /*yPrecision=*/0, mRawXCursorPosition, |
| 1669 | mRawYCursorPosition, mDownTime, /*videoFrames=*/{}); |
| 1670 | |
| 1671 | return args; |
| 1672 | } |
| 1673 | |
| 1674 | private: |
| 1675 | int32_t mAction; |
| 1676 | int32_t mDeviceId = DEVICE_ID; |
| 1677 | int32_t mSource; |
| 1678 | nsecs_t mDownTime; |
| 1679 | nsecs_t mEventTime; |
| 1680 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1681 | int32_t mPolicyFlags = DEFAULT_POLICY_FLAGS; |
| 1682 | int32_t mActionButton{0}; |
| 1683 | int32_t mButtonState{0}; |
| 1684 | int32_t mFlags{0}; |
| 1685 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1686 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1687 | |
| 1688 | std::vector<PointerBuilder> mPointers; |
| 1689 | }; |
| 1690 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1691 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1692 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1693 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1694 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1695 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1696 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1697 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1700 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1701 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1702 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1703 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1704 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1705 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1706 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1707 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1708 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1709 | MotionEvent event = MotionEventBuilder(action, source) |
| 1710 | .displayId(displayId) |
| 1711 | .eventTime(eventTime) |
| 1712 | .rawXCursorPosition(cursorPosition.x) |
| 1713 | .rawYCursorPosition(cursorPosition.y) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1714 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1715 | .x(position.x) |
| 1716 | .y(position.y)) |
| 1717 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1718 | |
| 1719 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1720 | return injectMotionEvent(dispatcher, event, injectionTimeout, injectionMode, targetUid, |
| 1721 | policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1722 | } |
| 1723 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1724 | static InputEventInjectionResult injectMotionDown( |
| 1725 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1726 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1727 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1730 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1731 | int32_t source, int32_t displayId, |
| 1732 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1733 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1736 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1737 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1738 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1739 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1740 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1741 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1742 | |
| 1743 | return args; |
| 1744 | } |
| 1745 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1746 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1747 | const std::vector<PointF>& points) { |
| 1748 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1749 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1750 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1751 | } |
| 1752 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1753 | PointerProperties pointerProperties[pointerCount]; |
| 1754 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1755 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1756 | for (size_t i = 0; i < pointerCount; i++) { |
| 1757 | pointerProperties[i].clear(); |
| 1758 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1759 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1760 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1761 | pointerCoords[i].clear(); |
| 1762 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1763 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1764 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1765 | |
| 1766 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1767 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1768 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1769 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1770 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1771 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1772 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1773 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1774 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1775 | |
| 1776 | return args; |
| 1777 | } |
| 1778 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1779 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1780 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1781 | } |
| 1782 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1783 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1784 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1785 | } |
| 1786 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1787 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1788 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1789 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1792 | /** |
| 1793 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1794 | * broken channel. |
| 1795 | */ |
| 1796 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1797 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1798 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1799 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1800 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1801 | |
| 1802 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1803 | |
| 1804 | // Window closes its channel, but the window remains. |
| 1805 | window->destroyReceiver(); |
| 1806 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1807 | } |
| 1808 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1809 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1810 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1811 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1812 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1813 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1814 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1816 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1817 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1818 | |
| 1819 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1820 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1821 | } |
| 1822 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1823 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1824 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1825 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1826 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1827 | |
| 1828 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1829 | // Inject a MotionEvent to an unknown display. |
| 1830 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1831 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1832 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1833 | |
| 1834 | // Window should receive motion event. |
| 1835 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1836 | } |
| 1837 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1838 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1839 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1840 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1841 | * called twice. |
| 1842 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1843 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1844 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1845 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1846 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1847 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1848 | |
| 1849 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1850 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1851 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1852 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1853 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1854 | |
| 1855 | // Window should receive motion event. |
| 1856 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1857 | } |
| 1858 | |
| 1859 | /** |
| 1860 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1861 | */ |
| 1862 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1863 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1864 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1865 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1866 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1867 | |
| 1868 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1869 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1871 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1872 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1873 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1874 | |
| 1875 | // Window should receive motion event. |
| 1876 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1877 | } |
| 1878 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1879 | // The foreground window should receive the first touch down event. |
| 1880 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1881 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1882 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1883 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1884 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1885 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1886 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1887 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1889 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1890 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1891 | |
| 1892 | // Top window should receive the touch down event. Second window should not receive anything. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1893 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1894 | windowSecond->assertNoEvents(); |
| 1895 | } |
| 1896 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1897 | /** |
| 1898 | * Two windows: A top window, and a wallpaper behind the window. |
| 1899 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1900 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1901 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1902 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1903 | */ |
| 1904 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1905 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1906 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1907 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1908 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1909 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1910 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1911 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1912 | |
| 1913 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1914 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1915 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1916 | {100, 200})) |
| 1917 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1918 | |
| 1919 | // Both foreground window and its wallpaper should receive the touch down |
| 1920 | foregroundWindow->consumeMotionDown(); |
| 1921 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1922 | |
| 1923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1924 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1925 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1926 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1927 | |
| 1928 | foregroundWindow->consumeMotionMove(); |
| 1929 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1930 | |
| 1931 | // Now the foreground window goes away, but the wallpaper stays |
| 1932 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1933 | foregroundWindow->consumeMotionCancel(); |
| 1934 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1935 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1936 | } |
| 1937 | |
| 1938 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1939 | * Two fingers down on the window, and lift off the first finger. |
| 1940 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1941 | * contains a single pointer. |
| 1942 | */ |
| 1943 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1944 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1945 | sp<FakeWindowHandle> window = |
| 1946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1947 | |
| 1948 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1949 | NotifyMotionArgs args; |
| 1950 | // First touch pointer down on right window |
| 1951 | mDispatcher->notifyMotion(&( |
| 1952 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1953 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1954 | .build())); |
| 1955 | // Second touch pointer down |
| 1956 | mDispatcher->notifyMotion(&( |
| 1957 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1958 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1959 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1960 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1961 | .build())); |
| 1962 | // First touch pointer lifts. The second one remains down |
| 1963 | mDispatcher->notifyMotion(&( |
| 1964 | args = MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1965 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1966 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1967 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1968 | .build())); |
| 1969 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1970 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1971 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1972 | |
| 1973 | // Remove the window. The gesture should be canceled |
| 1974 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 1975 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1976 | window->consumeMotionEvent( |
| 1977 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1978 | } |
| 1979 | |
| 1980 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1981 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1982 | * with the following differences: |
| 1983 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1984 | * clean up the connection. |
| 1985 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1986 | * Ensure that there's no crash in the dispatcher. |
| 1987 | */ |
| 1988 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1989 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1990 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1991 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1992 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1993 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1994 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1995 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1996 | |
| 1997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1998 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1999 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2000 | {100, 200})) |
| 2001 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2002 | |
| 2003 | // Both foreground window and its wallpaper should receive the touch down |
| 2004 | foregroundWindow->consumeMotionDown(); |
| 2005 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2006 | |
| 2007 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2008 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2009 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 2010 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2011 | |
| 2012 | foregroundWindow->consumeMotionMove(); |
| 2013 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2014 | |
| 2015 | // Wallpaper closes its channel, but the window remains. |
| 2016 | wallpaperWindow->destroyReceiver(); |
| 2017 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 2018 | |
| 2019 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 2020 | // is no longer valid. |
| 2021 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 2022 | foregroundWindow->consumeMotionCancel(); |
| 2023 | } |
| 2024 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2025 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 2026 | public ::testing::WithParamInterface<bool> {}; |
| 2027 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 2028 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 2029 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2030 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 2031 | * The top window gets a multitouch gesture. |
| 2032 | * Ensure that wallpaper gets the same gesture. |
| 2033 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2034 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2035 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2036 | sp<FakeWindowHandle> foregroundWindow = |
| 2037 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 2038 | foregroundWindow->setDupTouchToWallpaper(true); |
| 2039 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2040 | |
| 2041 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2042 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2043 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2044 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2045 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2046 | |
| 2047 | // Touch down on top window |
| 2048 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2049 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2050 | {100, 100})) |
| 2051 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2052 | |
| 2053 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2054 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2055 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2056 | |
| 2057 | // Second finger down on the top window |
| 2058 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2059 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2060 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2061 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2062 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2063 | .build(); |
| 2064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2065 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2066 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2067 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2068 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2069 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 2070 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2071 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2072 | |
| 2073 | const MotionEvent secondFingerUpEvent = |
| 2074 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2075 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2076 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2077 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2078 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2079 | .build(); |
| 2080 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2081 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 2082 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2083 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2084 | foregroundWindow->consumeMotionPointerUp(0); |
| 2085 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2086 | |
| 2087 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2088 | injectMotionEvent(mDispatcher, |
| 2089 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2090 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2091 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2092 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 2093 | .pointer(PointerBuilder(/* id */ 1, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2094 | ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2095 | .x(100) |
| 2096 | .y(100)) |
| 2097 | .build(), |
| 2098 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2099 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2100 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2101 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | /** |
| 2105 | * Two windows: a window on the left and window on the right. |
| 2106 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 2107 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 2108 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 2109 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 2110 | * ACTION_POINTER_DOWN(1). |
| 2111 | */ |
| 2112 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 2113 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2114 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2115 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2116 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2117 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2118 | |
| 2119 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2120 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2121 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2122 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2123 | |
| 2124 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2125 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2126 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2127 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2128 | |
| 2129 | mDispatcher->setInputWindows( |
| 2130 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 2131 | |
| 2132 | // Touch down on left window |
| 2133 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2134 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2135 | {100, 100})) |
| 2136 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2137 | |
| 2138 | // Both foreground window and its wallpaper should receive the touch down |
| 2139 | leftWindow->consumeMotionDown(); |
| 2140 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2141 | |
| 2142 | // Second finger down on the right window |
| 2143 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2144 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2145 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2146 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2147 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2148 | .build(); |
| 2149 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2150 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 2151 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2152 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2153 | |
| 2154 | leftWindow->consumeMotionMove(); |
| 2155 | // Since the touch is split, right window gets ACTION_DOWN |
| 2156 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2157 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2158 | expectedWallpaperFlags); |
| 2159 | |
| 2160 | // Now, leftWindow, which received the first finger, disappears. |
| 2161 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 2162 | leftWindow->consumeMotionCancel(); |
| 2163 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2164 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2165 | |
| 2166 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2167 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2168 | const MotionEvent secondFingerMoveEvent = |
| 2169 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2170 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2171 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2172 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2173 | .build(); |
| 2174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2175 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2176 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2177 | rightWindow->consumeMotionMove(); |
| 2178 | |
| 2179 | leftWindow->assertNoEvents(); |
| 2180 | rightWindow->assertNoEvents(); |
| 2181 | wallpaperWindow->assertNoEvents(); |
| 2182 | } |
| 2183 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2184 | /** |
| 2185 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2186 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2187 | * The right window should receive ACTION_DOWN. |
| 2188 | */ |
| 2189 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2190 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2191 | sp<FakeWindowHandle> leftWindow = |
| 2192 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2193 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2194 | leftWindow->setDupTouchToWallpaper(true); |
| 2195 | leftWindow->setSlippery(true); |
| 2196 | |
| 2197 | sp<FakeWindowHandle> rightWindow = |
| 2198 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2199 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2200 | |
| 2201 | sp<FakeWindowHandle> wallpaperWindow = |
| 2202 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2203 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2204 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2205 | mDispatcher->setInputWindows( |
| 2206 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2207 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2208 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2209 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2210 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2211 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2212 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2213 | |
| 2214 | // Both foreground window and its wallpaper should receive the touch down |
| 2215 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2216 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2217 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2218 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2220 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2221 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2222 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2223 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2224 | leftWindow->consumeMotionCancel(); |
| 2225 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2226 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2229 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2230 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2231 | * interactive, it might stop sending this flag. |
| 2232 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2233 | * to have a consistent input stream. |
| 2234 | * |
| 2235 | * Test procedure: |
| 2236 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2237 | * DOWN (new gesture). |
| 2238 | * |
| 2239 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2240 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2241 | * |
| 2242 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2243 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2244 | */ |
| 2245 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2246 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2247 | sp<FakeWindowHandle> spyWindow = |
| 2248 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2249 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2250 | spyWindow->setTrustedOverlay(true); |
| 2251 | spyWindow->setSpy(true); |
| 2252 | |
| 2253 | sp<FakeWindowHandle> window = |
| 2254 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2255 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2256 | |
| 2257 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2258 | const int32_t touchDeviceId = 4; |
| 2259 | NotifyMotionArgs args; |
| 2260 | |
| 2261 | // Two pointers down |
| 2262 | mDispatcher->notifyMotion(&( |
| 2263 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2264 | .deviceId(touchDeviceId) |
| 2265 | .policyFlags(DEFAULT_POLICY_FLAGS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2266 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2267 | .build())); |
| 2268 | |
| 2269 | mDispatcher->notifyMotion(&( |
| 2270 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2271 | .deviceId(touchDeviceId) |
| 2272 | .policyFlags(DEFAULT_POLICY_FLAGS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2273 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2274 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2275 | .build())); |
| 2276 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2277 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2278 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2279 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2280 | |
| 2281 | // Cancel the current gesture. Send the cancel without the default policy flags. |
| 2282 | mDispatcher->notifyMotion(&( |
| 2283 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2284 | .deviceId(touchDeviceId) |
| 2285 | .policyFlags(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2286 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2287 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2288 | .build())); |
| 2289 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2290 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2291 | |
| 2292 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2293 | // follows, so we keep it here to model the actual listener behaviour more closely. |
| 2294 | NotifyDeviceResetArgs resetArgs; |
| 2295 | resetArgs.id = 1; // arbitrary id |
| 2296 | resetArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2297 | resetArgs.deviceId = touchDeviceId; |
| 2298 | mDispatcher->notifyDeviceReset(&resetArgs); |
| 2299 | |
| 2300 | // Start new gesture |
| 2301 | mDispatcher->notifyMotion(&( |
| 2302 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2303 | .deviceId(touchDeviceId) |
| 2304 | .policyFlags(DEFAULT_POLICY_FLAGS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2305 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2306 | .build())); |
| 2307 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2308 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2309 | |
| 2310 | // No more events |
| 2311 | spyWindow->assertNoEvents(); |
| 2312 | window->assertNoEvents(); |
| 2313 | } |
| 2314 | |
| 2315 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2316 | * Two windows: a window on the left and a window on the right. |
| 2317 | * Mouse is hovered from the right window into the left window. |
| 2318 | * Next, we tap on the left window, where the cursor was last seen. |
| 2319 | * The second tap is done onto the right window. |
| 2320 | * The mouse and tap are from two different devices. |
| 2321 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2322 | * explicitly helps during debugging. |
| 2323 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2324 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2325 | */ |
| 2326 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2327 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2328 | sp<FakeWindowHandle> leftWindow = |
| 2329 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2330 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2331 | |
| 2332 | sp<FakeWindowHandle> rightWindow = |
| 2333 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2334 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2335 | |
| 2336 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2337 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2338 | // stale. |
| 2339 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2340 | const int32_t mouseDeviceId = 6; |
| 2341 | const int32_t touchDeviceId = 4; |
| 2342 | // Move the cursor from right |
| 2343 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2344 | injectMotionEvent(mDispatcher, |
| 2345 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2346 | AINPUT_SOURCE_MOUSE) |
| 2347 | .deviceId(mouseDeviceId) |
| 2348 | .downTime(baseTime + 10) |
| 2349 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2350 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2351 | .x(300) |
| 2352 | .y(100)) |
| 2353 | .build())); |
| 2354 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2355 | |
| 2356 | // .. to the left window |
| 2357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2358 | injectMotionEvent(mDispatcher, |
| 2359 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2360 | AINPUT_SOURCE_MOUSE) |
| 2361 | .deviceId(mouseDeviceId) |
| 2362 | .downTime(baseTime + 10) |
| 2363 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2364 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2365 | .x(110) |
| 2366 | .y(100)) |
| 2367 | .build())); |
| 2368 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2369 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2370 | // Now tap the left window |
| 2371 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2372 | injectMotionEvent(mDispatcher, |
| 2373 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2374 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2375 | .deviceId(touchDeviceId) |
| 2376 | .downTime(baseTime + 40) |
| 2377 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2378 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2379 | .x(100) |
| 2380 | .y(100)) |
| 2381 | .build())); |
| 2382 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2383 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2384 | |
| 2385 | // release tap |
| 2386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2387 | injectMotionEvent(mDispatcher, |
| 2388 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2389 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2390 | .deviceId(touchDeviceId) |
| 2391 | .downTime(baseTime + 40) |
| 2392 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2393 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2394 | .x(100) |
| 2395 | .y(100)) |
| 2396 | .build())); |
| 2397 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2398 | |
| 2399 | // Tap the window on the right |
| 2400 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2401 | injectMotionEvent(mDispatcher, |
| 2402 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2403 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2404 | .deviceId(touchDeviceId) |
| 2405 | .downTime(baseTime + 60) |
| 2406 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2407 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2408 | .x(300) |
| 2409 | .y(100)) |
| 2410 | .build())); |
| 2411 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2412 | |
| 2413 | // release tap |
| 2414 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2415 | injectMotionEvent(mDispatcher, |
| 2416 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2417 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2418 | .deviceId(touchDeviceId) |
| 2419 | .downTime(baseTime + 60) |
| 2420 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2421 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2422 | .x(300) |
| 2423 | .y(100)) |
| 2424 | .build())); |
| 2425 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2426 | |
| 2427 | // No more events |
| 2428 | leftWindow->assertNoEvents(); |
| 2429 | rightWindow->assertNoEvents(); |
| 2430 | } |
| 2431 | |
| 2432 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2433 | * Two windows: a window on the left and a window on the right. |
| 2434 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2435 | * down. Then, on the left window, also place second touch pointer down. |
| 2436 | * This test tries to reproduce a crash. |
| 2437 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2438 | */ |
| 2439 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { |
| 2440 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2441 | sp<FakeWindowHandle> leftWindow = |
| 2442 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2443 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2444 | |
| 2445 | sp<FakeWindowHandle> rightWindow = |
| 2446 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2447 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2448 | |
| 2449 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2450 | |
| 2451 | const int32_t touchDeviceId = 4; |
| 2452 | const int32_t mouseDeviceId = 6; |
| 2453 | NotifyMotionArgs args; |
| 2454 | |
| 2455 | // Start hovering over the left window |
| 2456 | mDispatcher->notifyMotion(&( |
| 2457 | args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2458 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2459 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2460 | .build())); |
| 2461 | leftWindow->consumeMotionEvent( |
| 2462 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2463 | |
| 2464 | // Mouse down on left window |
| 2465 | mDispatcher->notifyMotion(&( |
| 2466 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2467 | .deviceId(mouseDeviceId) |
| 2468 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2469 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2470 | .build())); |
| 2471 | |
| 2472 | leftWindow->consumeMotionEvent( |
| 2473 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2474 | leftWindow->consumeMotionEvent( |
| 2475 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2476 | |
| 2477 | mDispatcher->notifyMotion(&( |
| 2478 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2479 | .deviceId(mouseDeviceId) |
| 2480 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2481 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2482 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2483 | .build())); |
| 2484 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2485 | |
| 2486 | // First touch pointer down on right window |
| 2487 | mDispatcher->notifyMotion(&( |
| 2488 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2489 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2490 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2491 | .build())); |
| 2492 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2493 | |
| 2494 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2495 | |
| 2496 | // Second touch pointer down on left window |
| 2497 | mDispatcher->notifyMotion(&( |
| 2498 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2499 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2500 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2501 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2502 | .build())); |
| 2503 | leftWindow->consumeMotionEvent( |
| 2504 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2505 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2506 | // current implementation. |
| 2507 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2508 | rightWindow->consumeMotionEvent( |
| 2509 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2510 | |
| 2511 | leftWindow->assertNoEvents(); |
| 2512 | rightWindow->assertNoEvents(); |
| 2513 | } |
| 2514 | |
| 2515 | /** |
| 2516 | * On a single window, use two different devices: mouse and touch. |
| 2517 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2518 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2519 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2520 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2521 | * represent a new gesture. |
| 2522 | */ |
| 2523 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { |
| 2524 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2525 | sp<FakeWindowHandle> window = |
| 2526 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2527 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2528 | |
| 2529 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2530 | |
| 2531 | const int32_t touchDeviceId = 4; |
| 2532 | const int32_t mouseDeviceId = 6; |
| 2533 | NotifyMotionArgs args; |
| 2534 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2535 | // First touch pointer down |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2536 | mDispatcher->notifyMotion(&( |
| 2537 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2538 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2539 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2540 | .build())); |
| 2541 | // Second touch pointer down |
| 2542 | mDispatcher->notifyMotion(&( |
| 2543 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2544 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2545 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2546 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2547 | .build())); |
| 2548 | // First touch pointer lifts. The second one remains down |
| 2549 | mDispatcher->notifyMotion(&( |
| 2550 | args = MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2551 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2552 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2553 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2554 | .build())); |
| 2555 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2556 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2557 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2558 | |
| 2559 | // Mouse down. The touch should be canceled |
| 2560 | mDispatcher->notifyMotion(&( |
| 2561 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2562 | .deviceId(mouseDeviceId) |
| 2563 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2564 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2565 | .build())); |
| 2566 | |
| 2567 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2568 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2569 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2570 | |
| 2571 | mDispatcher->notifyMotion(&( |
| 2572 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2573 | .deviceId(mouseDeviceId) |
| 2574 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2575 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2576 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2577 | .build())); |
| 2578 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2579 | |
| 2580 | // Second touch pointer down. |
| 2581 | mDispatcher->notifyMotion(&( |
| 2582 | args = MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2583 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2584 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2585 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2586 | .build())); |
| 2587 | // The pointer_down event should be ignored |
| 2588 | window->assertNoEvents(); |
| 2589 | } |
| 2590 | |
| 2591 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2592 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 2593 | * the injected event. |
| 2594 | */ |
| 2595 | TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) { |
| 2596 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2597 | sp<FakeWindowHandle> window = |
| 2598 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2599 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2600 | |
| 2601 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2602 | |
| 2603 | const int32_t touchDeviceId = 4; |
| 2604 | NotifyMotionArgs args; |
| 2605 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 2606 | // completion. |
| 2607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2608 | injectMotionEvent(mDispatcher, |
| 2609 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2610 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2611 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2612 | .x(50) |
| 2613 | .y(50)) |
| 2614 | .build())); |
| 2615 | window->consumeMotionEvent( |
| 2616 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2617 | |
| 2618 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 2619 | // should be canceled and the new gesture should take over. |
| 2620 | mDispatcher->notifyMotion(&( |
| 2621 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2622 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2623 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2624 | .build())); |
| 2625 | |
| 2626 | window->consumeMotionEvent( |
| 2627 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2628 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2629 | } |
| 2630 | |
| 2631 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2632 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2633 | * |
| 2634 | * Two windows: a window on the left and a window on the right. |
| 2635 | * Mouse is hovered over the left window. |
| 2636 | * Next, we tap on the left window, where the cursor was last seen. |
| 2637 | * |
| 2638 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2639 | * the left window. |
| 2640 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2641 | * window (first), and then another on the left window (second). |
| 2642 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2643 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2644 | */ |
| 2645 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2646 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2647 | sp<FakeWindowHandle> leftWindow = |
| 2648 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2649 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2650 | |
| 2651 | sp<FakeWindowHandle> rightWindow = |
| 2652 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2653 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2654 | |
| 2655 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2656 | |
| 2657 | const int32_t mouseDeviceId = 6; |
| 2658 | const int32_t touchDeviceId = 4; |
| 2659 | // Hover over the left window. Keep the cursor there. |
| 2660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2661 | injectMotionEvent(mDispatcher, |
| 2662 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2663 | AINPUT_SOURCE_MOUSE) |
| 2664 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2665 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2666 | .x(50) |
| 2667 | .y(50)) |
| 2668 | .build())); |
| 2669 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2670 | |
| 2671 | // Tap on left window |
| 2672 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2673 | injectMotionEvent(mDispatcher, |
| 2674 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2675 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2676 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2677 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2678 | .x(100) |
| 2679 | .y(100)) |
| 2680 | .build())); |
| 2681 | |
| 2682 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2683 | injectMotionEvent(mDispatcher, |
| 2684 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2685 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2686 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2687 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2688 | .x(100) |
| 2689 | .y(100)) |
| 2690 | .build())); |
| 2691 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2692 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2693 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2694 | |
| 2695 | // First finger down on right window |
| 2696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2697 | injectMotionEvent(mDispatcher, |
| 2698 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2699 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2700 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2701 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2702 | .x(300) |
| 2703 | .y(100)) |
| 2704 | .build())); |
| 2705 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2706 | |
| 2707 | // Second finger down on the left window |
| 2708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2709 | injectMotionEvent(mDispatcher, |
| 2710 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2711 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2712 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2713 | .x(300) |
| 2714 | .y(100)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2715 | .pointer(PointerBuilder(1, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2716 | .x(100) |
| 2717 | .y(100)) |
| 2718 | .build())); |
| 2719 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2720 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2721 | |
| 2722 | // No more events |
| 2723 | leftWindow->assertNoEvents(); |
| 2724 | rightWindow->assertNoEvents(); |
| 2725 | } |
| 2726 | |
| 2727 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2728 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2729 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2730 | * touch is gone, stylus hovering should start working again. |
| 2731 | */ |
| 2732 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 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, 200, 200)); |
| 2737 | |
| 2738 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2739 | |
| 2740 | const int32_t stylusDeviceId = 5; |
| 2741 | const int32_t touchDeviceId = 4; |
| 2742 | // Start hovering with stylus |
| 2743 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2744 | injectMotionEvent(mDispatcher, |
| 2745 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2746 | AINPUT_SOURCE_STYLUS) |
| 2747 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2748 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2749 | .x(50) |
| 2750 | .y(50)) |
| 2751 | .build())); |
| 2752 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2753 | |
| 2754 | // Finger down on the window |
| 2755 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2756 | injectMotionEvent(mDispatcher, |
| 2757 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2758 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2759 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2760 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2761 | .x(100) |
| 2762 | .y(100)) |
| 2763 | .build())); |
| 2764 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2765 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2766 | |
| 2767 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2768 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2769 | injectMotionEvent(mDispatcher, |
| 2770 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2771 | AINPUT_SOURCE_STYLUS) |
| 2772 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2773 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2774 | .x(50) |
| 2775 | .y(50)) |
| 2776 | .build())); |
| 2777 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2778 | // is already down. |
| 2779 | |
| 2780 | // Lift up the finger |
| 2781 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2782 | injectMotionEvent(mDispatcher, |
| 2783 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2784 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2785 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2786 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2787 | .x(100) |
| 2788 | .y(100)) |
| 2789 | .build())); |
| 2790 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2791 | |
| 2792 | // Now that the touch is gone, stylus hovering should start working again |
| 2793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2794 | injectMotionEvent(mDispatcher, |
| 2795 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2796 | AINPUT_SOURCE_STYLUS) |
| 2797 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2798 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2799 | .x(50) |
| 2800 | .y(50)) |
| 2801 | .build())); |
| 2802 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2803 | // No more events |
| 2804 | window->assertNoEvents(); |
| 2805 | } |
| 2806 | |
| 2807 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2808 | * A spy window above a window with no input channel. |
| 2809 | * Start hovering with a stylus device, and then tap with it. |
| 2810 | * Ensure spy window receives the entire sequence. |
| 2811 | */ |
| 2812 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 2813 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2814 | sp<FakeWindowHandle> spyWindow = |
| 2815 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2816 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2817 | spyWindow->setTrustedOverlay(true); |
| 2818 | spyWindow->setSpy(true); |
| 2819 | sp<FakeWindowHandle> window = |
| 2820 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2821 | window->setNoInputChannel(true); |
| 2822 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2823 | |
| 2824 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2825 | |
| 2826 | NotifyMotionArgs args; |
| 2827 | |
| 2828 | // Start hovering with stylus |
| 2829 | mDispatcher->notifyMotion( |
| 2830 | &(args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2831 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2832 | .build())); |
| 2833 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2834 | // Stop hovering |
| 2835 | mDispatcher->notifyMotion( |
| 2836 | &(args = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2837 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2838 | .build())); |
| 2839 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2840 | |
| 2841 | // Stylus touches down |
| 2842 | mDispatcher->notifyMotion( |
| 2843 | &(args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2844 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2845 | .build())); |
| 2846 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2847 | |
| 2848 | // Stylus goes up |
| 2849 | mDispatcher->notifyMotion( |
| 2850 | &(args = MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2851 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2852 | .build())); |
| 2853 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 2854 | |
| 2855 | // Again hover |
| 2856 | mDispatcher->notifyMotion( |
| 2857 | &(args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2858 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2859 | .build())); |
| 2860 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2861 | // Stop hovering |
| 2862 | mDispatcher->notifyMotion( |
| 2863 | &(args = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2864 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2865 | .build())); |
| 2866 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2867 | |
| 2868 | // No more events |
| 2869 | spyWindow->assertNoEvents(); |
| 2870 | window->assertNoEvents(); |
| 2871 | } |
| 2872 | |
| 2873 | /** |
| 2874 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 2875 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 2876 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 2877 | * While the mouse is down, new move events from the touch device should be ignored. |
| 2878 | */ |
| 2879 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 2880 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2881 | sp<FakeWindowHandle> spyWindow = |
| 2882 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2883 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2884 | spyWindow->setTrustedOverlay(true); |
| 2885 | spyWindow->setSpy(true); |
| 2886 | sp<FakeWindowHandle> window = |
| 2887 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2888 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2889 | |
| 2890 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2891 | |
| 2892 | const int32_t mouseDeviceId = 7; |
| 2893 | const int32_t touchDeviceId = 4; |
| 2894 | NotifyMotionArgs args; |
| 2895 | |
| 2896 | // Hover a bit with mouse first |
| 2897 | mDispatcher->notifyMotion(&( |
| 2898 | args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2899 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2900 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2901 | .build())); |
| 2902 | spyWindow->consumeMotionEvent( |
| 2903 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2904 | window->consumeMotionEvent( |
| 2905 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2906 | |
| 2907 | // Start touching |
| 2908 | mDispatcher->notifyMotion( |
| 2909 | &(args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2910 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2911 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2912 | .build())); |
| 2913 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2914 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2915 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2916 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2917 | |
| 2918 | mDispatcher->notifyMotion( |
| 2919 | &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2920 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2921 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2922 | .build())); |
| 2923 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2924 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2925 | |
| 2926 | // Pilfer the stream |
| 2927 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2928 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2929 | |
| 2930 | mDispatcher->notifyMotion( |
| 2931 | &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2932 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2933 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2934 | .build())); |
| 2935 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2936 | |
| 2937 | // Mouse down |
| 2938 | mDispatcher->notifyMotion(&( |
| 2939 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2940 | .deviceId(mouseDeviceId) |
| 2941 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2942 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2943 | .build())); |
| 2944 | |
| 2945 | spyWindow->consumeMotionEvent( |
| 2946 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2947 | spyWindow->consumeMotionEvent( |
| 2948 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2949 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2950 | |
| 2951 | mDispatcher->notifyMotion(&( |
| 2952 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2953 | .deviceId(mouseDeviceId) |
| 2954 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2955 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2956 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2957 | .build())); |
| 2958 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2959 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2960 | |
| 2961 | // Mouse move! |
| 2962 | mDispatcher->notifyMotion(&( |
| 2963 | args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 2964 | .deviceId(mouseDeviceId) |
| 2965 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2966 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2967 | .build())); |
| 2968 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2969 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2970 | |
| 2971 | // Touch move! |
| 2972 | mDispatcher->notifyMotion( |
| 2973 | &(args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2974 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2975 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2976 | .build())); |
| 2977 | |
| 2978 | // No more events |
| 2979 | spyWindow->assertNoEvents(); |
| 2980 | window->assertNoEvents(); |
| 2981 | } |
| 2982 | |
| 2983 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2984 | * On the display, have a single window, and also an area where there's no window. |
| 2985 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2986 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2987 | */ |
| 2988 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2989 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2990 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2991 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2992 | |
| 2993 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
| 2994 | NotifyMotionArgs args; |
| 2995 | |
| 2996 | // Touch down on the empty space |
| 2997 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}}))); |
| 2998 | |
| 2999 | mDispatcher->waitForIdle(); |
| 3000 | window->assertNoEvents(); |
| 3001 | |
| 3002 | // Now touch down on the window with another pointer |
| 3003 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}}))); |
| 3004 | mDispatcher->waitForIdle(); |
| 3005 | window->consumeMotionDown(); |
| 3006 | } |
| 3007 | |
| 3008 | /** |
| 3009 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3010 | * non-touchable window. |
| 3011 | */ |
| 3012 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3013 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3014 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3015 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3016 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3017 | window1->setTouchable(false); |
| 3018 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3019 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3020 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3021 | |
| 3022 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 3023 | |
| 3024 | NotifyMotionArgs args; |
| 3025 | // Touch down on the non-touchable window |
| 3026 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 3027 | |
| 3028 | mDispatcher->waitForIdle(); |
| 3029 | window1->assertNoEvents(); |
| 3030 | window2->assertNoEvents(); |
| 3031 | |
| 3032 | // Now touch down on the window with another pointer |
| 3033 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 3034 | mDispatcher->waitForIdle(); |
| 3035 | window2->consumeMotionDown(); |
| 3036 | } |
| 3037 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3038 | /** |
| 3039 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3040 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3041 | */ |
| 3042 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3043 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3044 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3045 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3046 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3047 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3048 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3049 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3050 | |
| 3051 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 3052 | |
| 3053 | NotifyMotionArgs args; |
| 3054 | // Touch down on the first window |
| 3055 | mDispatcher->notifyMotion(&(args = generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}}))); |
| 3056 | |
| 3057 | mDispatcher->waitForIdle(); |
| 3058 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3059 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3060 | window2->assertNoEvents(); |
| 3061 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 3062 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 3063 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 3064 | |
| 3065 | // Now touch down on the window with another pointer |
| 3066 | mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}}))); |
| 3067 | mDispatcher->waitForIdle(); |
| 3068 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3069 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3070 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 3071 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 3072 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 3073 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 3074 | |
| 3075 | // Now move the pointer on the second window |
| 3076 | mDispatcher->notifyMotion( |
| 3077 | &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}}))); |
| 3078 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3079 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3080 | |
| 3081 | // Now add new touch down on the second window |
| 3082 | mDispatcher->notifyMotion( |
| 3083 | &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}}))); |
| 3084 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3085 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3086 | |
| 3087 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3088 | window1->consumeMotionMove(); |
| 3089 | window1->assertNoEvents(); |
| 3090 | |
| 3091 | // Now move the pointer on the first window |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 3092 | mDispatcher->notifyMotion(&( |
| 3093 | args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}}))); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3094 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3095 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3096 | |
| 3097 | mDispatcher->notifyMotion(&( |
| 3098 | args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}}))); |
| 3099 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3100 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3103 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3104 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3105 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3106 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3107 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3108 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3109 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3110 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3111 | |
| 3112 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3113 | |
| 3114 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 3115 | |
| 3116 | // 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] | 3117 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3118 | injectMotionEvent(mDispatcher, |
| 3119 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3120 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3121 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3122 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3123 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3124 | |
| 3125 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3126 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3127 | injectMotionEvent(mDispatcher, |
| 3128 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3129 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3130 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3131 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3132 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3133 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3134 | |
| 3135 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3136 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3137 | injectMotionEvent(mDispatcher, |
| 3138 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3139 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3140 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3141 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3142 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3143 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3144 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3145 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3146 | injectMotionEvent(mDispatcher, |
| 3147 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3148 | AINPUT_SOURCE_MOUSE) |
| 3149 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3150 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3151 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3152 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3153 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3154 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3155 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3156 | injectMotionEvent(mDispatcher, |
| 3157 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3158 | AINPUT_SOURCE_MOUSE) |
| 3159 | .buttonState(0) |
| 3160 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3161 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3162 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3163 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3164 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3165 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3166 | injectMotionEvent(mDispatcher, |
| 3167 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3168 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3169 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3170 | .build())); |
| 3171 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3172 | |
| 3173 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3175 | injectMotionEvent(mDispatcher, |
| 3176 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3177 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3178 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3179 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3180 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3181 | |
| 3182 | // No more events |
| 3183 | windowLeft->assertNoEvents(); |
| 3184 | windowRight->assertNoEvents(); |
| 3185 | } |
| 3186 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3187 | /** |
| 3188 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3189 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3190 | * currently active gesture should be canceled, and the new one should proceed. |
| 3191 | */ |
| 3192 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3193 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3194 | sp<FakeWindowHandle> window = |
| 3195 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3196 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3197 | |
| 3198 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3199 | |
| 3200 | const int32_t touchDeviceId = 4; |
| 3201 | const int32_t mouseDeviceId = 6; |
| 3202 | NotifyMotionArgs args; |
| 3203 | |
| 3204 | // Two pointers down |
| 3205 | mDispatcher->notifyMotion(&( |
| 3206 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3207 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3208 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3209 | .build())); |
| 3210 | |
| 3211 | mDispatcher->notifyMotion(&( |
| 3212 | args = MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3213 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3214 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3215 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3216 | .build())); |
| 3217 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3218 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3219 | |
| 3220 | // Inject a series of mouse events for a mouse click |
| 3221 | mDispatcher->notifyMotion(&( |
| 3222 | args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3223 | .deviceId(mouseDeviceId) |
| 3224 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3225 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3226 | .build())); |
| 3227 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3228 | WithPointerCount(2u))); |
| 3229 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3230 | |
| 3231 | mDispatcher->notifyMotion(&( |
| 3232 | args = MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3233 | .deviceId(mouseDeviceId) |
| 3234 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3235 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3236 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3237 | .build())); |
| 3238 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3239 | |
| 3240 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3241 | // already canceled gesture, it should be ignored. |
| 3242 | mDispatcher->notifyMotion(&( |
| 3243 | args = MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3244 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3245 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3246 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3247 | .build())); |
| 3248 | window->assertNoEvents(); |
| 3249 | } |
| 3250 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3251 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3252 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3253 | |
| 3254 | sp<FakeWindowHandle> spyWindow = |
| 3255 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3256 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3257 | spyWindow->setTrustedOverlay(true); |
| 3258 | spyWindow->setSpy(true); |
| 3259 | sp<FakeWindowHandle> window = |
| 3260 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3261 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3262 | |
| 3263 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3264 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3265 | |
| 3266 | // Send mouse cursor to the window |
| 3267 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3268 | injectMotionEvent(mDispatcher, |
| 3269 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3270 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3271 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3272 | .x(100) |
| 3273 | .y(100)) |
| 3274 | .build())); |
| 3275 | |
| 3276 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3277 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3278 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3279 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3280 | |
| 3281 | window->assertNoEvents(); |
| 3282 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3283 | } |
| 3284 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3285 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3286 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3287 | |
| 3288 | sp<FakeWindowHandle> spyWindow = |
| 3289 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3290 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3291 | spyWindow->setTrustedOverlay(true); |
| 3292 | spyWindow->setSpy(true); |
| 3293 | sp<FakeWindowHandle> window = |
| 3294 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3295 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3296 | |
| 3297 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3298 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3299 | |
| 3300 | // Send mouse cursor to the window |
| 3301 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3302 | injectMotionEvent(mDispatcher, |
| 3303 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3304 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3305 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3306 | .x(100) |
| 3307 | .y(100)) |
| 3308 | .build())); |
| 3309 | |
| 3310 | // Move mouse cursor |
| 3311 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3312 | injectMotionEvent(mDispatcher, |
| 3313 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3314 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3315 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3316 | .x(110) |
| 3317 | .y(110)) |
| 3318 | .build())); |
| 3319 | |
| 3320 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3321 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3322 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3323 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3324 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3325 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3326 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3327 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3328 | // Touch down on the window |
| 3329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3330 | injectMotionEvent(mDispatcher, |
| 3331 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3332 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3333 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3334 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3335 | .x(200) |
| 3336 | .y(200)) |
| 3337 | .build())); |
| 3338 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3339 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3340 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3341 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3342 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3343 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3344 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3345 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3346 | |
| 3347 | // pilfer the motion, retaining the gesture on the spy window. |
| 3348 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3349 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3350 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3351 | |
| 3352 | // Touch UP on the window |
| 3353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3354 | injectMotionEvent(mDispatcher, |
| 3355 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3356 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3357 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3358 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3359 | .x(200) |
| 3360 | .y(200)) |
| 3361 | .build())); |
| 3362 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3363 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3364 | |
| 3365 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3366 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3367 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3368 | |
| 3369 | // One more tap - DOWN |
| 3370 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3371 | injectMotionEvent(mDispatcher, |
| 3372 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3373 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3374 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3375 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3376 | .x(250) |
| 3377 | .y(250)) |
| 3378 | .build())); |
| 3379 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3380 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3381 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3382 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3383 | |
| 3384 | // Touch UP on the window |
| 3385 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3386 | injectMotionEvent(mDispatcher, |
| 3387 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3388 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3389 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3390 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3391 | .x(250) |
| 3392 | .y(250)) |
| 3393 | .build())); |
| 3394 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3395 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3396 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3397 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3398 | |
| 3399 | window->assertNoEvents(); |
| 3400 | spyWindow->assertNoEvents(); |
| 3401 | } |
| 3402 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3403 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3404 | // directly in this test. |
| 3405 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3406 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3407 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3408 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3409 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3410 | |
| 3411 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3412 | |
| 3413 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3414 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3416 | injectMotionEvent(mDispatcher, |
| 3417 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3418 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3419 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3420 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3421 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3422 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3423 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3424 | injectMotionEvent(mDispatcher, |
| 3425 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3426 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3427 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3428 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3429 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3430 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3431 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3432 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3433 | injectMotionEvent(mDispatcher, |
| 3434 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3435 | AINPUT_SOURCE_MOUSE) |
| 3436 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3437 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3438 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3439 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3440 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3441 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3442 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3443 | injectMotionEvent(mDispatcher, |
| 3444 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3445 | AINPUT_SOURCE_MOUSE) |
| 3446 | .buttonState(0) |
| 3447 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3448 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3449 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3450 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3451 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3453 | injectMotionEvent(mDispatcher, |
| 3454 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3455 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3456 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3457 | .build())); |
| 3458 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3459 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3461 | injectMotionEvent(mDispatcher, |
| 3462 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3463 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3464 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3465 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3466 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3467 | } |
| 3468 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3469 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3470 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3471 | * is generated. |
| 3472 | */ |
| 3473 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3474 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3475 | sp<FakeWindowHandle> window = |
| 3476 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3477 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3478 | |
| 3479 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3480 | |
| 3481 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3482 | |
| 3483 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3484 | injectMotionEvent(mDispatcher, |
| 3485 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3486 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3487 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3488 | .x(300) |
| 3489 | .y(400)) |
| 3490 | .build())); |
| 3491 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3492 | |
| 3493 | // Remove the window, but keep the channel. |
| 3494 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3495 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3496 | } |
| 3497 | |
| 3498 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3499 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3500 | */ |
| 3501 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3502 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3503 | sp<FakeWindowHandle> window = |
| 3504 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3505 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3506 | |
| 3507 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3508 | |
| 3509 | const int32_t mouseDeviceId = 7; |
| 3510 | const int32_t touchDeviceId = 4; |
| 3511 | NotifyMotionArgs args; |
| 3512 | |
| 3513 | // Start hovering with the mouse |
| 3514 | mDispatcher->notifyMotion( |
| 3515 | &(args = MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3516 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3517 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3518 | .build())); |
| 3519 | window->consumeMotionEvent( |
| 3520 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3521 | |
| 3522 | // Touch goes down |
| 3523 | mDispatcher->notifyMotion( |
| 3524 | &(args = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3525 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3526 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3527 | .build())); |
| 3528 | |
| 3529 | window->consumeMotionEvent( |
| 3530 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3531 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3532 | } |
| 3533 | |
| 3534 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3535 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3536 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3537 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3538 | */ |
| 3539 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3540 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3541 | sp<FakeWindowHandle> window = |
| 3542 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3543 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3544 | |
| 3545 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3546 | |
| 3547 | // Inject a hover_move from mouse. |
| 3548 | NotifyMotionArgs motionArgs = |
| 3549 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 3550 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3551 | motionArgs.xCursorPosition = 50; |
| 3552 | motionArgs.yCursorPosition = 50; |
| 3553 | mDispatcher->notifyMotion(&motionArgs); |
| 3554 | ASSERT_NO_FATAL_FAILURE( |
| 3555 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3556 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3557 | |
| 3558 | // Tap on the window |
| 3559 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3560 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 3561 | mDispatcher->notifyMotion(&motionArgs); |
| 3562 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3563 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3564 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3565 | |
| 3566 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3567 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3568 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3569 | |
| 3570 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3571 | ADISPLAY_ID_DEFAULT, {{10, 10}}); |
| 3572 | mDispatcher->notifyMotion(&motionArgs); |
| 3573 | ASSERT_NO_FATAL_FAILURE( |
| 3574 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3575 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3576 | } |
| 3577 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3578 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3579 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3580 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3581 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3582 | ADISPLAY_ID_DEFAULT); |
| 3583 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3584 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3585 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3586 | SECOND_DISPLAY_ID); |
| 3587 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3588 | |
| 3589 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3590 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3591 | |
| 3592 | // Set cursor position in window in default display and check that hover enter and move |
| 3593 | // events are generated. |
| 3594 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3595 | injectMotionEvent(mDispatcher, |
| 3596 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3597 | AINPUT_SOURCE_MOUSE) |
| 3598 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3599 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3600 | .x(300) |
| 3601 | .y(600)) |
| 3602 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3603 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3604 | |
| 3605 | // Remove all windows in secondary display and check that no event happens on window in |
| 3606 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3607 | mDispatcher->setInputWindows( |
| 3608 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3609 | windowDefaultDisplay->assertNoEvents(); |
| 3610 | |
| 3611 | // Move cursor position in window in default display and check that only hover move |
| 3612 | // event is generated and not hover enter event. |
| 3613 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3614 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3615 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3616 | injectMotionEvent(mDispatcher, |
| 3617 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3618 | AINPUT_SOURCE_MOUSE) |
| 3619 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3620 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3621 | .x(400) |
| 3622 | .y(700)) |
| 3623 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3624 | windowDefaultDisplay->consumeMotionEvent( |
| 3625 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3626 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3627 | windowDefaultDisplay->assertNoEvents(); |
| 3628 | } |
| 3629 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3630 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3631 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3632 | |
| 3633 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3634 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3635 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3636 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3637 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3638 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3639 | |
| 3640 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3641 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3642 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3643 | |
| 3644 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3645 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3647 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3648 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3649 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3650 | windowRight->assertNoEvents(); |
| 3651 | } |
| 3652 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3653 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3654 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3655 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3656 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3657 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3658 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3659 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3660 | setFocusedWindow(window); |
| 3661 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3662 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3663 | |
| 3664 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3665 | mDispatcher->notifyKey(&keyArgs); |
| 3666 | |
| 3667 | // Window should receive key down event. |
| 3668 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3669 | |
| 3670 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3671 | // on the app side. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3672 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3673 | mDispatcher->notifyDeviceReset(&args); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 3674 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3675 | AKEY_EVENT_FLAG_CANCELED); |
| 3676 | } |
| 3677 | |
| 3678 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3679 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3680 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3681 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3682 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3684 | |
| 3685 | NotifyMotionArgs motionArgs = |
| 3686 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3687 | ADISPLAY_ID_DEFAULT); |
| 3688 | mDispatcher->notifyMotion(&motionArgs); |
| 3689 | |
| 3690 | // Window should receive motion down event. |
| 3691 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3692 | |
| 3693 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 3694 | // on the app side. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 3695 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3696 | mDispatcher->notifyDeviceReset(&args); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3697 | window->consumeMotionEvent( |
| 3698 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3699 | } |
| 3700 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3701 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 3702 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3703 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3704 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3705 | window->setFocusable(true); |
| 3706 | |
| 3707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3708 | setFocusedWindow(window); |
| 3709 | |
| 3710 | window->consumeFocusEvent(true); |
| 3711 | |
| 3712 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3713 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3714 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3715 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
| 3716 | mDispatcher->notifyKey(&keyArgs); |
| 3717 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3718 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3719 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3720 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3721 | } |
| 3722 | |
| 3723 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3724 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3725 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3726 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3727 | window->setFocusable(true); |
| 3728 | |
| 3729 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3730 | setFocusedWindow(window); |
| 3731 | |
| 3732 | window->consumeFocusEvent(true); |
| 3733 | |
| 3734 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3735 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3736 | mFakePolicy->setInterceptKeyTimeout(150ms); |
| 3737 | mDispatcher->notifyKey(&keyDown); |
| 3738 | mDispatcher->notifyKey(&keyUp); |
| 3739 | |
| 3740 | // Window should receive key event immediately when same key up. |
| 3741 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3742 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3743 | } |
| 3744 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3745 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3746 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3747 | * WATCH_OUTSIDE_TOUCH. |
| 3748 | * Both windows are owned by the same UID. |
| 3749 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3750 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3751 | */ |
| 3752 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3753 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3754 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3755 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3756 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3757 | |
| 3758 | sp<FakeWindowHandle> outsideWindow = |
| 3759 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3760 | ADISPLAY_ID_DEFAULT); |
| 3761 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3762 | outsideWindow->setWatchOutsideTouch(true); |
| 3763 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3764 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3765 | |
| 3766 | // Tap on first window. |
| 3767 | NotifyMotionArgs motionArgs = |
| 3768 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3769 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 3770 | mDispatcher->notifyMotion(&motionArgs); |
| 3771 | window->consumeMotionDown(); |
| 3772 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3773 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3774 | outsideWindow->consumeMotionEvent( |
| 3775 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3776 | } |
| 3777 | |
| 3778 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3779 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3780 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3781 | * ACTION_OUTSIDE event is sent per gesture. |
| 3782 | */ |
| 3783 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3784 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 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 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3788 | window->setWatchOutsideTouch(true); |
| 3789 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3790 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3791 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3792 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3793 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3794 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3795 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3796 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3797 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3798 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3799 | |
| 3800 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
| 3801 | NotifyMotionArgs motionArgs = |
| 3802 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3803 | ADISPLAY_ID_DEFAULT, {PointF{-10, -10}}); |
| 3804 | mDispatcher->notifyMotion(&motionArgs); |
| 3805 | window->assertNoEvents(); |
| 3806 | secondWindow->assertNoEvents(); |
| 3807 | |
| 3808 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3809 | // Now, `window` should get ACTION_OUTSIDE. |
| 3810 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3811 | {PointF{-10, -10}, PointF{105, 105}}); |
| 3812 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3813 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3814 | window->consumeMotionEvent( |
| 3815 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3816 | secondWindow->consumeMotionDown(); |
| 3817 | thirdWindow->assertNoEvents(); |
| 3818 | |
| 3819 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3820 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
| 3821 | motionArgs = generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3822 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}}); |
| 3823 | mDispatcher->notifyMotion(&motionArgs); |
| 3824 | window->assertNoEvents(); |
| 3825 | secondWindow->consumeMotionMove(); |
| 3826 | thirdWindow->consumeMotionDown(); |
| 3827 | } |
| 3828 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3829 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3830 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3831 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3832 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3833 | window->setFocusable(true); |
| 3834 | |
| 3835 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 3836 | setFocusedWindow(window); |
| 3837 | |
| 3838 | window->consumeFocusEvent(true); |
| 3839 | |
| 3840 | NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3841 | NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3842 | mDispatcher->notifyKey(&keyDown); |
| 3843 | mDispatcher->notifyKey(&keyUp); |
| 3844 | |
| 3845 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3846 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3847 | |
| 3848 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
| 3849 | mDispatcher->onWindowInfosChanged({}, {}); |
| 3850 | |
| 3851 | window->consumeFocusEvent(false); |
| 3852 | |
| 3853 | mDispatcher->notifyKey(&keyDown); |
| 3854 | mDispatcher->notifyKey(&keyUp); |
| 3855 | window->assertNoEvents(); |
| 3856 | } |
| 3857 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3858 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3859 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3860 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3861 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3862 | // Ensure window is non-split and have some transform. |
| 3863 | window->setPreventSplitting(true); |
| 3864 | window->setWindowOffset(20, 40); |
| 3865 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {}); |
| 3866 | |
| 3867 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3868 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3869 | {50, 50})) |
| 3870 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3871 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3872 | |
| 3873 | const MotionEvent secondFingerDownEvent = |
| 3874 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3875 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3876 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3877 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 3878 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3879 | .build(); |
| 3880 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3881 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3882 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3883 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3884 | |
| 3885 | const MotionEvent* event = window->consumeMotion(); |
| 3886 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3887 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3888 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3889 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3890 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3891 | } |
| 3892 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3893 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3894 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 3895 | * |
| 3896 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 3897 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 3898 | * space. |
| 3899 | */ |
| 3900 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 3901 | public: |
| 3902 | void SetUp() override { |
| 3903 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3904 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 3908 | gui::DisplayInfo info; |
| 3909 | info.displayId = displayId; |
| 3910 | info.transform = transform; |
| 3911 | mDisplayInfos.push_back(std::move(info)); |
| 3912 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3913 | } |
| 3914 | |
| 3915 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 3916 | mWindowInfos.push_back(*windowHandle->getInfo()); |
| 3917 | mDispatcher->onWindowInfosChanged(mWindowInfos, mDisplayInfos); |
| 3918 | } |
| 3919 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3920 | void removeAllWindowsAndDisplays() { |
| 3921 | mDisplayInfos.clear(); |
| 3922 | mWindowInfos.clear(); |
| 3923 | } |
| 3924 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3925 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 3926 | // on the display. |
| 3927 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 3928 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 3929 | // respectively. |
| 3930 | ui::Transform displayTransform; |
| 3931 | displayTransform.set(2, 0, 0, 4); |
| 3932 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 3933 | |
| 3934 | std::shared_ptr<FakeApplicationHandle> application = |
| 3935 | std::make_shared<FakeApplicationHandle>(); |
| 3936 | |
| 3937 | // Add two windows to the display. Their frames are represented in the display space. |
| 3938 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3939 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3940 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3941 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 3942 | addWindow(firstWindow); |
| 3943 | |
| 3944 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3945 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3946 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3947 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 3948 | addWindow(secondWindow); |
| 3949 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 3950 | } |
| 3951 | |
| 3952 | private: |
| 3953 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 3954 | std::vector<gui::WindowInfo> mWindowInfos; |
| 3955 | }; |
| 3956 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3957 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3958 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3959 | // 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] | 3960 | // selected so that if the hit test was performed with the point and the bounds being in |
| 3961 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3962 | NotifyMotionArgs downMotionArgs = |
| 3963 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3964 | ADISPLAY_ID_DEFAULT, {PointF{75, 55}}); |
| 3965 | mDispatcher->notifyMotion(&downMotionArgs); |
| 3966 | |
| 3967 | firstWindow->consumeMotionDown(); |
| 3968 | secondWindow->assertNoEvents(); |
| 3969 | } |
| 3970 | |
| 3971 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 3972 | // the event should be treated as being in the logical display space. |
| 3973 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 3974 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3975 | // Send down to the first window. The point is represented in the logical display space. The |
| 3976 | // point is selected so that if the hit test was done in logical display space, then it would |
| 3977 | // end up in the incorrect window. |
| 3978 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3979 | PointF{75 * 2, 55 * 4}); |
| 3980 | |
| 3981 | firstWindow->consumeMotionDown(); |
| 3982 | secondWindow->assertNoEvents(); |
| 3983 | } |
| 3984 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3985 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 3986 | // event should be treated as being in the logical display space. |
| 3987 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 3988 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3989 | |
| 3990 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 3991 | ui::Transform injectedEventTransform; |
| 3992 | injectedEventTransform.set(matrix); |
| 3993 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 3994 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 3995 | |
| 3996 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3997 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3998 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3999 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4000 | .x(untransformedPoint.x) |
| 4001 | .y(untransformedPoint.y)) |
| 4002 | .build(); |
| 4003 | event.transform(matrix); |
| 4004 | |
| 4005 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 4006 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4007 | |
| 4008 | firstWindow->consumeMotionDown(); |
| 4009 | secondWindow->assertNoEvents(); |
| 4010 | } |
| 4011 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4012 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4013 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4014 | |
| 4015 | // Send down to the second window. |
| 4016 | NotifyMotionArgs downMotionArgs = |
| 4017 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4018 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}}); |
| 4019 | mDispatcher->notifyMotion(&downMotionArgs); |
| 4020 | |
| 4021 | firstWindow->assertNoEvents(); |
| 4022 | const MotionEvent* event = secondWindow->consumeMotion(); |
| 4023 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 4024 | |
| 4025 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 4026 | EXPECT_EQ(300, event->getRawX(0)); |
| 4027 | EXPECT_EQ(880, event->getRawY(0)); |
| 4028 | |
| 4029 | // Ensure that the x and y values are in the window's coordinate space. |
| 4030 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4031 | // the logical display space. This will be the origin of the window space. |
| 4032 | EXPECT_EQ(100, event->getX(0)); |
| 4033 | EXPECT_EQ(80, event->getY(0)); |
| 4034 | } |
| 4035 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4036 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4037 | class InputDispatcherDisplayOrientationFixture |
| 4038 | : public InputDispatcherDisplayProjectionTest, |
| 4039 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 4040 | |
| 4041 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 4042 | // in different locations on the display, specifically points close to the four corners of a |
| 4043 | // window. |
| 4044 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 4045 | constexpr static int32_t displayWidth = 400; |
| 4046 | constexpr static int32_t displayHeight = 800; |
| 4047 | |
| 4048 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4049 | |
| 4050 | const auto rotation = GetParam(); |
| 4051 | |
| 4052 | // Set up the display with the specified rotation. |
| 4053 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4054 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4055 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4056 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4057 | logicalDisplayWidth, logicalDisplayHeight); |
| 4058 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4059 | |
| 4060 | // Create a window with its bounds determined in the logical display. |
| 4061 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 4062 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4063 | sp<FakeWindowHandle> window = |
| 4064 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4065 | window->setFrame(frameInDisplay, displayTransform); |
| 4066 | addWindow(window); |
| 4067 | |
| 4068 | // The following points in logical display space should be inside the window. |
| 4069 | static const std::array<vec2, 4> insidePoints{ |
| 4070 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4071 | for (const auto pointInsideWindow : insidePoints) { |
| 4072 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4073 | const PointF pointInDisplaySpace{p.x, p.y}; |
| 4074 | const auto down = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4075 | ADISPLAY_ID_DEFAULT, {pointInDisplaySpace}); |
| 4076 | mDispatcher->notifyMotion(&down); |
| 4077 | window->consumeMotionDown(); |
| 4078 | |
| 4079 | const auto up = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4080 | ADISPLAY_ID_DEFAULT, {pointInDisplaySpace}); |
| 4081 | mDispatcher->notifyMotion(&up); |
| 4082 | window->consumeMotionUp(); |
| 4083 | } |
| 4084 | |
| 4085 | // The following points in logical display space should be outside the window. |
| 4086 | static const std::array<vec2, 5> outsidePoints{ |
| 4087 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4088 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4089 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4090 | const PointF pointInDisplaySpace{p.x, p.y}; |
| 4091 | const auto down = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4092 | ADISPLAY_ID_DEFAULT, {pointInDisplaySpace}); |
| 4093 | mDispatcher->notifyMotion(&down); |
| 4094 | |
| 4095 | const auto up = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4096 | ADISPLAY_ID_DEFAULT, {pointInDisplaySpace}); |
| 4097 | mDispatcher->notifyMotion(&up); |
| 4098 | } |
| 4099 | window->assertNoEvents(); |
| 4100 | } |
| 4101 | |
| 4102 | // Run the precision tests for all rotations. |
| 4103 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4104 | InputDispatcherDisplayOrientationFixture, |
| 4105 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4106 | ui::ROTATION_270), |
| 4107 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4108 | return ftl::enum_string(testParamInfo.param); |
| 4109 | }); |
| 4110 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4111 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4112 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4113 | |
| 4114 | class TransferTouchFixture : public InputDispatcherTest, |
| 4115 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4116 | |
| 4117 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4118 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4119 | |
| 4120 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4121 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4122 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4123 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4124 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4125 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4126 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4127 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4128 | sp<FakeWindowHandle> wallpaper = |
| 4129 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 4130 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4131 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4132 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4133 | |
| 4134 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4135 | NotifyMotionArgs downMotionArgs = |
| 4136 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4137 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4138 | mDispatcher->notifyMotion(&downMotionArgs); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4139 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4140 | // Only the first window should get the down event |
| 4141 | firstWindow->consumeMotionDown(); |
| 4142 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4143 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4144 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4145 | // Transfer touch to the second window |
| 4146 | TransferFunction f = GetParam(); |
| 4147 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4148 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4149 | // The first window gets cancel and the second gets down |
| 4150 | firstWindow->consumeMotionCancel(); |
| 4151 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4152 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4153 | |
| 4154 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4155 | NotifyMotionArgs upMotionArgs = |
| 4156 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4157 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4158 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4159 | // The first window gets no events and the second gets up |
| 4160 | firstWindow->assertNoEvents(); |
| 4161 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4162 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4163 | } |
| 4164 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4165 | /** |
| 4166 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 4167 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 4168 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 4169 | * natural to the user. |
| 4170 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 4171 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 4172 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 4173 | * the first window to the second. |
| 4174 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 4175 | * the other API, as well. |
| 4176 | */ |
| 4177 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 4178 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4179 | |
| 4180 | // Create a couple of windows + a spy window |
| 4181 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4182 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4183 | spyWindow->setTrustedOverlay(true); |
| 4184 | spyWindow->setSpy(true); |
| 4185 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4186 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4187 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4188 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4189 | |
| 4190 | // Add the windows to the dispatcher |
| 4191 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 4192 | |
| 4193 | // Send down to the first window |
| 4194 | NotifyMotionArgs downMotionArgs = |
| 4195 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4196 | ADISPLAY_ID_DEFAULT); |
| 4197 | mDispatcher->notifyMotion(&downMotionArgs); |
| 4198 | // Only the first window and spy should get the down event |
| 4199 | spyWindow->consumeMotionDown(); |
| 4200 | firstWindow->consumeMotionDown(); |
| 4201 | |
| 4202 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 4203 | // if f === 'transferTouch'. |
| 4204 | TransferFunction f = GetParam(); |
| 4205 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4206 | ASSERT_TRUE(success); |
| 4207 | // The first window gets cancel and the second gets down |
| 4208 | firstWindow->consumeMotionCancel(); |
| 4209 | secondWindow->consumeMotionDown(); |
| 4210 | |
| 4211 | // Send up event to the second window |
| 4212 | NotifyMotionArgs upMotionArgs = |
| 4213 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4214 | ADISPLAY_ID_DEFAULT); |
| 4215 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4216 | // The first window gets no events and the second+spy get up |
| 4217 | firstWindow->assertNoEvents(); |
| 4218 | spyWindow->consumeMotionUp(); |
| 4219 | secondWindow->consumeMotionUp(); |
| 4220 | } |
| 4221 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4222 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4223 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4224 | |
| 4225 | PointF touchPoint = {10, 10}; |
| 4226 | |
| 4227 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4228 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4229 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4230 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4231 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4232 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4233 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4234 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4235 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4236 | |
| 4237 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4238 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4239 | |
| 4240 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4241 | NotifyMotionArgs downMotionArgs = |
| 4242 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4243 | ADISPLAY_ID_DEFAULT, {touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4244 | mDispatcher->notifyMotion(&downMotionArgs); |
| 4245 | // Only the first window should get the down event |
| 4246 | firstWindow->consumeMotionDown(); |
| 4247 | secondWindow->assertNoEvents(); |
| 4248 | |
| 4249 | // Send pointer down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4250 | NotifyMotionArgs pointerDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4251 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4252 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4253 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 4254 | // Only the first window should get the pointer down event |
| 4255 | firstWindow->consumeMotionPointerDown(1); |
| 4256 | secondWindow->assertNoEvents(); |
| 4257 | |
| 4258 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4259 | TransferFunction f = GetParam(); |
| 4260 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4261 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4262 | // The first window gets cancel and the second gets down and pointer down |
| 4263 | firstWindow->consumeMotionCancel(); |
| 4264 | secondWindow->consumeMotionDown(); |
| 4265 | secondWindow->consumeMotionPointerDown(1); |
| 4266 | |
| 4267 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4268 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4269 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4270 | {touchPoint, touchPoint}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4271 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 4272 | // The first window gets nothing and the second gets pointer up |
| 4273 | firstWindow->assertNoEvents(); |
| 4274 | secondWindow->consumeMotionPointerUp(1); |
| 4275 | |
| 4276 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4277 | NotifyMotionArgs upMotionArgs = |
| 4278 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4279 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4280 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4281 | // The first window gets nothing and the second gets up |
| 4282 | firstWindow->assertNoEvents(); |
| 4283 | secondWindow->consumeMotionUp(); |
| 4284 | } |
| 4285 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4286 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 4287 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4288 | |
| 4289 | // Create a couple of windows |
| 4290 | sp<FakeWindowHandle> firstWindow = |
| 4291 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4292 | ADISPLAY_ID_DEFAULT); |
| 4293 | firstWindow->setDupTouchToWallpaper(true); |
| 4294 | sp<FakeWindowHandle> secondWindow = |
| 4295 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4296 | ADISPLAY_ID_DEFAULT); |
| 4297 | secondWindow->setDupTouchToWallpaper(true); |
| 4298 | |
| 4299 | sp<FakeWindowHandle> wallpaper1 = |
| 4300 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 4301 | wallpaper1->setIsWallpaper(true); |
| 4302 | |
| 4303 | sp<FakeWindowHandle> wallpaper2 = |
| 4304 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 4305 | wallpaper2->setIsWallpaper(true); |
| 4306 | // Add the windows to the dispatcher |
| 4307 | mDispatcher->setInputWindows( |
| 4308 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 4309 | |
| 4310 | // Send down to the first window |
| 4311 | NotifyMotionArgs downMotionArgs = |
| 4312 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4313 | ADISPLAY_ID_DEFAULT); |
| 4314 | mDispatcher->notifyMotion(&downMotionArgs); |
| 4315 | |
| 4316 | // Only the first window should get the down event |
| 4317 | firstWindow->consumeMotionDown(); |
| 4318 | secondWindow->assertNoEvents(); |
| 4319 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4320 | wallpaper2->assertNoEvents(); |
| 4321 | |
| 4322 | // Transfer touch focus to the second window |
| 4323 | TransferFunction f = GetParam(); |
| 4324 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4325 | ASSERT_TRUE(success); |
| 4326 | |
| 4327 | // The first window gets cancel and the second gets down |
| 4328 | firstWindow->consumeMotionCancel(); |
| 4329 | secondWindow->consumeMotionDown(); |
| 4330 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4331 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4332 | |
| 4333 | // Send up event to the second window |
| 4334 | NotifyMotionArgs upMotionArgs = |
| 4335 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4336 | ADISPLAY_ID_DEFAULT); |
| 4337 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4338 | // The first window gets no events and the second gets up |
| 4339 | firstWindow->assertNoEvents(); |
| 4340 | secondWindow->consumeMotionUp(); |
| 4341 | wallpaper1->assertNoEvents(); |
| 4342 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4343 | } |
| 4344 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4345 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 4346 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 4347 | // for the case where there are multiple pointers split across several windows. |
| 4348 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 4349 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4350 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4351 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4352 | return dispatcher->transferTouch(destChannelToken, |
| 4353 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4354 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4355 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4356 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4357 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4358 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4359 | })); |
| 4360 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4361 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4362 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4363 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4364 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4365 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4366 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4367 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4368 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4369 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4370 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4371 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4372 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4373 | |
| 4374 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4375 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4376 | |
| 4377 | PointF pointInFirst = {300, 200}; |
| 4378 | PointF pointInSecond = {300, 600}; |
| 4379 | |
| 4380 | // Send down to the first window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4381 | NotifyMotionArgs firstDownMotionArgs = |
| 4382 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4383 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4384 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 4385 | // Only the first window should get the down event |
| 4386 | firstWindow->consumeMotionDown(); |
| 4387 | secondWindow->assertNoEvents(); |
| 4388 | |
| 4389 | // Send down to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4390 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4391 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4392 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4393 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 4394 | // The first window gets a move and the second a down |
| 4395 | firstWindow->consumeMotionMove(); |
| 4396 | secondWindow->consumeMotionDown(); |
| 4397 | |
| 4398 | // Transfer touch focus to the second window |
| 4399 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4400 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 4401 | firstWindow->consumeMotionCancel(); |
| 4402 | secondWindow->consumeMotionPointerDown(1); |
| 4403 | |
| 4404 | // Send pointer up to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4405 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4406 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4407 | {pointInFirst, pointInSecond}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4408 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 4409 | // The first window gets nothing and the second gets pointer up |
| 4410 | firstWindow->assertNoEvents(); |
| 4411 | secondWindow->consumeMotionPointerUp(1); |
| 4412 | |
| 4413 | // Send up event to the second window |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4414 | NotifyMotionArgs upMotionArgs = |
| 4415 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4416 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4417 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4418 | // The first window gets nothing and the second gets up |
| 4419 | firstWindow->assertNoEvents(); |
| 4420 | secondWindow->consumeMotionUp(); |
| 4421 | } |
| 4422 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4423 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 4424 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 4425 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 4426 | // window should get nothing. |
| 4427 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 4428 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4429 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4430 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4431 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4432 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4433 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4434 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4435 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4436 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4437 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4438 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4439 | |
| 4440 | // Add the windows to the dispatcher |
| 4441 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4442 | |
| 4443 | PointF pointInFirst = {300, 200}; |
| 4444 | PointF pointInSecond = {300, 600}; |
| 4445 | |
| 4446 | // Send down to the first window |
| 4447 | NotifyMotionArgs firstDownMotionArgs = |
| 4448 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4449 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 4450 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 4451 | // Only the first window should get the down event |
| 4452 | firstWindow->consumeMotionDown(); |
| 4453 | secondWindow->assertNoEvents(); |
| 4454 | |
| 4455 | // Send down to the second window |
| 4456 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4457 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4458 | {pointInFirst, pointInSecond}); |
| 4459 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 4460 | // The first window gets a move and the second a down |
| 4461 | firstWindow->consumeMotionMove(); |
| 4462 | secondWindow->consumeMotionDown(); |
| 4463 | |
| 4464 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4465 | const bool transferred = |
| 4466 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4467 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 4468 | ASSERT_FALSE(transferred); |
| 4469 | firstWindow->assertNoEvents(); |
| 4470 | secondWindow->assertNoEvents(); |
| 4471 | |
| 4472 | // The rest of the dispatch should proceed as normal |
| 4473 | // Send pointer up to the second window |
| 4474 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4475 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4476 | {pointInFirst, pointInSecond}); |
| 4477 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 4478 | // The first window gets MOVE and the second gets pointer up |
| 4479 | firstWindow->consumeMotionMove(); |
| 4480 | secondWindow->consumeMotionUp(); |
| 4481 | |
| 4482 | // Send up event to the first window |
| 4483 | NotifyMotionArgs upMotionArgs = |
| 4484 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4485 | ADISPLAY_ID_DEFAULT); |
| 4486 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4487 | // The first window gets nothing and the second gets up |
| 4488 | firstWindow->consumeMotionUp(); |
| 4489 | secondWindow->assertNoEvents(); |
| 4490 | } |
| 4491 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4492 | // This case will create two windows and one mirrored window on the default display and mirror |
| 4493 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 4494 | // the windows info of second display before default display. |
| 4495 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 4496 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4497 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4498 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4499 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4500 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4501 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4502 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4503 | |
| 4504 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4505 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4506 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4507 | |
| 4508 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4509 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4510 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4511 | |
| 4512 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4513 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4514 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4515 | |
| 4516 | // Update window info, let it find window handle of second display first. |
| 4517 | mDispatcher->setInputWindows( |
| 4518 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4519 | {ADISPLAY_ID_DEFAULT, |
| 4520 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4521 | |
| 4522 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4523 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4524 | {50, 50})) |
| 4525 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4526 | |
| 4527 | // Window should receive motion event. |
| 4528 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4529 | |
| 4530 | // Transfer touch focus |
| 4531 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 4532 | secondWindowInPrimary->getToken())); |
| 4533 | // The first window gets cancel. |
| 4534 | firstWindowInPrimary->consumeMotionCancel(); |
| 4535 | secondWindowInPrimary->consumeMotionDown(); |
| 4536 | |
| 4537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4538 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4539 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4540 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4541 | firstWindowInPrimary->assertNoEvents(); |
| 4542 | secondWindowInPrimary->consumeMotionMove(); |
| 4543 | |
| 4544 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4545 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4546 | {150, 50})) |
| 4547 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4548 | firstWindowInPrimary->assertNoEvents(); |
| 4549 | secondWindowInPrimary->consumeMotionUp(); |
| 4550 | } |
| 4551 | |
| 4552 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 4553 | // 'transferTouch' api. |
| 4554 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 4555 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4556 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4557 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4558 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4559 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4560 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4561 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4562 | |
| 4563 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4564 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4565 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4566 | |
| 4567 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4568 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4569 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4570 | |
| 4571 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4572 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4573 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4574 | |
| 4575 | // Update window info, let it find window handle of second display first. |
| 4576 | mDispatcher->setInputWindows( |
| 4577 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4578 | {ADISPLAY_ID_DEFAULT, |
| 4579 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4580 | |
| 4581 | // Touch on second display. |
| 4582 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4583 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 4584 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4585 | |
| 4586 | // Window should receive motion event. |
| 4587 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4588 | |
| 4589 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4590 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4591 | |
| 4592 | // The first window gets cancel. |
| 4593 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4594 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4595 | |
| 4596 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4597 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4598 | SECOND_DISPLAY_ID, {150, 50})) |
| 4599 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4600 | firstWindowInPrimary->assertNoEvents(); |
| 4601 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 4602 | |
| 4603 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4604 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 4605 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4606 | firstWindowInPrimary->assertNoEvents(); |
| 4607 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 4608 | } |
| 4609 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4610 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4611 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4612 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4613 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4614 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4615 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4616 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4617 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4618 | |
| 4619 | window->consumeFocusEvent(true); |
| 4620 | |
| 4621 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4622 | mDispatcher->notifyKey(&keyArgs); |
| 4623 | |
| 4624 | // Window should receive key down event. |
| 4625 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4626 | } |
| 4627 | |
| 4628 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4629 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4630 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4631 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4632 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4633 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4634 | |
| 4635 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4636 | mDispatcher->notifyKey(&keyArgs); |
| 4637 | mDispatcher->waitForIdle(); |
| 4638 | |
| 4639 | window->assertNoEvents(); |
| 4640 | } |
| 4641 | |
| 4642 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4643 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4644 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4645 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4646 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4647 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4648 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4649 | |
| 4650 | // Send key |
| 4651 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4652 | mDispatcher->notifyKey(&keyArgs); |
| 4653 | // Send motion |
| 4654 | NotifyMotionArgs motionArgs = |
| 4655 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4656 | ADISPLAY_ID_DEFAULT); |
| 4657 | mDispatcher->notifyMotion(&motionArgs); |
| 4658 | |
| 4659 | // Window should receive only the motion event |
| 4660 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4661 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4662 | } |
| 4663 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4664 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4665 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4666 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4667 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4668 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4669 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4670 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4671 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4672 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4673 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4674 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4675 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4676 | |
| 4677 | // Add the windows to the dispatcher |
| 4678 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4679 | |
| 4680 | PointF pointInFirst = {300, 200}; |
| 4681 | PointF pointInSecond = {300, 600}; |
| 4682 | |
| 4683 | // Send down to the first window |
| 4684 | NotifyMotionArgs firstDownMotionArgs = |
| 4685 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4686 | ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 4687 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 4688 | // Only the first window should get the down event |
| 4689 | firstWindow->consumeMotionDown(); |
| 4690 | secondWindow->assertNoEvents(); |
| 4691 | |
| 4692 | // Send down to the second window |
| 4693 | NotifyMotionArgs secondDownMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4694 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4695 | {pointInFirst, pointInSecond}); |
| 4696 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 4697 | // The first window gets a move and the second a down |
| 4698 | firstWindow->consumeMotionMove(); |
| 4699 | secondWindow->consumeMotionDown(); |
| 4700 | |
| 4701 | // Send pointer cancel to the second window |
| 4702 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4703 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4704 | {pointInFirst, pointInSecond}); |
| 4705 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 4706 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 4707 | // The first window gets move and the second gets cancel. |
| 4708 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4709 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4710 | |
| 4711 | // Send up event. |
| 4712 | NotifyMotionArgs upMotionArgs = |
| 4713 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4714 | ADISPLAY_ID_DEFAULT); |
| 4715 | mDispatcher->notifyMotion(&upMotionArgs); |
| 4716 | // The first window gets up and the second gets nothing. |
| 4717 | firstWindow->consumeMotionUp(); |
| 4718 | secondWindow->assertNoEvents(); |
| 4719 | } |
| 4720 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4721 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 4722 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4723 | |
| 4724 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4725 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4726 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4727 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 4728 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 4729 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 4730 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4731 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4732 | window->assertNoEvents(); |
| 4733 | mDispatcher->waitForIdle(); |
| 4734 | } |
| 4735 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4736 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4737 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4738 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4739 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4740 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4741 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4742 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4745 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 4746 | |
| 4747 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 4748 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 4749 | expectedFlags); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4750 | } |
| 4751 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4752 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 4753 | |
| 4754 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 4755 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4756 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 4757 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4758 | expectedDisplayId, expectedFlags); |
| 4759 | } |
| 4760 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4761 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 4762 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4763 | expectedDisplayId, expectedFlags); |
| 4764 | } |
| 4765 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4766 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 4767 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4768 | expectedDisplayId, expectedFlags); |
| 4769 | } |
| 4770 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4771 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4772 | mInputReceiver->consumeMotionEvent( |
| 4773 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4774 | WithDisplayId(expectedDisplayId), |
| 4775 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4778 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 4779 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 4780 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 4781 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4782 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4785 | MotionEvent* consumeMotion() { |
| 4786 | InputEvent* event = mInputReceiver->consume(); |
| 4787 | if (!event) { |
| 4788 | ADD_FAILURE() << "No event was produced"; |
| 4789 | return nullptr; |
| 4790 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 4791 | if (event->getType() != InputEventType::MOTION) { |
| 4792 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4793 | return nullptr; |
| 4794 | } |
| 4795 | return static_cast<MotionEvent*>(event); |
| 4796 | } |
| 4797 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4798 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 4799 | |
| 4800 | private: |
| 4801 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4802 | }; |
| 4803 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4804 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 4805 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4806 | /** |
| 4807 | * Two entities that receive touch: A window, and a global monitor. |
| 4808 | * The touch goes to the window, and then the window disappears. |
| 4809 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 4810 | * for the monitor, as well. |
| 4811 | * 1. foregroundWindow |
| 4812 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 4813 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4814 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4815 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4816 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4817 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4818 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4819 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4820 | |
| 4821 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4823 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4824 | {100, 200})) |
| 4825 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4826 | |
| 4827 | // Both the foreground window and the global monitor should receive the touch down |
| 4828 | window->consumeMotionDown(); |
| 4829 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4830 | |
| 4831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4832 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4833 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4834 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4835 | |
| 4836 | window->consumeMotionMove(); |
| 4837 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4838 | |
| 4839 | // Now the foreground window goes away |
| 4840 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4841 | window->consumeMotionCancel(); |
| 4842 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 4843 | |
| 4844 | // If more events come in, there will be no more foreground window to send them to. This will |
| 4845 | // cause a cancel for the monitor, as well. |
| 4846 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4847 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4848 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 4849 | << "Injection should fail because the window was removed"; |
| 4850 | window->assertNoEvents(); |
| 4851 | // Global monitor now gets the cancel |
| 4852 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4853 | } |
| 4854 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4855 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4856 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4857 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4858 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4859 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4860 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4861 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4862 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4863 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4864 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4865 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4866 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4867 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4870 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 4871 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4872 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4873 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4874 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4875 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4876 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4877 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4879 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4880 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4881 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4882 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4883 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4884 | // Pilfer pointers from the monitor. |
| 4885 | // This should not do anything and the window should continue to receive events. |
| 4886 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4887 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4889 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4890 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4891 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4892 | |
| 4893 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4894 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4897 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4898 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4899 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4900 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4901 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4902 | window->setWindowOffset(20, 40); |
| 4903 | window->setWindowTransform(0, 1, -1, 0); |
| 4904 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4905 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4906 | |
| 4907 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4908 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4909 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4910 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4911 | MotionEvent* event = monitor.consumeMotion(); |
| 4912 | // Even though window has transform, gesture monitor must not. |
| 4913 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 4914 | } |
| 4915 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4916 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4917 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4918 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4919 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4920 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4921 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4922 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 4923 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4926 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4927 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4928 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4929 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4930 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4931 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4932 | |
| 4933 | NotifyMotionArgs motionArgs = |
| 4934 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4935 | ADISPLAY_ID_DEFAULT); |
| 4936 | |
| 4937 | mDispatcher->notifyMotion(&motionArgs); |
| 4938 | // Window should receive motion down event. |
| 4939 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4940 | |
| 4941 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 4942 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4943 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4944 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4945 | motionArgs.pointerCoords[0].getX() - 10); |
| 4946 | |
| 4947 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 4948 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4949 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4950 | } |
| 4951 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4952 | /** |
| 4953 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 4954 | * the device default right away. In the test scenario, we check both the default value, |
| 4955 | * and the action of enabling / disabling. |
| 4956 | */ |
| 4957 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4958 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4959 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4960 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4961 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4962 | |
| 4963 | // Set focused application. |
| 4964 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4965 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4966 | |
| 4967 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4968 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4969 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4970 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4971 | |
| 4972 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4973 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4974 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4975 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4976 | |
| 4977 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4978 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4979 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4980 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4981 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4982 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4983 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4984 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4985 | |
| 4986 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4987 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4988 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4989 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4990 | |
| 4991 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 4992 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4993 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 4994 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4995 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4996 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4997 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4998 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4999 | |
| 5000 | window->assertNoEvents(); |
| 5001 | } |
| 5002 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5003 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5004 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5005 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5006 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5007 | |
| 5008 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5009 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5010 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5011 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5012 | setFocusedWindow(window); |
| 5013 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5014 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5015 | |
| 5016 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5017 | mDispatcher->notifyKey(&keyArgs); |
| 5018 | |
| 5019 | InputEvent* event = window->consume(); |
| 5020 | ASSERT_NE(event, nullptr); |
| 5021 | |
| 5022 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5023 | ASSERT_NE(verified, nullptr); |
| 5024 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 5025 | |
| 5026 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 5027 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 5028 | ASSERT_EQ(keyArgs.source, verified->source); |
| 5029 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 5030 | |
| 5031 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 5032 | |
| 5033 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5034 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5035 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5036 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 5037 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 5038 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 5039 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 5040 | } |
| 5041 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5042 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5043 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5044 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5045 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5046 | |
| 5047 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5048 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5049 | ui::Transform transform; |
| 5050 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5051 | |
| 5052 | gui::DisplayInfo displayInfo; |
| 5053 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5054 | displayInfo.transform = transform; |
| 5055 | |
| 5056 | mDispatcher->onWindowInfosChanged({*window->getInfo()}, {displayInfo}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5057 | |
| 5058 | NotifyMotionArgs motionArgs = |
| 5059 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5060 | ADISPLAY_ID_DEFAULT); |
| 5061 | mDispatcher->notifyMotion(&motionArgs); |
| 5062 | |
| 5063 | InputEvent* event = window->consume(); |
| 5064 | ASSERT_NE(event, nullptr); |
| 5065 | |
| 5066 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5067 | ASSERT_NE(verified, nullptr); |
| 5068 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 5069 | |
| 5070 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 5071 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 5072 | EXPECT_EQ(motionArgs.source, verified->source); |
| 5073 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 5074 | |
| 5075 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 5076 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5077 | const vec2 rawXY = |
| 5078 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 5079 | motionArgs.pointerCoords[0].getXYValue()); |
| 5080 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 5081 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5082 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5083 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5084 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5085 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 5086 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 5087 | } |
| 5088 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5089 | /** |
| 5090 | * Ensure that separate calls to sign the same data are generating the same key. |
| 5091 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 5092 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 5093 | * tests. |
| 5094 | */ |
| 5095 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 5096 | KeyEvent event = getTestKeyEvent(); |
| 5097 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5098 | |
| 5099 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 5100 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5101 | ASSERT_EQ(hmac1, hmac2); |
| 5102 | } |
| 5103 | |
| 5104 | /** |
| 5105 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 5106 | */ |
| 5107 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 5108 | KeyEvent event = getTestKeyEvent(); |
| 5109 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5110 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 5111 | |
| 5112 | verifiedEvent.deviceId += 1; |
| 5113 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5114 | |
| 5115 | verifiedEvent.source += 1; |
| 5116 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5117 | |
| 5118 | verifiedEvent.eventTimeNanos += 1; |
| 5119 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5120 | |
| 5121 | verifiedEvent.displayId += 1; |
| 5122 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5123 | |
| 5124 | verifiedEvent.action += 1; |
| 5125 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5126 | |
| 5127 | verifiedEvent.downTimeNanos += 1; |
| 5128 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5129 | |
| 5130 | verifiedEvent.flags += 1; |
| 5131 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5132 | |
| 5133 | verifiedEvent.keyCode += 1; |
| 5134 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5135 | |
| 5136 | verifiedEvent.scanCode += 1; |
| 5137 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5138 | |
| 5139 | verifiedEvent.metaState += 1; |
| 5140 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5141 | |
| 5142 | verifiedEvent.repeatCount += 1; |
| 5143 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5144 | } |
| 5145 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5146 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 5147 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5148 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5149 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5150 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5151 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5152 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5153 | |
| 5154 | // Top window is also focusable but is not granted focus. |
| 5155 | windowTop->setFocusable(true); |
| 5156 | windowSecond->setFocusable(true); |
| 5157 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5158 | setFocusedWindow(windowSecond); |
| 5159 | |
| 5160 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5161 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5162 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5163 | |
| 5164 | // Focused window should receive event. |
| 5165 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5166 | windowTop->assertNoEvents(); |
| 5167 | } |
| 5168 | |
| 5169 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5170 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5171 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5172 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5173 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5174 | |
| 5175 | window->setFocusable(true); |
| 5176 | // Release channel for window is no longer valid. |
| 5177 | window->releaseChannel(); |
| 5178 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5179 | setFocusedWindow(window); |
| 5180 | |
| 5181 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5182 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5183 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5184 | |
| 5185 | // window channel is invalid, so it should not receive any input event. |
| 5186 | window->assertNoEvents(); |
| 5187 | } |
| 5188 | |
| 5189 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5190 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5191 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5192 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5193 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5194 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5195 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5196 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5197 | setFocusedWindow(window); |
| 5198 | |
| 5199 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5200 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5201 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5202 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5203 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5204 | window->assertNoEvents(); |
| 5205 | } |
| 5206 | |
| 5207 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5208 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5209 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5210 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5211 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5212 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5213 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5214 | |
| 5215 | windowTop->setFocusable(true); |
| 5216 | windowSecond->setFocusable(true); |
| 5217 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5218 | setFocusedWindow(windowTop); |
| 5219 | windowTop->consumeFocusEvent(true); |
| 5220 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5221 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
| 5222 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5223 | windowSecond->consumeFocusEvent(true); |
| 5224 | windowTop->consumeFocusEvent(false); |
| 5225 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5227 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5228 | |
| 5229 | // Focused window should receive event. |
| 5230 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5231 | } |
| 5232 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5233 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5234 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5235 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5236 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5237 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5238 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5239 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5240 | |
| 5241 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5242 | windowSecond->setFocusable(false); |
| 5243 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5244 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5245 | setFocusedWindow(windowTop); |
| 5246 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5247 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5248 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5249 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5250 | |
| 5251 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5252 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5253 | windowSecond->assertNoEvents(); |
| 5254 | } |
| 5255 | |
| 5256 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5257 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5258 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5259 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5260 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5261 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5262 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5263 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5264 | |
| 5265 | window->setFocusable(true); |
| 5266 | previousFocusedWindow->setFocusable(true); |
| 5267 | window->setVisible(false); |
| 5268 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5269 | setFocusedWindow(previousFocusedWindow); |
| 5270 | previousFocusedWindow->consumeFocusEvent(true); |
| 5271 | |
| 5272 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5273 | setFocusedWindow(window); |
| 5274 | previousFocusedWindow->consumeFocusEvent(false); |
| 5275 | |
| 5276 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5278 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5279 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5280 | |
| 5281 | // Window does not get focus event or key down. |
| 5282 | window->assertNoEvents(); |
| 5283 | |
| 5284 | // Window becomes visible. |
| 5285 | window->setVisible(true); |
| 5286 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5287 | |
| 5288 | // Window receives focus event. |
| 5289 | window->consumeFocusEvent(true); |
| 5290 | // Focused window receives key down. |
| 5291 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5292 | } |
| 5293 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5294 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5295 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5296 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5297 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5298 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5299 | |
| 5300 | // window is granted focus. |
| 5301 | window->setFocusable(true); |
| 5302 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5303 | setFocusedWindow(window); |
| 5304 | window->consumeFocusEvent(true); |
| 5305 | |
| 5306 | // When a display is removed window loses focus. |
| 5307 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5308 | window->consumeFocusEvent(false); |
| 5309 | } |
| 5310 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5311 | /** |
| 5312 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5313 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5314 | * of the 'slipperyEnterWindow'. |
| 5315 | * |
| 5316 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5317 | * a way so that the touched location is no longer covered by the top window. |
| 5318 | * |
| 5319 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5320 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5321 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5322 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5323 | * with ACTION_DOWN). |
| 5324 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5325 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5326 | * |
| 5327 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5328 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5329 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5330 | * |
| 5331 | * In this test, we ensure that the event received by the bottom window has |
| 5332 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5333 | */ |
| 5334 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5335 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 5336 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5337 | |
| 5338 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5339 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5340 | |
| 5341 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5342 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5343 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5344 | // Make sure this one overlaps the bottom window |
| 5345 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5346 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5347 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5348 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5349 | |
| 5350 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5351 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5352 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5353 | |
| 5354 | mDispatcher->setInputWindows( |
| 5355 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5356 | |
| 5357 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
| 5358 | NotifyMotionArgs args = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5359 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 5360 | mDispatcher->notifyMotion(&args); |
| 5361 | slipperyExitWindow->consumeMotionDown(); |
| 5362 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5363 | mDispatcher->setInputWindows( |
| 5364 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5365 | |
| 5366 | args = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5367 | ADISPLAY_ID_DEFAULT, {{51, 51}}); |
| 5368 | mDispatcher->notifyMotion(&args); |
| 5369 | |
| 5370 | slipperyExitWindow->consumeMotionCancel(); |
| 5371 | |
| 5372 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5373 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5374 | } |
| 5375 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5376 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5377 | protected: |
| 5378 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5379 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5380 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5381 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5382 | sp<FakeWindowHandle> mWindow; |
| 5383 | |
| 5384 | virtual void SetUp() override { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5385 | mFakePolicy = sp<FakeInputDispatcherPolicy>::make(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5386 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5387 | mDispatcher = std::make_unique<InputDispatcher>(mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5388 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 5389 | ASSERT_EQ(OK, mDispatcher->start()); |
| 5390 | |
| 5391 | setUpWindow(); |
| 5392 | } |
| 5393 | |
| 5394 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5395 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5396 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5397 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5398 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5399 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5400 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5401 | mWindow->consumeFocusEvent(true); |
| 5402 | } |
| 5403 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5404 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5405 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5406 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5407 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 5408 | mDispatcher->notifyKey(&keyArgs); |
| 5409 | |
| 5410 | // Window should receive key down event. |
| 5411 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5412 | } |
| 5413 | |
| 5414 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 5415 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 5416 | InputEvent* repeatEvent = mWindow->consume(); |
| 5417 | ASSERT_NE(nullptr, repeatEvent); |
| 5418 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 5419 | ASSERT_EQ(InputEventType::KEY, repeatEvent->getType()); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5420 | |
| 5421 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 5422 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 5423 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 5424 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 5425 | } |
| 5426 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5427 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5428 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5429 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5430 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 5431 | mDispatcher->notifyKey(&keyArgs); |
| 5432 | |
| 5433 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 5434 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5435 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5436 | } |
| 5437 | }; |
| 5438 | |
| 5439 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5440 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5441 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5442 | expectKeyRepeatOnce(repeatCount); |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5447 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5448 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5449 | expectKeyRepeatOnce(repeatCount); |
| 5450 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5451 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5452 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5453 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5454 | expectKeyRepeatOnce(repeatCount); |
| 5455 | } |
| 5456 | } |
| 5457 | |
| 5458 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5459 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5460 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5461 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5462 | mWindow->assertNoEvents(); |
| 5463 | } |
| 5464 | |
| 5465 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5466 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5467 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5468 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5469 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5470 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5471 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5472 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5473 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 5474 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5475 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5476 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5477 | mWindow->assertNoEvents(); |
| 5478 | } |
| 5479 | |
| 5480 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5481 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5482 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5483 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5484 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5485 | // 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] | 5486 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5487 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5488 | mWindow->assertNoEvents(); |
| 5489 | } |
| 5490 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5491 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 5492 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5493 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5494 | NotifyDeviceResetArgs args(/*id=*/10, /*eventTime=*/20, DEVICE_ID); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5495 | mDispatcher->notifyDeviceReset(&args); |
| 5496 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 5497 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 5498 | mWindow->assertNoEvents(); |
| 5499 | } |
| 5500 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5501 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5502 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5503 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5504 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5505 | InputEvent* repeatEvent = mWindow->consume(); |
| 5506 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5507 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 5508 | IdGenerator::getSource(repeatEvent->getId())); |
| 5509 | } |
| 5510 | } |
| 5511 | |
| 5512 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5513 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5514 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5515 | |
| 5516 | std::unordered_set<int32_t> idSet; |
| 5517 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5518 | InputEvent* repeatEvent = mWindow->consume(); |
| 5519 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5520 | int32_t id = repeatEvent->getId(); |
| 5521 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 5522 | idSet.insert(id); |
| 5523 | } |
| 5524 | } |
| 5525 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5526 | /* Test InputDispatcher for MultiDisplay */ |
| 5527 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 5528 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5529 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5530 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5531 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5532 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5533 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5534 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5535 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5536 | // Set focus window for primary display, but focused display would be second one. |
| 5537 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5538 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5539 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5540 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5541 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5542 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5543 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5544 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5545 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5546 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5547 | // Set focus display to second one. |
| 5548 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 5549 | // Set focus window for second display. |
| 5550 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5551 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5552 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5553 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5554 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5555 | } |
| 5556 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5557 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5558 | InputDispatcherTest::TearDown(); |
| 5559 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5560 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5561 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5562 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5563 | windowInSecondary.clear(); |
| 5564 | } |
| 5565 | |
| 5566 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5567 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5568 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5569 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5570 | sp<FakeWindowHandle> windowInSecondary; |
| 5571 | }; |
| 5572 | |
| 5573 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 5574 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5575 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5576 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5577 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5578 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5579 | windowInSecondary->assertNoEvents(); |
| 5580 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5581 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5582 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5583 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5584 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5585 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5586 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5587 | } |
| 5588 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5589 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5590 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5591 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5592 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5593 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5594 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5595 | windowInSecondary->assertNoEvents(); |
| 5596 | |
| 5597 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5598 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5599 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5600 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5601 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5602 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5603 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5604 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5605 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5606 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 5607 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5608 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5609 | |
| 5610 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5611 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5612 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5613 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5614 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5615 | windowInSecondary->assertNoEvents(); |
| 5616 | } |
| 5617 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5618 | // Test per-display input monitors for motion event. |
| 5619 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5620 | FakeMonitorReceiver monitorInPrimary = |
| 5621 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5622 | FakeMonitorReceiver monitorInSecondary = |
| 5623 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5624 | |
| 5625 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5627 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5628 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5629 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5630 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5631 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5632 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5633 | |
| 5634 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5635 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5636 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5637 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5638 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5639 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5640 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5641 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5642 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 5643 | // Lift up the touch from the second display |
| 5644 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5645 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5646 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5647 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5648 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 5649 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5650 | // Test inject a non-pointer motion event. |
| 5651 | // If specific a display, it will dispatch to the focused window of particular display, |
| 5652 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5653 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5654 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 5655 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5656 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5657 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5658 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5659 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | // Test per-display input monitors for key event. |
| 5663 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5664 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5665 | FakeMonitorReceiver monitorInPrimary = |
| 5666 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5667 | FakeMonitorReceiver monitorInSecondary = |
| 5668 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5669 | |
| 5670 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5671 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5672 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5673 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5674 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5675 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5676 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5677 | } |
| 5678 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5679 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 5680 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5681 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5682 | secondWindowInPrimary->setFocusable(true); |
| 5683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 5684 | setFocusedWindow(secondWindowInPrimary); |
| 5685 | windowInPrimary->consumeFocusEvent(false); |
| 5686 | secondWindowInPrimary->consumeFocusEvent(true); |
| 5687 | |
| 5688 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5689 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 5690 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5691 | windowInPrimary->assertNoEvents(); |
| 5692 | windowInSecondary->assertNoEvents(); |
| 5693 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5694 | } |
| 5695 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 5696 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 5697 | FakeMonitorReceiver monitorInPrimary = |
| 5698 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5699 | FakeMonitorReceiver monitorInSecondary = |
| 5700 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 5701 | |
| 5702 | // Test touch down on primary display. |
| 5703 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5704 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5705 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5706 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5707 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5708 | |
| 5709 | // Test touch down on second display. |
| 5710 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5711 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5712 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5713 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5714 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 5715 | |
| 5716 | // Trigger cancel touch. |
| 5717 | mDispatcher->cancelCurrentTouch(); |
| 5718 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5719 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5720 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5721 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5722 | |
| 5723 | // Test inject a move motion event, no window/monitor should receive the event. |
| 5724 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5725 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5726 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5727 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5728 | windowInPrimary->assertNoEvents(); |
| 5729 | monitorInPrimary.assertNoEvents(); |
| 5730 | |
| 5731 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5732 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5733 | SECOND_DISPLAY_ID, {110, 200})) |
| 5734 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5735 | windowInSecondary->assertNoEvents(); |
| 5736 | monitorInSecondary.assertNoEvents(); |
| 5737 | } |
| 5738 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5739 | class InputFilterTest : public InputDispatcherTest { |
| 5740 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5741 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 5742 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5743 | NotifyMotionArgs motionArgs; |
| 5744 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5745 | motionArgs = |
| 5746 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5747 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5748 | motionArgs = |
| 5749 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5750 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5751 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5752 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5753 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 5754 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5755 | } else { |
| 5756 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5757 | } |
| 5758 | } |
| 5759 | |
| 5760 | void testNotifyKey(bool expectToBeFiltered) { |
| 5761 | NotifyKeyArgs keyArgs; |
| 5762 | |
| 5763 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5764 | mDispatcher->notifyKey(&keyArgs); |
| 5765 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 5766 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5767 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5768 | |
| 5769 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 5770 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5771 | } else { |
| 5772 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5773 | } |
| 5774 | } |
| 5775 | }; |
| 5776 | |
| 5777 | // Test InputFilter for MotionEvent |
| 5778 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 5779 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 5780 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5781 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5782 | |
| 5783 | // Enable InputFilter |
| 5784 | mDispatcher->setInputFilterEnabled(true); |
| 5785 | // Test touch on both primary and second display, and check if both events are filtered. |
| 5786 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 5787 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 5788 | |
| 5789 | // Disable InputFilter |
| 5790 | mDispatcher->setInputFilterEnabled(false); |
| 5791 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 5792 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5793 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5794 | } |
| 5795 | |
| 5796 | // Test InputFilter for KeyEvent |
| 5797 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 5798 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 5799 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5800 | |
| 5801 | // Enable InputFilter |
| 5802 | mDispatcher->setInputFilterEnabled(true); |
| 5803 | // Send a key event, and check if it is filtered. |
| 5804 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 5805 | |
| 5806 | // Disable InputFilter |
| 5807 | mDispatcher->setInputFilterEnabled(false); |
| 5808 | // Send a key event, and check if it isn't filtered. |
| 5809 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5810 | } |
| 5811 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5812 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 5813 | // logical display coordinate space. |
| 5814 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 5815 | ui::Transform firstDisplayTransform; |
| 5816 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5817 | ui::Transform secondDisplayTransform; |
| 5818 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 5819 | |
| 5820 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 5821 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 5822 | displayInfos[0].transform = firstDisplayTransform; |
| 5823 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 5824 | displayInfos[1].transform = secondDisplayTransform; |
| 5825 | |
| 5826 | mDispatcher->onWindowInfosChanged({}, displayInfos); |
| 5827 | |
| 5828 | // Enable InputFilter |
| 5829 | mDispatcher->setInputFilterEnabled(true); |
| 5830 | |
| 5831 | // Ensure the correct transforms are used for the displays. |
| 5832 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 5833 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 5834 | } |
| 5835 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5836 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 5837 | protected: |
| 5838 | virtual void SetUp() override { |
| 5839 | InputDispatcherTest::SetUp(); |
| 5840 | |
| 5841 | /** |
| 5842 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 5843 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 5844 | * on. |
| 5845 | */ |
| 5846 | mDispatcher->setInputFilterEnabled(true); |
| 5847 | |
| 5848 | std::shared_ptr<InputApplicationHandle> application = |
| 5849 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5850 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 5851 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5852 | |
| 5853 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5854 | mWindow->setFocusable(true); |
| 5855 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5856 | setFocusedWindow(mWindow); |
| 5857 | mWindow->consumeFocusEvent(true); |
| 5858 | } |
| 5859 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5860 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5861 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5862 | KeyEvent event; |
| 5863 | |
| 5864 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5865 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 5866 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5867 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5868 | const int32_t additionalPolicyFlags = |
| 5869 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 5870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5871 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5872 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5873 | policyFlags | additionalPolicyFlags)); |
| 5874 | |
| 5875 | InputEvent* received = mWindow->consume(); |
| 5876 | ASSERT_NE(nullptr, received); |
| 5877 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 5878 | ASSERT_EQ(received->getType(), InputEventType::KEY); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5879 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 5880 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 5881 | } |
| 5882 | |
| 5883 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5884 | int32_t flags) { |
| 5885 | MotionEvent event; |
| 5886 | PointerProperties pointerProperties[1]; |
| 5887 | PointerCoords pointerCoords[1]; |
| 5888 | pointerProperties[0].clear(); |
| 5889 | pointerProperties[0].id = 0; |
| 5890 | pointerCoords[0].clear(); |
| 5891 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 5892 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 5893 | |
| 5894 | ui::Transform identityTransform; |
| 5895 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5896 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 5897 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 5898 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 5899 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 5900 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 5901 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5902 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 5903 | |
| 5904 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 5905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5906 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5907 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5908 | policyFlags | additionalPolicyFlags)); |
| 5909 | |
| 5910 | InputEvent* received = mWindow->consume(); |
| 5911 | ASSERT_NE(nullptr, received); |
| 5912 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 5913 | ASSERT_EQ(received->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5914 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 5915 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | private: |
| 5919 | sp<FakeWindowHandle> mWindow; |
| 5920 | }; |
| 5921 | |
| 5922 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5923 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 5924 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 5925 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5926 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 5927 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5928 | } |
| 5929 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5930 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5931 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5932 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5933 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 5934 | } |
| 5935 | |
| 5936 | TEST_F(InputFilterInjectionPolicyTest, |
| 5937 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 5938 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5939 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5940 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5944 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 5945 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5948 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5949 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5950 | InputDispatcherTest::SetUp(); |
| 5951 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5952 | std::shared_ptr<FakeApplicationHandle> application = |
| 5953 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5954 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5955 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5956 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5957 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5958 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5959 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5960 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5961 | |
| 5962 | // Set focused application. |
| 5963 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5964 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5965 | |
| 5966 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5967 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5968 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5969 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5970 | } |
| 5971 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5972 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5973 | InputDispatcherTest::TearDown(); |
| 5974 | |
| 5975 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5976 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5977 | } |
| 5978 | |
| 5979 | protected: |
| 5980 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5981 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5982 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5983 | }; |
| 5984 | |
| 5985 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 5986 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 5987 | // the onPointerDownOutsideFocus callback. |
| 5988 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5989 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 5990 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5991 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5992 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 5993 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5994 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5995 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 5996 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 5997 | } |
| 5998 | |
| 5999 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 6000 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 6001 | // onPointerDownOutsideFocus callback. |
| 6002 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6003 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6004 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6005 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6006 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6007 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6008 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6009 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6010 | } |
| 6011 | |
| 6012 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 6013 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 6014 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6015 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6016 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6017 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6018 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6019 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6020 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6021 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6022 | } |
| 6023 | |
| 6024 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6025 | // DOWN on the window that already has focus. Ensure no window received the |
| 6026 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6027 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6028 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6029 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6030 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6031 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6032 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6033 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6034 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6035 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6036 | } |
| 6037 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6038 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 6039 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 6040 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 6041 | const MotionEvent event = |
| 6042 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6043 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 6044 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6045 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 6046 | .build(); |
| 6047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 6048 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6049 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6050 | |
| 6051 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6052 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 6053 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 6054 | mUnfocusedWindow->assertNoEvents(); |
| 6055 | } |
| 6056 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6057 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 6058 | // windows that point to the same client token. |
| 6059 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 6060 | virtual void SetUp() override { |
| 6061 | InputDispatcherTest::SetUp(); |
| 6062 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6063 | std::shared_ptr<FakeApplicationHandle> application = |
| 6064 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6065 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 6066 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6067 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 6068 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6069 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 6070 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6071 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 6072 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6073 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6074 | } |
| 6075 | |
| 6076 | protected: |
| 6077 | sp<FakeWindowHandle> mWindow1; |
| 6078 | sp<FakeWindowHandle> mWindow2; |
| 6079 | |
| 6080 | // 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] | 6081 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 6082 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 6083 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6084 | } |
| 6085 | |
| 6086 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 6087 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6088 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6089 | InputEvent* event = window->consume(); |
| 6090 | |
| 6091 | ASSERT_NE(nullptr, event) << name.c_str() |
| 6092 | << ": consumer should have returned non-NULL event."; |
| 6093 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 6094 | ASSERT_EQ(InputEventType::MOTION, event->getType()) |
| 6095 | << name.c_str() << ": expected MotionEvent, got " << *event; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6096 | |
| 6097 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6098 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 6099 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6100 | |
| 6101 | for (size_t i = 0; i < points.size(); i++) { |
| 6102 | float expectedX = points[i].x; |
| 6103 | float expectedY = points[i].y; |
| 6104 | |
| 6105 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 6106 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 6107 | << ", got " << motionEvent.getX(i); |
| 6108 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 6109 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 6110 | << ", got " << motionEvent.getY(i); |
| 6111 | } |
| 6112 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6113 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6114 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6115 | std::vector<PointF> expectedPoints) { |
| 6116 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 6117 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 6118 | mDispatcher->notifyMotion(&motionArgs); |
| 6119 | |
| 6120 | // Always consume from window1 since it's the window that has the InputReceiver |
| 6121 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 6122 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6123 | }; |
| 6124 | |
| 6125 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 6126 | // Touch Window 1 |
| 6127 | PointF touchedPoint = {10, 10}; |
| 6128 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6129 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6130 | |
| 6131 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6132 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6133 | |
| 6134 | // Touch Window 2 |
| 6135 | touchedPoint = {150, 150}; |
| 6136 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6137 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6138 | } |
| 6139 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6140 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 6141 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6142 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6143 | |
| 6144 | // Touch Window 1 |
| 6145 | PointF touchedPoint = {10, 10}; |
| 6146 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6147 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6148 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6149 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6150 | |
| 6151 | // Touch Window 2 |
| 6152 | touchedPoint = {150, 150}; |
| 6153 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6154 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 6155 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6156 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6157 | // Update the transform so rotation is set |
| 6158 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6159 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 6160 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6163 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6164 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6165 | |
| 6166 | // Touch Window 1 |
| 6167 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6168 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6169 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6170 | |
| 6171 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6172 | touchedPoints.push_back(PointF{150, 150}); |
| 6173 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6174 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6175 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6176 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6177 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6178 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6179 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6180 | // Update the transform so rotation is set for Window 2 |
| 6181 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6182 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6183 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6186 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6187 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6188 | |
| 6189 | // Touch Window 1 |
| 6190 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6191 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6192 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6193 | |
| 6194 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6195 | touchedPoints.push_back(PointF{150, 150}); |
| 6196 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6197 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6198 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6199 | |
| 6200 | // Move both windows |
| 6201 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6202 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6203 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6204 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6205 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6206 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6207 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6208 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6209 | expectedPoints.pop_back(); |
| 6210 | |
| 6211 | // Touch Window 2 |
| 6212 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6213 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6214 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6215 | |
| 6216 | // Move both windows |
| 6217 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6218 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6219 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6220 | |
| 6221 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 6225 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 6226 | |
| 6227 | // Touch Window 1 |
| 6228 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6229 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6230 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6231 | |
| 6232 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6233 | touchedPoints.push_back(PointF{150, 150}); |
| 6234 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6235 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6236 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6237 | |
| 6238 | // Move both windows |
| 6239 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6240 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6241 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6242 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6243 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6244 | } |
| 6245 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6246 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 6247 | virtual void SetUp() override { |
| 6248 | InputDispatcherTest::SetUp(); |
| 6249 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6250 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6251 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6252 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 6253 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6254 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6255 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6256 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6257 | |
| 6258 | // Set focused application. |
| 6259 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 6260 | |
| 6261 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6262 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6263 | mWindow->consumeFocusEvent(true); |
| 6264 | } |
| 6265 | |
| 6266 | virtual void TearDown() override { |
| 6267 | InputDispatcherTest::TearDown(); |
| 6268 | mWindow.clear(); |
| 6269 | } |
| 6270 | |
| 6271 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6272 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6273 | sp<FakeWindowHandle> mWindow; |
| 6274 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 6275 | |
| 6276 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6278 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6279 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6281 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6282 | WINDOW_LOCATION)); |
| 6283 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6284 | |
| 6285 | sp<FakeWindowHandle> addSpyWindow() { |
| 6286 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6287 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6288 | spy->setTrustedOverlay(true); |
| 6289 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6290 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6291 | spy->setDispatchingTimeout(30ms); |
| 6292 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 6293 | return spy; |
| 6294 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6295 | }; |
| 6296 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6297 | // Send a tap and respond, which should not cause an ANR. |
| 6298 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 6299 | tapOnWindow(); |
| 6300 | mWindow->consumeMotionDown(); |
| 6301 | mWindow->consumeMotionUp(); |
| 6302 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6303 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6304 | } |
| 6305 | |
| 6306 | // Send a regular key and respond, which should not cause an ANR. |
| 6307 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6308 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6309 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6310 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6311 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6312 | } |
| 6313 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6314 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 6315 | mWindow->setFocusable(false); |
| 6316 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6317 | mWindow->consumeFocusEvent(false); |
| 6318 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6319 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6320 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6321 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6322 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6324 | // Key will not go to window because we have no focused window. |
| 6325 | // The 'no focused window' ANR timer should start instead. |
| 6326 | |
| 6327 | // Now, the focused application goes away. |
| 6328 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 6329 | // The key should get dropped and there should be no ANR. |
| 6330 | |
| 6331 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6332 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6333 | } |
| 6334 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6335 | // 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] | 6336 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 6337 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6338 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6339 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6340 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6341 | WINDOW_LOCATION)); |
| 6342 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6343 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 6344 | ASSERT_TRUE(sequenceNum); |
| 6345 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6346 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6347 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6348 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6349 | mWindow->consumeMotionEvent( |
| 6350 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6351 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6352 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6353 | } |
| 6354 | |
| 6355 | // Send a key to the app and have the app not respond right away. |
| 6356 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 6357 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6358 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6359 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 6360 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6361 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6362 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6363 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6364 | } |
| 6365 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6366 | // We have a focused application, but no focused window |
| 6367 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6368 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6369 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6370 | mWindow->consumeFocusEvent(false); |
| 6371 | |
| 6372 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6373 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6374 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6375 | WINDOW_LOCATION)); |
| 6376 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 6377 | mDispatcher->waitForIdle(); |
| 6378 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6379 | |
| 6380 | // Once a focused event arrives, we get an ANR for this application |
| 6381 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6382 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6383 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6384 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6385 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6386 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6387 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6388 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6389 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6390 | } |
| 6391 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6392 | /** |
| 6393 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 6394 | * there will not be an ANR. |
| 6395 | */ |
| 6396 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 6397 | mWindow->setFocusable(false); |
| 6398 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6399 | mWindow->consumeFocusEvent(false); |
| 6400 | |
| 6401 | KeyEvent event; |
| 6402 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 6403 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 6404 | |
| 6405 | // Define a valid key down event that is stale (too old). |
| 6406 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 6407 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6408 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6409 | |
| 6410 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 6411 | |
| 6412 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6413 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6414 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 6415 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 6416 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 6417 | << "Injection should fail because the event is stale"; |
| 6418 | |
| 6419 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6420 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6421 | mWindow->assertNoEvents(); |
| 6422 | } |
| 6423 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6424 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6425 | // Make sure that we don't notify policy twice about the same ANR. |
| 6426 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6427 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6428 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6429 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6430 | |
| 6431 | // Once a focused event arrives, we get an ANR for this application |
| 6432 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6433 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6434 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6435 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6436 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6437 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6438 | const std::chrono::duration appTimeout = |
| 6439 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6440 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6441 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6442 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6443 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 6444 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6445 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6446 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 6447 | // '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] | 6448 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6449 | } |
| 6450 | |
| 6451 | // We have a focused application, but no focused window |
| 6452 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6453 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6454 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6455 | mWindow->consumeFocusEvent(false); |
| 6456 | |
| 6457 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6458 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6459 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6460 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6461 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6462 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6463 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6464 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6465 | |
| 6466 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6467 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6468 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6469 | mWindow->assertNoEvents(); |
| 6470 | } |
| 6471 | |
| 6472 | /** |
| 6473 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 6474 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 6475 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 6476 | * the ANR mechanism should still work. |
| 6477 | * |
| 6478 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 6479 | * DOWN event, while not responding on the second one. |
| 6480 | */ |
| 6481 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 6482 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6483 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6484 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6485 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6486 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6487 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6488 | |
| 6489 | // Now send ACTION_UP, with identical timestamp |
| 6490 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6491 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6492 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6493 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6494 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6495 | |
| 6496 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 6497 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6498 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6499 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6500 | } |
| 6501 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6502 | // A spy window can receive an ANR |
| 6503 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 6504 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 6505 | |
| 6506 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6507 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6508 | WINDOW_LOCATION)); |
| 6509 | mWindow->consumeMotionDown(); |
| 6510 | |
| 6511 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 6512 | ASSERT_TRUE(sequenceNum); |
| 6513 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6514 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6515 | |
| 6516 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6517 | spy->consumeMotionEvent( |
| 6518 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6519 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6520 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6521 | } |
| 6522 | |
| 6523 | // 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] | 6524 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6525 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 6526 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6527 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6528 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6529 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6530 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6531 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6532 | |
| 6533 | // Stuck on the ACTION_UP |
| 6534 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6535 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6536 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6537 | // 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] | 6538 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6539 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6540 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6541 | |
| 6542 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6543 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6544 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6545 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6546 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6547 | } |
| 6548 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6549 | // 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] | 6550 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6551 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 6552 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6553 | |
| 6554 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6555 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6556 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6557 | |
| 6558 | mWindow->consumeMotionDown(); |
| 6559 | // Stuck on the ACTION_UP |
| 6560 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6561 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6562 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6563 | // 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] | 6564 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6565 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6566 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6567 | |
| 6568 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6569 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6570 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6571 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6572 | spy->assertNoEvents(); |
| 6573 | } |
| 6574 | |
| 6575 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 6576 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 6577 | |
| 6578 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6579 | |
| 6580 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6581 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6582 | WINDOW_LOCATION)); |
| 6583 | |
| 6584 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6585 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 6586 | ASSERT_TRUE(consumeSeq); |
| 6587 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6588 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6589 | |
| 6590 | monitor.finishEvent(*consumeSeq); |
| 6591 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6592 | |
| 6593 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6594 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6595 | } |
| 6596 | |
| 6597 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 6598 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 6599 | // get an ANR again. |
| 6600 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 6601 | // 2. consume all pending events (= queue becomes healthy again) |
| 6602 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 6603 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 6604 | tapOnWindow(); |
| 6605 | |
| 6606 | mWindow->consumeMotionDown(); |
| 6607 | // Block on ACTION_UP |
| 6608 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6609 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6610 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 6611 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6612 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6613 | mWindow->assertNoEvents(); |
| 6614 | |
| 6615 | tapOnWindow(); |
| 6616 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6617 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6618 | mWindow->consumeMotionUp(); |
| 6619 | |
| 6620 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6621 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6622 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6623 | mWindow->assertNoEvents(); |
| 6624 | } |
| 6625 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6626 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 6627 | // it. |
| 6628 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6630 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6631 | WINDOW_LOCATION)); |
| 6632 | |
| 6633 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6634 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6635 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6636 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 6637 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6638 | // 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] | 6639 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6640 | mWindow->consumeMotionEvent( |
| 6641 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6642 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6643 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6644 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6645 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6646 | } |
| 6647 | |
| 6648 | /** |
| 6649 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6650 | * not to to the focused window until the motion is processed. |
| 6651 | * |
| 6652 | * Warning!!! |
| 6653 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6654 | * and the injection timeout that we specify when injecting the key. |
| 6655 | * We must have the injection timeout (10ms) be smaller than |
| 6656 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6657 | * |
| 6658 | * If that value changes, this test should also change. |
| 6659 | */ |
| 6660 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 6661 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6662 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6663 | |
| 6664 | tapOnWindow(); |
| 6665 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6666 | ASSERT_TRUE(downSequenceNum); |
| 6667 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6668 | ASSERT_TRUE(upSequenceNum); |
| 6669 | // Don't finish the events yet, and send a key |
| 6670 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 6671 | // window even if motions are still being processed. But because the injection timeout is short, |
| 6672 | // we will receive INJECTION_TIMED_OUT as the result. |
| 6673 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6674 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6675 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6676 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6677 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6678 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6679 | // and the key remains pending, waiting for the touch events to be processed |
| 6680 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6681 | ASSERT_FALSE(keySequenceNum); |
| 6682 | |
| 6683 | std::this_thread::sleep_for(500ms); |
| 6684 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 6685 | // to the focused window, even though we have not yet finished the motion event |
| 6686 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6687 | mWindow->finishEvent(*downSequenceNum); |
| 6688 | mWindow->finishEvent(*upSequenceNum); |
| 6689 | } |
| 6690 | |
| 6691 | /** |
| 6692 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6693 | * not go to the focused window until the motion is processed. |
| 6694 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 6695 | * focused window right away. |
| 6696 | */ |
| 6697 | TEST_F(InputDispatcherSingleWindowAnr, |
| 6698 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 6699 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6700 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6701 | |
| 6702 | tapOnWindow(); |
| 6703 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6704 | ASSERT_TRUE(downSequenceNum); |
| 6705 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6706 | ASSERT_TRUE(upSequenceNum); |
| 6707 | // Don't finish the events yet, and send a key |
| 6708 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6709 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6710 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6711 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6712 | // At this point, key is still pending, and should not be sent to the application yet. |
| 6713 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6714 | ASSERT_FALSE(keySequenceNum); |
| 6715 | |
| 6716 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 6717 | tapOnWindow(); |
| 6718 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 6719 | // the other events yet. We can finish events in any order. |
| 6720 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 6721 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 6722 | mWindow->consumeMotionDown(); |
| 6723 | mWindow->consumeMotionUp(); |
| 6724 | mWindow->assertNoEvents(); |
| 6725 | } |
| 6726 | |
| 6727 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 6728 | virtual void SetUp() override { |
| 6729 | InputDispatcherTest::SetUp(); |
| 6730 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6731 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6732 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6733 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 6734 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6735 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6736 | // 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] | 6737 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6738 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6739 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 6740 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6741 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6742 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6743 | |
| 6744 | // Set focused application. |
| 6745 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6746 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6747 | |
| 6748 | // Expect one focus window exist in display. |
| 6749 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6750 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6751 | mFocusedWindow->consumeFocusEvent(true); |
| 6752 | } |
| 6753 | |
| 6754 | virtual void TearDown() override { |
| 6755 | InputDispatcherTest::TearDown(); |
| 6756 | |
| 6757 | mUnfocusedWindow.clear(); |
| 6758 | mFocusedWindow.clear(); |
| 6759 | } |
| 6760 | |
| 6761 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6762 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6763 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 6764 | sp<FakeWindowHandle> mFocusedWindow; |
| 6765 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 6766 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 6767 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 6768 | |
| 6769 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 6770 | |
| 6771 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 6772 | |
| 6773 | private: |
| 6774 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6775 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6776 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6777 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6779 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6780 | location)); |
| 6781 | } |
| 6782 | }; |
| 6783 | |
| 6784 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 6785 | // should be ANR'd first. |
| 6786 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6787 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6788 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6789 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6790 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6791 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 6792 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6793 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6794 | // We consumed all events, so no ANR |
| 6795 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6796 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6797 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6798 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6799 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6800 | FOCUSED_WINDOW_LOCATION)); |
| 6801 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6802 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6803 | |
| 6804 | const std::chrono::duration timeout = |
| 6805 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6806 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6807 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 6808 | // sequence to make it consistent |
| 6809 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6810 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6811 | mFocusedWindow->consumeMotionDown(); |
| 6812 | // This cancel is generated because the connection was unresponsive |
| 6813 | mFocusedWindow->consumeMotionCancel(); |
| 6814 | mFocusedWindow->assertNoEvents(); |
| 6815 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6816 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6817 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6818 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6819 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6820 | } |
| 6821 | |
| 6822 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 6823 | // it doesn't matter which order they should have ANR. |
| 6824 | // But we should receive ANR for both. |
| 6825 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 6826 | // Set the timeout for unfocused window to match the focused window |
| 6827 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 6828 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6829 | |
| 6830 | tapOnFocusedWindow(); |
| 6831 | // 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] | 6832 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 6833 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 6834 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6835 | |
| 6836 | // 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] | 6837 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 6838 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 6839 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 6840 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6841 | |
| 6842 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6843 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6844 | |
| 6845 | mFocusedWindow->consumeMotionDown(); |
| 6846 | mFocusedWindow->consumeMotionUp(); |
| 6847 | mUnfocusedWindow->consumeMotionOutside(); |
| 6848 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6849 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 6850 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 6851 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6852 | |
| 6853 | // Both applications should be marked as responsive, in any order |
| 6854 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 6855 | mFocusedWindow->getToken() == responsiveToken2); |
| 6856 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 6857 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 6858 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6859 | } |
| 6860 | |
| 6861 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 6862 | // We should also log an error to account for the dropped event (not tested here). |
| 6863 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 6864 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 6865 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 6866 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6867 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6868 | // Receive the events, but don't respond |
| 6869 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 6870 | ASSERT_TRUE(downEventSequenceNum); |
| 6871 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 6872 | ASSERT_TRUE(upEventSequenceNum); |
| 6873 | const std::chrono::duration timeout = |
| 6874 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6875 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6876 | |
| 6877 | // Tap once again |
| 6878 | // 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] | 6879 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6880 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6881 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6882 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6883 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6884 | FOCUSED_WINDOW_LOCATION)); |
| 6885 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 6886 | // valid touch target |
| 6887 | mUnfocusedWindow->assertNoEvents(); |
| 6888 | |
| 6889 | // Consume the first tap |
| 6890 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 6891 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 6892 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6893 | // The second tap did not go to the focused window |
| 6894 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6895 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6896 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6897 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6898 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6899 | } |
| 6900 | |
| 6901 | // If you tap outside of all windows, there will not be ANR |
| 6902 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6903 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6904 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6905 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 6906 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6907 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6908 | } |
| 6909 | |
| 6910 | // Since the focused window is paused, tapping on it should not produce any events |
| 6911 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 6912 | mFocusedWindow->setPaused(true); |
| 6913 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6914 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6915 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6916 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6917 | FOCUSED_WINDOW_LOCATION)); |
| 6918 | |
| 6919 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 6920 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6921 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 6922 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6923 | |
| 6924 | mFocusedWindow->assertNoEvents(); |
| 6925 | mUnfocusedWindow->assertNoEvents(); |
| 6926 | } |
| 6927 | |
| 6928 | /** |
| 6929 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6930 | * not to to the focused window until the motion is processed. |
| 6931 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 6932 | * |
| 6933 | * Warning!!! |
| 6934 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6935 | * and the injection timeout that we specify when injecting the key. |
| 6936 | * We must have the injection timeout (10ms) be smaller than |
| 6937 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6938 | * |
| 6939 | * If that value changes, this test should also change. |
| 6940 | */ |
| 6941 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 6942 | // Set a long ANR timeout to prevent it from triggering |
| 6943 | mFocusedWindow->setDispatchingTimeout(2s); |
| 6944 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 6945 | |
| 6946 | tapOnUnfocusedWindow(); |
| 6947 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6948 | ASSERT_TRUE(downSequenceNum); |
| 6949 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6950 | ASSERT_TRUE(upSequenceNum); |
| 6951 | // Don't finish the events yet, and send a key |
| 6952 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 6953 | // window even if motions are still being processed. |
| 6954 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6955 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6956 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6957 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6958 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6959 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6960 | // and the key remains pending, waiting for the touch events to be processed |
| 6961 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 6962 | ASSERT_FALSE(keySequenceNum); |
| 6963 | |
| 6964 | // 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] | 6965 | mFocusedWindow->setFocusable(false); |
| 6966 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6967 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6968 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6969 | |
| 6970 | // Focus events should precede the key events |
| 6971 | mUnfocusedWindow->consumeFocusEvent(true); |
| 6972 | mFocusedWindow->consumeFocusEvent(false); |
| 6973 | |
| 6974 | // Finish the tap events, which should unblock dispatcher |
| 6975 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 6976 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 6977 | |
| 6978 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 6979 | // can finally go to the newly focused "mUnfocusedWindow". |
| 6980 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6981 | mFocusedWindow->assertNoEvents(); |
| 6982 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6983 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6984 | } |
| 6985 | |
| 6986 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 6987 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 6988 | // The other window should not be affected by that. |
| 6989 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 6990 | // Touch Window 1 |
| 6991 | NotifyMotionArgs motionArgs = |
| 6992 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6993 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 6994 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 6995 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6996 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6997 | |
| 6998 | // Touch Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6999 | motionArgs = generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7000 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7001 | mDispatcher->notifyMotion(&motionArgs); |
| 7002 | |
| 7003 | const std::chrono::duration timeout = |
| 7004 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7005 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7006 | |
| 7007 | mUnfocusedWindow->consumeMotionDown(); |
| 7008 | mFocusedWindow->consumeMotionDown(); |
| 7009 | // Focused window may or may not receive ACTION_MOVE |
| 7010 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 7011 | InputEvent* event; |
| 7012 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 7013 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 7014 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 7015 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 7016 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7017 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 7018 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 7019 | mFocusedWindow->consumeMotionCancel(); |
| 7020 | } else { |
| 7021 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 7022 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7023 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7024 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7025 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7026 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7027 | mUnfocusedWindow->assertNoEvents(); |
| 7028 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7029 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7030 | } |
| 7031 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7032 | /** |
| 7033 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 7034 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 7035 | * |
| 7036 | * If the user touches another application during this time, the key should be dropped. |
| 7037 | * Next, if a new focused window comes in, without toggling the focused application, |
| 7038 | * then no ANR should occur. |
| 7039 | * |
| 7040 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 7041 | * but in some cases the policy may not update the focused application. |
| 7042 | */ |
| 7043 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 7044 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 7045 | std::make_shared<FakeApplicationHandle>(); |
| 7046 | focusedApplication->setDispatchingTimeout(60ms); |
| 7047 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 7048 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 7049 | mFocusedWindow->setFocusable(false); |
| 7050 | |
| 7051 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7052 | mFocusedWindow->consumeFocusEvent(false); |
| 7053 | |
| 7054 | // Send a key. The ANR timer should start because there is no focused window. |
| 7055 | // 'focusedApplication' will get blamed if this timer completes. |
| 7056 | // 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] | 7057 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7058 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7059 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 7060 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7061 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7062 | |
| 7063 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 7064 | // then the injected touches won't cause the focused event to get dropped. |
| 7065 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 7066 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 7067 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 7068 | // For this test, it means that the key would get delivered to the window once it becomes |
| 7069 | // focused. |
| 7070 | std::this_thread::sleep_for(10ms); |
| 7071 | |
| 7072 | // Touch unfocused window. This should force the pending key to get dropped. |
| 7073 | NotifyMotionArgs motionArgs = |
| 7074 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7075 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 7076 | mDispatcher->notifyMotion(&motionArgs); |
| 7077 | |
| 7078 | // We do not consume the motion right away, because that would require dispatcher to first |
| 7079 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 7080 | // Set the focused window first. |
| 7081 | mFocusedWindow->setFocusable(true); |
| 7082 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7083 | setFocusedWindow(mFocusedWindow); |
| 7084 | mFocusedWindow->consumeFocusEvent(true); |
| 7085 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 7086 | // to another application. This could be a bug / behaviour in the policy. |
| 7087 | |
| 7088 | mUnfocusedWindow->consumeMotionDown(); |
| 7089 | |
| 7090 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7091 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 7092 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 7093 | } |
| 7094 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7095 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 7096 | // that has feature NO_INPUT_CHANNEL. |
| 7097 | // Layout: |
| 7098 | // Top (closest to user) |
| 7099 | // mNoInputWindow (above all windows) |
| 7100 | // mBottomWindow |
| 7101 | // Bottom (furthest from user) |
| 7102 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 7103 | virtual void SetUp() override { |
| 7104 | InputDispatcherTest::SetUp(); |
| 7105 | |
| 7106 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7107 | mNoInputWindow = |
| 7108 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7109 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7110 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7111 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7112 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7113 | // It's perfectly valid for this window to not have an associated input channel |
| 7114 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7115 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 7116 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7117 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7118 | |
| 7119 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7120 | } |
| 7121 | |
| 7122 | protected: |
| 7123 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 7124 | sp<FakeWindowHandle> mNoInputWindow; |
| 7125 | sp<FakeWindowHandle> mBottomWindow; |
| 7126 | }; |
| 7127 | |
| 7128 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 7129 | PointF touchedPoint = {10, 10}; |
| 7130 | |
| 7131 | NotifyMotionArgs motionArgs = |
| 7132 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7133 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 7134 | mDispatcher->notifyMotion(&motionArgs); |
| 7135 | |
| 7136 | mNoInputWindow->assertNoEvents(); |
| 7137 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 7138 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 7139 | // and therefore should prevent mBottomWindow from receiving touches |
| 7140 | mBottomWindow->assertNoEvents(); |
| 7141 | } |
| 7142 | |
| 7143 | /** |
| 7144 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 7145 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 7146 | */ |
| 7147 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 7148 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7149 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7150 | "Window with input channel and NO_INPUT_CHANNEL", |
| 7151 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7152 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7153 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7154 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7155 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7156 | |
| 7157 | PointF touchedPoint = {10, 10}; |
| 7158 | |
| 7159 | NotifyMotionArgs motionArgs = |
| 7160 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7161 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 7162 | mDispatcher->notifyMotion(&motionArgs); |
| 7163 | |
| 7164 | mNoInputWindow->assertNoEvents(); |
| 7165 | mBottomWindow->assertNoEvents(); |
| 7166 | } |
| 7167 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7168 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7169 | protected: |
| 7170 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7171 | sp<FakeWindowHandle> mWindow; |
| 7172 | sp<FakeWindowHandle> mMirror; |
| 7173 | |
| 7174 | virtual void SetUp() override { |
| 7175 | InputDispatcherTest::SetUp(); |
| 7176 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7177 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7178 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7179 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7180 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7181 | mWindow->setFocusable(true); |
| 7182 | mMirror->setFocusable(true); |
| 7183 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7184 | } |
| 7185 | }; |
| 7186 | |
| 7187 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7188 | // Request focus on a mirrored window |
| 7189 | setFocusedWindow(mMirror); |
| 7190 | |
| 7191 | // window gets focused |
| 7192 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7194 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7195 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7196 | } |
| 7197 | |
| 7198 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7199 | // focusable. |
| 7200 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7201 | setFocusedWindow(mMirror); |
| 7202 | |
| 7203 | // window gets focused |
| 7204 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7206 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7207 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7208 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7209 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7210 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7211 | |
| 7212 | mMirror->setFocusable(false); |
| 7213 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7214 | |
| 7215 | // window loses focus since one of the windows associated with the token in not focusable |
| 7216 | mWindow->consumeFocusEvent(false); |
| 7217 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7218 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7219 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7220 | mWindow->assertNoEvents(); |
| 7221 | } |
| 7222 | |
| 7223 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7224 | // invisible. |
| 7225 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 7226 | setFocusedWindow(mMirror); |
| 7227 | |
| 7228 | // window gets focused |
| 7229 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7230 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7231 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7232 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7233 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7234 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7235 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7236 | |
| 7237 | mMirror->setVisible(false); |
| 7238 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7239 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7241 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7242 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7244 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7245 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7246 | |
| 7247 | mWindow->setVisible(false); |
| 7248 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7249 | |
| 7250 | // window loses focus only after all windows associated with the token become invisible. |
| 7251 | mWindow->consumeFocusEvent(false); |
| 7252 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7253 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7254 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7255 | mWindow->assertNoEvents(); |
| 7256 | } |
| 7257 | |
| 7258 | // A focused & mirrored window remains focused until both windows are removed. |
| 7259 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7260 | setFocusedWindow(mMirror); |
| 7261 | |
| 7262 | // window gets focused |
| 7263 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7264 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7265 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7266 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7267 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7268 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7269 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7270 | |
| 7271 | // single window is removed but the window token remains focused |
| 7272 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7273 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7275 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7276 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7278 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7279 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7280 | |
| 7281 | // Both windows are removed |
| 7282 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7283 | mWindow->consumeFocusEvent(false); |
| 7284 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7285 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7286 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7287 | mWindow->assertNoEvents(); |
| 7288 | } |
| 7289 | |
| 7290 | // Focus request can be pending until one window becomes visible. |
| 7291 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7292 | // Request focus on an invisible mirror. |
| 7293 | mWindow->setVisible(false); |
| 7294 | mMirror->setVisible(false); |
| 7295 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7296 | setFocusedWindow(mMirror); |
| 7297 | |
| 7298 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7299 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7300 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7301 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7302 | |
| 7303 | mMirror->setVisible(true); |
| 7304 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7305 | |
| 7306 | // window gets focused |
| 7307 | mWindow->consumeFocusEvent(true); |
| 7308 | // window gets the pending key event |
| 7309 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7310 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7311 | |
| 7312 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7313 | protected: |
| 7314 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7315 | sp<FakeWindowHandle> mWindow; |
| 7316 | sp<FakeWindowHandle> mSecondWindow; |
| 7317 | |
| 7318 | void SetUp() override { |
| 7319 | InputDispatcherTest::SetUp(); |
| 7320 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7321 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7322 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7323 | mSecondWindow = |
| 7324 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7325 | mSecondWindow->setFocusable(true); |
| 7326 | |
| 7327 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7328 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7329 | |
| 7330 | setFocusedWindow(mWindow); |
| 7331 | mWindow->consumeFocusEvent(true); |
| 7332 | } |
| 7333 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7334 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
| 7335 | const NotifyPointerCaptureChangedArgs args = generatePointerCaptureChangedArgs(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7336 | mDispatcher->notifyPointerCaptureChanged(&args); |
| 7337 | } |
| 7338 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7339 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7340 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7341 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7342 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7343 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7344 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7345 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7346 | } |
| 7347 | }; |
| 7348 | |
| 7349 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7350 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7351 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7352 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7353 | mSecondWindow->assertNoEvents(); |
| 7354 | |
| 7355 | // Ensure that capture can be enabled from the focus window. |
| 7356 | requestAndVerifyPointerCapture(mWindow, true); |
| 7357 | |
| 7358 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7359 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7360 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7361 | |
| 7362 | // Ensure that capture can be disabled from the window with capture. |
| 7363 | requestAndVerifyPointerCapture(mWindow, false); |
| 7364 | } |
| 7365 | |
| 7366 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7367 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7368 | |
| 7369 | setFocusedWindow(mSecondWindow); |
| 7370 | |
| 7371 | // Ensure that the capture disabled event was sent first. |
| 7372 | mWindow->consumeCaptureEvent(false); |
| 7373 | mWindow->consumeFocusEvent(false); |
| 7374 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7375 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7376 | |
| 7377 | // 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] | 7378 | notifyPointerCaptureChanged({}); |
| 7379 | notifyPointerCaptureChanged(request); |
| 7380 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7381 | mWindow->assertNoEvents(); |
| 7382 | mSecondWindow->assertNoEvents(); |
| 7383 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7384 | } |
| 7385 | |
| 7386 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7387 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7388 | |
| 7389 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7390 | notifyPointerCaptureChanged({}); |
| 7391 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7392 | |
| 7393 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7394 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7395 | mWindow->consumeCaptureEvent(false); |
| 7396 | mWindow->assertNoEvents(); |
| 7397 | } |
| 7398 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7399 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7400 | requestAndVerifyPointerCapture(mWindow, true); |
| 7401 | |
| 7402 | // The first window loses focus. |
| 7403 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7404 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7405 | mWindow->consumeCaptureEvent(false); |
| 7406 | |
| 7407 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7408 | // arrives. |
| 7409 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7410 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7411 | |
| 7412 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7413 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7414 | |
| 7415 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7416 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7417 | |
| 7418 | mSecondWindow->consumeFocusEvent(true); |
| 7419 | mSecondWindow->consumeCaptureEvent(true); |
| 7420 | } |
| 7421 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7422 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7423 | // App repeatedly enables and disables capture. |
| 7424 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7425 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7426 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7427 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7428 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7429 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7430 | |
| 7431 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7432 | // first request is now stale, this should do nothing. |
| 7433 | notifyPointerCaptureChanged(firstRequest); |
| 7434 | mWindow->assertNoEvents(); |
| 7435 | |
| 7436 | // InputReader notifies that the second request was enabled. |
| 7437 | notifyPointerCaptureChanged(secondRequest); |
| 7438 | mWindow->consumeCaptureEvent(true); |
| 7439 | } |
| 7440 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7441 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7442 | requestAndVerifyPointerCapture(mWindow, true); |
| 7443 | |
| 7444 | // App toggles pointer capture off and on. |
| 7445 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7446 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7447 | |
| 7448 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7449 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7450 | |
| 7451 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7452 | // preceding "disable" request. |
| 7453 | notifyPointerCaptureChanged(enableRequest); |
| 7454 | |
| 7455 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7456 | // any notifications. |
| 7457 | mWindow->assertNoEvents(); |
| 7458 | } |
| 7459 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7460 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7461 | protected: |
| 7462 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7463 | |
| 7464 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7465 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7466 | |
| 7467 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7468 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7469 | |
| 7470 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7471 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7472 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7473 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7474 | MAXIMUM_OBSCURING_OPACITY); |
| 7475 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7476 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7477 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7478 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7479 | |
| 7480 | sp<FakeWindowHandle> mTouchWindow; |
| 7481 | |
| 7482 | virtual void SetUp() override { |
| 7483 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7484 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7485 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7486 | } |
| 7487 | |
| 7488 | virtual void TearDown() override { |
| 7489 | InputDispatcherTest::TearDown(); |
| 7490 | mTouchWindow.clear(); |
| 7491 | } |
| 7492 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7493 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 7494 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7495 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7496 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7497 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7498 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7499 | return window; |
| 7500 | } |
| 7501 | |
| 7502 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 7503 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7504 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7505 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7506 | // Generate an arbitrary PID based on the UID |
| 7507 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 7508 | return window; |
| 7509 | } |
| 7510 | |
| 7511 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
| 7512 | NotifyMotionArgs args = |
| 7513 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 7514 | ADISPLAY_ID_DEFAULT, points); |
| 7515 | mDispatcher->notifyMotion(&args); |
| 7516 | } |
| 7517 | }; |
| 7518 | |
| 7519 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7520 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7521 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7522 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7523 | |
| 7524 | touch(); |
| 7525 | |
| 7526 | mTouchWindow->assertNoEvents(); |
| 7527 | } |
| 7528 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7529 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7530 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7531 | const sp<FakeWindowHandle>& w = |
| 7532 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7533 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7534 | |
| 7535 | touch(); |
| 7536 | |
| 7537 | mTouchWindow->assertNoEvents(); |
| 7538 | } |
| 7539 | |
| 7540 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7541 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7542 | const sp<FakeWindowHandle>& w = |
| 7543 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7544 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7545 | |
| 7546 | touch(); |
| 7547 | |
| 7548 | w->assertNoEvents(); |
| 7549 | } |
| 7550 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7551 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7552 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7553 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7554 | |
| 7555 | touch(); |
| 7556 | |
| 7557 | mTouchWindow->consumeAnyMotionDown(); |
| 7558 | } |
| 7559 | |
| 7560 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7561 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7562 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7563 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7564 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7565 | |
| 7566 | touch({PointF{100, 100}}); |
| 7567 | |
| 7568 | mTouchWindow->consumeAnyMotionDown(); |
| 7569 | } |
| 7570 | |
| 7571 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7572 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7573 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7574 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7575 | |
| 7576 | touch(); |
| 7577 | |
| 7578 | mTouchWindow->consumeAnyMotionDown(); |
| 7579 | } |
| 7580 | |
| 7581 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 7582 | const sp<FakeWindowHandle>& w = |
| 7583 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7584 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7585 | |
| 7586 | touch(); |
| 7587 | |
| 7588 | mTouchWindow->consumeAnyMotionDown(); |
| 7589 | } |
| 7590 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7591 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 7592 | const sp<FakeWindowHandle>& w = |
| 7593 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7594 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7595 | |
| 7596 | touch(); |
| 7597 | |
| 7598 | w->assertNoEvents(); |
| 7599 | } |
| 7600 | |
| 7601 | /** |
| 7602 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 7603 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 7604 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 7605 | */ |
| 7606 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7607 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 7608 | const sp<FakeWindowHandle>& w = |
| 7609 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7610 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7611 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7612 | |
| 7613 | touch(); |
| 7614 | |
| 7615 | w->consumeMotionOutside(); |
| 7616 | } |
| 7617 | |
| 7618 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 7619 | const sp<FakeWindowHandle>& w = |
| 7620 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7621 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7622 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7623 | |
| 7624 | touch(); |
| 7625 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7626 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7627 | } |
| 7628 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7629 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7630 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7631 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7632 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7633 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7634 | |
| 7635 | touch(); |
| 7636 | |
| 7637 | mTouchWindow->consumeAnyMotionDown(); |
| 7638 | } |
| 7639 | |
| 7640 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 7641 | const sp<FakeWindowHandle>& w = |
| 7642 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7643 | MAXIMUM_OBSCURING_OPACITY); |
| 7644 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7645 | |
| 7646 | touch(); |
| 7647 | |
| 7648 | mTouchWindow->consumeAnyMotionDown(); |
| 7649 | } |
| 7650 | |
| 7651 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7652 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7653 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7654 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7655 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7656 | |
| 7657 | touch(); |
| 7658 | |
| 7659 | mTouchWindow->assertNoEvents(); |
| 7660 | } |
| 7661 | |
| 7662 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 7663 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 7664 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7665 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7666 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7667 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7668 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7669 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7670 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7671 | |
| 7672 | touch(); |
| 7673 | |
| 7674 | mTouchWindow->assertNoEvents(); |
| 7675 | } |
| 7676 | |
| 7677 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 7678 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 7679 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7680 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7681 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7682 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7683 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7684 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7685 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7686 | |
| 7687 | touch(); |
| 7688 | |
| 7689 | mTouchWindow->consumeAnyMotionDown(); |
| 7690 | } |
| 7691 | |
| 7692 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7693 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 7694 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7695 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7696 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7697 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7698 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7699 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7700 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7701 | |
| 7702 | touch(); |
| 7703 | |
| 7704 | mTouchWindow->consumeAnyMotionDown(); |
| 7705 | } |
| 7706 | |
| 7707 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 7708 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7709 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7710 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7711 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7712 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7713 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7714 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7715 | |
| 7716 | touch(); |
| 7717 | |
| 7718 | mTouchWindow->assertNoEvents(); |
| 7719 | } |
| 7720 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7721 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7722 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 7723 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7724 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7725 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7726 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7727 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7728 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7729 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7730 | |
| 7731 | touch(); |
| 7732 | |
| 7733 | mTouchWindow->assertNoEvents(); |
| 7734 | } |
| 7735 | |
| 7736 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7737 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 7738 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7739 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7740 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7741 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7742 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7743 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7744 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7745 | |
| 7746 | touch(); |
| 7747 | |
| 7748 | mTouchWindow->consumeAnyMotionDown(); |
| 7749 | } |
| 7750 | |
| 7751 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 7752 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7753 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7754 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7755 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7756 | |
| 7757 | touch(); |
| 7758 | |
| 7759 | mTouchWindow->consumeAnyMotionDown(); |
| 7760 | } |
| 7761 | |
| 7762 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 7763 | const sp<FakeWindowHandle>& w = |
| 7764 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7765 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7766 | |
| 7767 | touch(); |
| 7768 | |
| 7769 | mTouchWindow->consumeAnyMotionDown(); |
| 7770 | } |
| 7771 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7772 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7773 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 7774 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7775 | const sp<FakeWindowHandle>& w = |
| 7776 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 7777 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7778 | |
| 7779 | touch(); |
| 7780 | |
| 7781 | mTouchWindow->assertNoEvents(); |
| 7782 | } |
| 7783 | |
| 7784 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 7785 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7786 | const sp<FakeWindowHandle>& w = |
| 7787 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 7788 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7789 | |
| 7790 | touch(); |
| 7791 | |
| 7792 | mTouchWindow->consumeAnyMotionDown(); |
| 7793 | } |
| 7794 | |
| 7795 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7796 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 7797 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 7798 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7799 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7800 | OPACITY_ABOVE_THRESHOLD); |
| 7801 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7802 | |
| 7803 | touch(); |
| 7804 | |
| 7805 | mTouchWindow->consumeAnyMotionDown(); |
| 7806 | } |
| 7807 | |
| 7808 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7809 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 7810 | const sp<FakeWindowHandle>& w1 = |
| 7811 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7812 | OPACITY_BELOW_THRESHOLD); |
| 7813 | const sp<FakeWindowHandle>& w2 = |
| 7814 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7815 | OPACITY_BELOW_THRESHOLD); |
| 7816 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7817 | |
| 7818 | touch(); |
| 7819 | |
| 7820 | mTouchWindow->assertNoEvents(); |
| 7821 | } |
| 7822 | |
| 7823 | /** |
| 7824 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 7825 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 7826 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 7827 | */ |
| 7828 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7829 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 7830 | const sp<FakeWindowHandle>& wB = |
| 7831 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7832 | OPACITY_BELOW_THRESHOLD); |
| 7833 | const sp<FakeWindowHandle>& wC = |
| 7834 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7835 | OPACITY_BELOW_THRESHOLD); |
| 7836 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7837 | |
| 7838 | touch(); |
| 7839 | |
| 7840 | mTouchWindow->assertNoEvents(); |
| 7841 | } |
| 7842 | |
| 7843 | /** |
| 7844 | * This test is testing that a window from a different UID but with same application token doesn't |
| 7845 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 7846 | */ |
| 7847 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7848 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 7849 | const sp<FakeWindowHandle>& w = |
| 7850 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7851 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7852 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7853 | |
| 7854 | touch(); |
| 7855 | |
| 7856 | mTouchWindow->consumeAnyMotionDown(); |
| 7857 | } |
| 7858 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7859 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 7860 | protected: |
| 7861 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7862 | sp<FakeWindowHandle> mWindow; |
| 7863 | sp<FakeWindowHandle> mSecondWindow; |
| 7864 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7865 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7866 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 7867 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7868 | |
| 7869 | void SetUp() override { |
| 7870 | InputDispatcherTest::SetUp(); |
| 7871 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7872 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7873 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7874 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7875 | mSecondWindow = |
| 7876 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7877 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7878 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7879 | mSpyWindow = |
| 7880 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7881 | mSpyWindow->setSpy(true); |
| 7882 | mSpyWindow->setTrustedOverlay(true); |
| 7883 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 7884 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7885 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7886 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7887 | } |
| 7888 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7889 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 7890 | switch (fromSource) { |
| 7891 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 7892 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7893 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7894 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7895 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7896 | break; |
| 7897 | case AINPUT_SOURCE_STYLUS: |
| 7898 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7899 | injectMotionEvent( |
| 7900 | mDispatcher, |
| 7901 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 7902 | AINPUT_SOURCE_STYLUS) |
| 7903 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7904 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7905 | .x(50) |
| 7906 | .y(50)) |
| 7907 | .build())); |
| 7908 | break; |
| 7909 | case AINPUT_SOURCE_MOUSE: |
| 7910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7911 | injectMotionEvent( |
| 7912 | mDispatcher, |
| 7913 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7914 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 7915 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7916 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7917 | .x(50) |
| 7918 | .y(50)) |
| 7919 | .build())); |
| 7920 | break; |
| 7921 | default: |
| 7922 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 7923 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7924 | |
| 7925 | // Window should receive motion event. |
| 7926 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7927 | // Spy window should also receive motion event |
| 7928 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7929 | } |
| 7930 | |
| 7931 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 7932 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 7933 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7934 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7935 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7936 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7937 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7938 | |
| 7939 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7940 | mDragWindow = |
| 7941 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7942 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7943 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7944 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7945 | |
| 7946 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7947 | bool transferred = |
| 7948 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7949 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 7950 | if (transferred) { |
| 7951 | mWindow->consumeMotionCancel(); |
| 7952 | mDragWindow->consumeMotionDown(); |
| 7953 | } |
| 7954 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7955 | } |
| 7956 | }; |
| 7957 | |
| 7958 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7959 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7960 | |
| 7961 | // Move on window. |
| 7962 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7963 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7964 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7965 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7966 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7967 | mWindow->consumeDragEvent(false, 50, 50); |
| 7968 | mSecondWindow->assertNoEvents(); |
| 7969 | |
| 7970 | // Move to another window. |
| 7971 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7972 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7973 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 7974 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7975 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7976 | mWindow->consumeDragEvent(true, 150, 50); |
| 7977 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 7978 | |
| 7979 | // Move back to original window. |
| 7980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7981 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7982 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7983 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7984 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 7985 | mWindow->consumeDragEvent(false, 50, 50); |
| 7986 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 7987 | |
| 7988 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7989 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 7990 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7991 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 7992 | mWindow->assertNoEvents(); |
| 7993 | mSecondWindow->assertNoEvents(); |
| 7994 | } |
| 7995 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7996 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7997 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7998 | |
| 7999 | // No cancel event after drag start |
| 8000 | mSpyWindow->assertNoEvents(); |
| 8001 | |
| 8002 | const MotionEvent secondFingerDownEvent = |
| 8003 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8004 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8005 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8006 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8007 | .build(); |
| 8008 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8009 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8010 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8011 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8012 | |
| 8013 | // Receives cancel for first pointer after next pointer down |
| 8014 | mSpyWindow->consumeMotionCancel(); |
| 8015 | mSpyWindow->consumeMotionDown(); |
| 8016 | |
| 8017 | mSpyWindow->assertNoEvents(); |
| 8018 | } |
| 8019 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8020 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8021 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8022 | |
| 8023 | // Move on window. |
| 8024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8025 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8026 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8027 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8028 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8029 | mWindow->consumeDragEvent(false, 50, 50); |
| 8030 | mSecondWindow->assertNoEvents(); |
| 8031 | |
| 8032 | // Move to another window. |
| 8033 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8034 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8035 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8036 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8037 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8038 | mWindow->consumeDragEvent(true, 150, 50); |
| 8039 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8040 | |
| 8041 | // drop to another window. |
| 8042 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8043 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8044 | {150, 50})) |
| 8045 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8046 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8047 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8048 | mWindow->assertNoEvents(); |
| 8049 | mSecondWindow->assertNoEvents(); |
| 8050 | } |
| 8051 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8052 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8053 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8054 | |
| 8055 | // Move on window and keep button pressed. |
| 8056 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8057 | injectMotionEvent(mDispatcher, |
| 8058 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8059 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8060 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8061 | .build())) |
| 8062 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8063 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8064 | mWindow->consumeDragEvent(false, 50, 50); |
| 8065 | mSecondWindow->assertNoEvents(); |
| 8066 | |
| 8067 | // Move to another window and release button, expect to drop item. |
| 8068 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8069 | injectMotionEvent(mDispatcher, |
| 8070 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8071 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8072 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8073 | .build())) |
| 8074 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8075 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8076 | mWindow->assertNoEvents(); |
| 8077 | mSecondWindow->assertNoEvents(); |
| 8078 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8079 | |
| 8080 | // nothing to the window. |
| 8081 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8082 | injectMotionEvent(mDispatcher, |
| 8083 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 8084 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8085 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8086 | .build())) |
| 8087 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8088 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8089 | mWindow->assertNoEvents(); |
| 8090 | mSecondWindow->assertNoEvents(); |
| 8091 | } |
| 8092 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8093 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8094 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8095 | |
| 8096 | // Set second window invisible. |
| 8097 | mSecondWindow->setVisible(false); |
| 8098 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 8099 | |
| 8100 | // Move on window. |
| 8101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8102 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8103 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8104 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8105 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8106 | mWindow->consumeDragEvent(false, 50, 50); |
| 8107 | mSecondWindow->assertNoEvents(); |
| 8108 | |
| 8109 | // Move to another window. |
| 8110 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8111 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8112 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8113 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8114 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8115 | mWindow->consumeDragEvent(true, 150, 50); |
| 8116 | mSecondWindow->assertNoEvents(); |
| 8117 | |
| 8118 | // drop to another window. |
| 8119 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8120 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8121 | {150, 50})) |
| 8122 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8123 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8124 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 8125 | mWindow->assertNoEvents(); |
| 8126 | mSecondWindow->assertNoEvents(); |
| 8127 | } |
| 8128 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8129 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8130 | // Ensure window could track pointerIds if it didn't support split touch. |
| 8131 | mWindow->setPreventSplitting(true); |
| 8132 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8133 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8134 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8135 | {50, 50})) |
| 8136 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8137 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8138 | |
| 8139 | const MotionEvent secondFingerDownEvent = |
| 8140 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8141 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8142 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8143 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8144 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8145 | .build(); |
| 8146 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8147 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8148 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8149 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8150 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8151 | |
| 8152 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8153 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8154 | } |
| 8155 | |
| 8156 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 8157 | // First down on second window. |
| 8158 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8159 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8160 | {150, 50})) |
| 8161 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8162 | |
| 8163 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8164 | |
| 8165 | // Second down on first window. |
| 8166 | const MotionEvent secondFingerDownEvent = |
| 8167 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8168 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8169 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8170 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8171 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8172 | .build(); |
| 8173 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8174 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8175 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8176 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8177 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8178 | |
| 8179 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8180 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8181 | |
| 8182 | // Move on window. |
| 8183 | const MotionEvent secondFingerMoveEvent = |
| 8184 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8185 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8186 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8187 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8188 | .build(); |
| 8189 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8190 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8191 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8192 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8193 | mWindow->consumeDragEvent(false, 50, 50); |
| 8194 | mSecondWindow->consumeMotionMove(); |
| 8195 | |
| 8196 | // Release the drag pointer should perform drop. |
| 8197 | const MotionEvent secondFingerUpEvent = |
| 8198 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8199 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8200 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8201 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8202 | .build(); |
| 8203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8204 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8205 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8206 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8207 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 8208 | mWindow->assertNoEvents(); |
| 8209 | mSecondWindow->consumeMotionMove(); |
| 8210 | } |
| 8211 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8212 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8213 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8214 | |
| 8215 | // Update window of second display. |
| 8216 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8217 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8218 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8219 | |
| 8220 | // Let second display has a touch state. |
| 8221 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8222 | injectMotionEvent(mDispatcher, |
| 8223 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8224 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8225 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8226 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8227 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame^] | 8228 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8229 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8230 | // Update window again. |
| 8231 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8232 | |
| 8233 | // Move on window. |
| 8234 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8235 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8236 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8237 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8238 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8239 | mWindow->consumeDragEvent(false, 50, 50); |
| 8240 | mSecondWindow->assertNoEvents(); |
| 8241 | |
| 8242 | // Move to another window. |
| 8243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8244 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8245 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8246 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8247 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8248 | mWindow->consumeDragEvent(true, 150, 50); |
| 8249 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8250 | |
| 8251 | // drop to another window. |
| 8252 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8253 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8254 | {150, 50})) |
| 8255 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8256 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8257 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8258 | mWindow->assertNoEvents(); |
| 8259 | mSecondWindow->assertNoEvents(); |
| 8260 | } |
| 8261 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8262 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8263 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8264 | // Move on window. |
| 8265 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8266 | injectMotionEvent(mDispatcher, |
| 8267 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8268 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8269 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8270 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8271 | .x(50) |
| 8272 | .y(50)) |
| 8273 | .build())) |
| 8274 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8275 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8276 | mWindow->consumeDragEvent(false, 50, 50); |
| 8277 | mSecondWindow->assertNoEvents(); |
| 8278 | |
| 8279 | // Move to another window. |
| 8280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8281 | injectMotionEvent(mDispatcher, |
| 8282 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8283 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8284 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8285 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8286 | .x(150) |
| 8287 | .y(50)) |
| 8288 | .build())) |
| 8289 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8290 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8291 | mWindow->consumeDragEvent(true, 150, 50); |
| 8292 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8293 | |
| 8294 | // drop to another window. |
| 8295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8296 | injectMotionEvent(mDispatcher, |
| 8297 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8298 | .buttonState(0) |
| 8299 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8300 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8301 | .x(150) |
| 8302 | .y(50)) |
| 8303 | .build())) |
| 8304 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8305 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8306 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8307 | mWindow->assertNoEvents(); |
| 8308 | mSecondWindow->assertNoEvents(); |
| 8309 | } |
| 8310 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8311 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8312 | |
| 8313 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8314 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8315 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8316 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8317 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8318 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8319 | window->setFocusable(true); |
| 8320 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8321 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8322 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8323 | |
| 8324 | // With the flag set, window should not get any input |
| 8325 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 8326 | mDispatcher->notifyKey(&keyArgs); |
| 8327 | window->assertNoEvents(); |
| 8328 | |
| 8329 | NotifyMotionArgs motionArgs = |
| 8330 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8331 | ADISPLAY_ID_DEFAULT); |
| 8332 | mDispatcher->notifyMotion(&motionArgs); |
| 8333 | window->assertNoEvents(); |
| 8334 | |
| 8335 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8336 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8337 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8338 | |
| 8339 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 8340 | mDispatcher->notifyKey(&keyArgs); |
| 8341 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8342 | |
| 8343 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8344 | ADISPLAY_ID_DEFAULT); |
| 8345 | mDispatcher->notifyMotion(&motionArgs); |
| 8346 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8347 | window->assertNoEvents(); |
| 8348 | } |
| 8349 | |
| 8350 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8351 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8352 | std::make_shared<FakeApplicationHandle>(); |
| 8353 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8354 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8355 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8356 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8357 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8358 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8359 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8360 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8361 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8362 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8363 | window->setOwnerInfo(222, 222); |
| 8364 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8365 | window->setFocusable(true); |
| 8366 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8367 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8368 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8369 | |
| 8370 | // With the flag set, window should not get any input |
| 8371 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 8372 | mDispatcher->notifyKey(&keyArgs); |
| 8373 | window->assertNoEvents(); |
| 8374 | |
| 8375 | NotifyMotionArgs motionArgs = |
| 8376 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8377 | ADISPLAY_ID_DEFAULT); |
| 8378 | mDispatcher->notifyMotion(&motionArgs); |
| 8379 | window->assertNoEvents(); |
| 8380 | |
| 8381 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8382 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8383 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8384 | |
| 8385 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 8386 | mDispatcher->notifyKey(&keyArgs); |
| 8387 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8388 | |
| 8389 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8390 | ADISPLAY_ID_DEFAULT); |
| 8391 | mDispatcher->notifyMotion(&motionArgs); |
| 8392 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8393 | window->assertNoEvents(); |
| 8394 | } |
| 8395 | |
| 8396 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8397 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8398 | std::make_shared<FakeApplicationHandle>(); |
| 8399 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8400 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8401 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8402 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8403 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8404 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8405 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8406 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8407 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8408 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8409 | window->setOwnerInfo(222, 222); |
| 8410 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8411 | window->setFocusable(true); |
| 8412 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8413 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8414 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8415 | |
| 8416 | // With the flag set, window should not get any input |
| 8417 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 8418 | mDispatcher->notifyKey(&keyArgs); |
| 8419 | window->assertNoEvents(); |
| 8420 | |
| 8421 | NotifyMotionArgs motionArgs = |
| 8422 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8423 | ADISPLAY_ID_DEFAULT); |
| 8424 | mDispatcher->notifyMotion(&motionArgs); |
| 8425 | window->assertNoEvents(); |
| 8426 | |
| 8427 | // When the window is no longer obscured because it went on top, it should get input |
| 8428 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8429 | |
| 8430 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 8431 | mDispatcher->notifyKey(&keyArgs); |
| 8432 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8433 | |
| 8434 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8435 | ADISPLAY_ID_DEFAULT); |
| 8436 | mDispatcher->notifyMotion(&motionArgs); |
| 8437 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8438 | window->assertNoEvents(); |
| 8439 | } |
| 8440 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8441 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8442 | protected: |
| 8443 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8444 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8445 | sp<FakeWindowHandle> mWindow; |
| 8446 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8447 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8448 | |
| 8449 | void SetUp() override { |
| 8450 | InputDispatcherTest::SetUp(); |
| 8451 | |
| 8452 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8453 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8454 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8455 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8456 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8457 | mSecondWindow = |
| 8458 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8459 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8460 | mThirdWindow = |
| 8461 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8462 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8463 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8464 | |
| 8465 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8466 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8467 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8468 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8469 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8470 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8471 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8472 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8473 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8474 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8475 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8476 | mThirdWindow->assertNoEvents(); |
| 8477 | } |
| 8478 | |
| 8479 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8480 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8481 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8482 | SECOND_DISPLAY_ID)) { |
| 8483 | mWindow->assertNoEvents(); |
| 8484 | mSecondWindow->assertNoEvents(); |
| 8485 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8486 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8487 | } |
| 8488 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8489 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 8490 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8491 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8492 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8493 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8494 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8495 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8496 | } |
| 8497 | }; |
| 8498 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8499 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8500 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8501 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8502 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8503 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8504 | } |
| 8505 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8506 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8507 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8508 | int32_t ownerPid = windowInfo.ownerPid; |
| 8509 | int32_t ownerUid = windowInfo.ownerUid; |
| 8510 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 8511 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8512 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8513 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8514 | mWindow->assertNoEvents(); |
| 8515 | mSecondWindow->assertNoEvents(); |
| 8516 | } |
| 8517 | |
| 8518 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8519 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8520 | int32_t ownerPid = windowInfo.ownerPid; |
| 8521 | int32_t ownerUid = windowInfo.ownerUid; |
| 8522 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8523 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8524 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8525 | } |
| 8526 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8527 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8528 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8529 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8530 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8531 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8532 | mWindow->assertNoEvents(); |
| 8533 | mSecondWindow->assertNoEvents(); |
| 8534 | } |
| 8535 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8536 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8537 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8538 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8539 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8540 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8541 | mWindow->assertNoEvents(); |
| 8542 | mSecondWindow->assertNoEvents(); |
| 8543 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8544 | } |
| 8545 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8546 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8547 | // Interact with the window first. |
| 8548 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8549 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8550 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8551 | |
| 8552 | // Then remove focus. |
| 8553 | mWindow->setFocusable(false); |
| 8554 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8555 | |
| 8556 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8557 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8558 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8559 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8560 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8561 | } |
| 8562 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8563 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8564 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8565 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8566 | std::shared_ptr<FakeApplicationHandle> application = |
| 8567 | std::make_shared<FakeApplicationHandle>(); |
| 8568 | std::string name = "Fake Spy "; |
| 8569 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8570 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8571 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8572 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8573 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8574 | return spy; |
| 8575 | } |
| 8576 | |
| 8577 | sp<FakeWindowHandle> createForeground() { |
| 8578 | std::shared_ptr<FakeApplicationHandle> application = |
| 8579 | std::make_shared<FakeApplicationHandle>(); |
| 8580 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8581 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 8582 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8583 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8584 | return window; |
| 8585 | } |
| 8586 | |
| 8587 | private: |
| 8588 | int mSpyCount{0}; |
| 8589 | }; |
| 8590 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8591 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8592 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8593 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 8594 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8595 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 8596 | ScopedSilentDeath _silentDeath; |
| 8597 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8598 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8599 | spy->setTrustedOverlay(false); |
| 8600 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 8601 | ".* not a trusted overlay"); |
| 8602 | } |
| 8603 | |
| 8604 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8605 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 8606 | */ |
| 8607 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8608 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8609 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 8610 | |
| 8611 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8612 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8613 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8614 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8615 | } |
| 8616 | |
| 8617 | /** |
| 8618 | * Verify the order in which different input windows receive events. The touched foreground window |
| 8619 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 8620 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 8621 | * receive events before ones belows it. |
| 8622 | * |
| 8623 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 8624 | * spy1, spy2, window, spy3. |
| 8625 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 8626 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 8627 | * window. |
| 8628 | */ |
| 8629 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 8630 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8631 | auto spy1 = createSpy(); |
| 8632 | auto spy2 = createSpy(); |
| 8633 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8634 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 8635 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 8636 | const size_t numChannels = channels.size(); |
| 8637 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 8638 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8639 | if (!epollFd.ok()) { |
| 8640 | FAIL() << "Failed to create epoll fd"; |
| 8641 | } |
| 8642 | |
| 8643 | for (size_t i = 0; i < numChannels; i++) { |
| 8644 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 8645 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 8646 | FAIL() << "Failed to add fd to epoll"; |
| 8647 | } |
| 8648 | } |
| 8649 | |
| 8650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8651 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8652 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8653 | |
| 8654 | std::vector<size_t> eventOrder; |
| 8655 | std::vector<struct epoll_event> events(numChannels); |
| 8656 | for (;;) { |
| 8657 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 8658 | (100ms).count()); |
| 8659 | if (nFds < 0) { |
| 8660 | FAIL() << "Failed to call epoll_wait"; |
| 8661 | } |
| 8662 | if (nFds == 0) { |
| 8663 | break; // epoll_wait timed out |
| 8664 | } |
| 8665 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 8666 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 8667 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8668 | channels[i]->consumeMotionDown(); |
| 8669 | } |
| 8670 | } |
| 8671 | |
| 8672 | // Verify the order in which the events were received. |
| 8673 | EXPECT_EQ(3u, eventOrder.size()); |
| 8674 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 8675 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 8676 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 8677 | } |
| 8678 | |
| 8679 | /** |
| 8680 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 8681 | */ |
| 8682 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 8683 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8684 | auto spy = createSpy(); |
| 8685 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8686 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8687 | |
| 8688 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8689 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8690 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8691 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8692 | spy->assertNoEvents(); |
| 8693 | } |
| 8694 | |
| 8695 | /** |
| 8696 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 8697 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 8698 | * to the window. |
| 8699 | */ |
| 8700 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 8701 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8702 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8703 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 8704 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8705 | |
| 8706 | // Inject an event outside the spy window's touchable region. |
| 8707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8708 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8709 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8710 | window->consumeMotionDown(); |
| 8711 | spy->assertNoEvents(); |
| 8712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8713 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8714 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8715 | window->consumeMotionUp(); |
| 8716 | spy->assertNoEvents(); |
| 8717 | |
| 8718 | // Inject an event inside the spy window's touchable region. |
| 8719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8720 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8721 | {5, 10})) |
| 8722 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8723 | window->consumeMotionDown(); |
| 8724 | spy->consumeMotionDown(); |
| 8725 | } |
| 8726 | |
| 8727 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8728 | * 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] | 8729 | * 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] | 8730 | */ |
| 8731 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 8732 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8733 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8734 | auto spy = createSpy(); |
| 8735 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8736 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8737 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 8738 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8739 | |
| 8740 | // Inject an event outside the spy window's frame and touchable region. |
| 8741 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8742 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8743 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8744 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8745 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8746 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8747 | } |
| 8748 | |
| 8749 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8750 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 8751 | * pointers that are down within its bounds. |
| 8752 | */ |
| 8753 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 8754 | auto windowLeft = createForeground(); |
| 8755 | windowLeft->setFrame({0, 0, 100, 200}); |
| 8756 | auto windowRight = createForeground(); |
| 8757 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8758 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8759 | spy->setFrame({0, 0, 200, 200}); |
| 8760 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 8761 | |
| 8762 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8763 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8764 | {50, 50})) |
| 8765 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8766 | windowLeft->consumeMotionDown(); |
| 8767 | spy->consumeMotionDown(); |
| 8768 | |
| 8769 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8770 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8771 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8772 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8773 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8774 | .build(); |
| 8775 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8776 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8777 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8778 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8779 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8780 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8781 | } |
| 8782 | |
| 8783 | /** |
| 8784 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 8785 | * the spy should receive the second pointer with ACTION_DOWN. |
| 8786 | */ |
| 8787 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 8788 | auto window = createForeground(); |
| 8789 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8790 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8791 | spyRight->setFrame({100, 0, 200, 200}); |
| 8792 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 8793 | |
| 8794 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8795 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8796 | {50, 50})) |
| 8797 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8798 | window->consumeMotionDown(); |
| 8799 | spyRight->assertNoEvents(); |
| 8800 | |
| 8801 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8802 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8803 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8804 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8805 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8806 | .build(); |
| 8807 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8808 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8809 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8810 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8811 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8812 | spyRight->consumeMotionDown(); |
| 8813 | } |
| 8814 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8815 | /** |
| 8816 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 8817 | * windows should be allowed to control split touch. |
| 8818 | */ |
| 8819 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8820 | // 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] | 8821 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8822 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8823 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8824 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8825 | auto window = createForeground(); |
| 8826 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8827 | |
| 8828 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8829 | |
| 8830 | // First finger down, no window touched. |
| 8831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8832 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8833 | {100, 200})) |
| 8834 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8835 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8836 | window->assertNoEvents(); |
| 8837 | |
| 8838 | // Second finger down on window, the window should receive touch down. |
| 8839 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8840 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8841 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8842 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8843 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8844 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8845 | .build(); |
| 8846 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8847 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8848 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8849 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8850 | |
| 8851 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8852 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8853 | } |
| 8854 | |
| 8855 | /** |
| 8856 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 8857 | * do not receive key events. |
| 8858 | */ |
| 8859 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8860 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8861 | spy->setFocusable(false); |
| 8862 | |
| 8863 | auto window = createForeground(); |
| 8864 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8865 | setFocusedWindow(window); |
| 8866 | window->consumeFocusEvent(true); |
| 8867 | |
| 8868 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 8869 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8870 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8871 | |
| 8872 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 8873 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8874 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8875 | |
| 8876 | spy->assertNoEvents(); |
| 8877 | } |
| 8878 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8879 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 8880 | |
| 8881 | /** |
| 8882 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 8883 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 8884 | */ |
| 8885 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 8886 | auto window = createForeground(); |
| 8887 | auto spy1 = createSpy(); |
| 8888 | auto spy2 = createSpy(); |
| 8889 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 8890 | |
| 8891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8892 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8893 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8894 | window->consumeMotionDown(); |
| 8895 | spy1->consumeMotionDown(); |
| 8896 | spy2->consumeMotionDown(); |
| 8897 | |
| 8898 | // Pilfer pointers from the second spy window. |
| 8899 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 8900 | spy2->assertNoEvents(); |
| 8901 | spy1->consumeMotionCancel(); |
| 8902 | window->consumeMotionCancel(); |
| 8903 | |
| 8904 | // The rest of the gesture should only be sent to the second spy window. |
| 8905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8906 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8907 | ADISPLAY_ID_DEFAULT)) |
| 8908 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8909 | spy2->consumeMotionMove(); |
| 8910 | spy1->assertNoEvents(); |
| 8911 | window->assertNoEvents(); |
| 8912 | } |
| 8913 | |
| 8914 | /** |
| 8915 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 8916 | * in the middle of the gesture. |
| 8917 | */ |
| 8918 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 8919 | auto window = createForeground(); |
| 8920 | auto spy = createSpy(); |
| 8921 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8922 | |
| 8923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8924 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8925 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8926 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8927 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8928 | |
| 8929 | window->releaseChannel(); |
| 8930 | |
| 8931 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8932 | |
| 8933 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8934 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8935 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8936 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8937 | } |
| 8938 | |
| 8939 | /** |
| 8940 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 8941 | * the spy, but not to any other windows. |
| 8942 | */ |
| 8943 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 8944 | auto spy = createSpy(); |
| 8945 | auto window = createForeground(); |
| 8946 | |
| 8947 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8948 | |
| 8949 | // First finger down on the window and the spy. |
| 8950 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8951 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8952 | {100, 200})) |
| 8953 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8954 | spy->consumeMotionDown(); |
| 8955 | window->consumeMotionDown(); |
| 8956 | |
| 8957 | // Spy window pilfers the pointers. |
| 8958 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 8959 | window->consumeMotionCancel(); |
| 8960 | |
| 8961 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 8962 | const MotionEvent secondFingerDownEvent = |
| 8963 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8964 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8965 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8966 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8967 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8968 | .build(); |
| 8969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8970 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8971 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8972 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8973 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8974 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8975 | |
| 8976 | // Third finger goes down outside all windows, so injection should fail. |
| 8977 | const MotionEvent thirdFingerDownEvent = |
| 8978 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8979 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8980 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8981 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8982 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 8983 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8984 | .build(); |
| 8985 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 8986 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8987 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8988 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8989 | |
| 8990 | spy->assertNoEvents(); |
| 8991 | window->assertNoEvents(); |
| 8992 | } |
| 8993 | |
| 8994 | /** |
| 8995 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 8996 | */ |
| 8997 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 8998 | auto spy = createSpy(); |
| 8999 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9000 | auto window = createForeground(); |
| 9001 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9002 | |
| 9003 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9004 | |
| 9005 | // First finger down on the window only |
| 9006 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9007 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9008 | {150, 150})) |
| 9009 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9010 | window->consumeMotionDown(); |
| 9011 | |
| 9012 | // Second finger down on the spy and window |
| 9013 | const MotionEvent secondFingerDownEvent = |
| 9014 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9015 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9016 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9017 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9018 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9019 | .build(); |
| 9020 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9021 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9022 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9023 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9024 | spy->consumeMotionDown(); |
| 9025 | window->consumeMotionPointerDown(1); |
| 9026 | |
| 9027 | // Third finger down on the spy and window |
| 9028 | const MotionEvent thirdFingerDownEvent = |
| 9029 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9030 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9031 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9032 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9033 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 9034 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9035 | .build(); |
| 9036 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9037 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9038 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9039 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9040 | spy->consumeMotionPointerDown(1); |
| 9041 | window->consumeMotionPointerDown(2); |
| 9042 | |
| 9043 | // Spy window pilfers the pointers. |
| 9044 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9045 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9046 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9047 | |
| 9048 | spy->assertNoEvents(); |
| 9049 | window->assertNoEvents(); |
| 9050 | } |
| 9051 | |
| 9052 | /** |
| 9053 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 9054 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 9055 | * window, then that window should receive ACTION_CANCEL. |
| 9056 | */ |
| 9057 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 9058 | auto spy = createSpy(); |
| 9059 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9060 | auto window = createForeground(); |
| 9061 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9062 | |
| 9063 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9064 | |
| 9065 | // First finger down on both spy and window |
| 9066 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9067 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9068 | {10, 10})) |
| 9069 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9070 | window->consumeMotionDown(); |
| 9071 | spy->consumeMotionDown(); |
| 9072 | |
| 9073 | // Second finger down on the spy and window |
| 9074 | const MotionEvent secondFingerDownEvent = |
| 9075 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9076 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9077 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9078 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9079 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9080 | .build(); |
| 9081 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9082 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9083 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9084 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9085 | spy->consumeMotionPointerDown(1); |
| 9086 | window->consumeMotionPointerDown(1); |
| 9087 | |
| 9088 | // Spy window pilfers the pointers. |
| 9089 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9090 | window->consumeMotionCancel(); |
| 9091 | |
| 9092 | spy->assertNoEvents(); |
| 9093 | window->assertNoEvents(); |
| 9094 | } |
| 9095 | |
| 9096 | /** |
| 9097 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 9098 | * be sent to other windows |
| 9099 | */ |
| 9100 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 9101 | auto spy = createSpy(); |
| 9102 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9103 | auto window = createForeground(); |
| 9104 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9105 | |
| 9106 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9107 | |
| 9108 | // First finger down on both window and spy |
| 9109 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9110 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9111 | {10, 10})) |
| 9112 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9113 | window->consumeMotionDown(); |
| 9114 | spy->consumeMotionDown(); |
| 9115 | |
| 9116 | // Spy window pilfers the pointers. |
| 9117 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9118 | window->consumeMotionCancel(); |
| 9119 | |
| 9120 | // Second finger down on the window only |
| 9121 | const MotionEvent secondFingerDownEvent = |
| 9122 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9123 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9124 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9125 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9126 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9127 | .build(); |
| 9128 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9129 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9130 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9131 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9132 | window->consumeMotionDown(); |
| 9133 | window->assertNoEvents(); |
| 9134 | |
| 9135 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 9136 | spy->consumeMotionMove(); |
| 9137 | spy->assertNoEvents(); |
| 9138 | } |
| 9139 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9140 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 9141 | public: |
| 9142 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 9143 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9144 | std::make_shared<FakeApplicationHandle>(); |
| 9145 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9146 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 9147 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9148 | overlay->setFocusable(false); |
| 9149 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9150 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9151 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9152 | overlay->setTrustedOverlay(true); |
| 9153 | |
| 9154 | std::shared_ptr<FakeApplicationHandle> application = |
| 9155 | std::make_shared<FakeApplicationHandle>(); |
| 9156 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9157 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9158 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9159 | window->setFocusable(true); |
| 9160 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9161 | |
| 9162 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9163 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9164 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9165 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9166 | return {std::move(overlay), std::move(window)}; |
| 9167 | } |
| 9168 | |
| 9169 | void sendFingerEvent(int32_t action) { |
| 9170 | NotifyMotionArgs motionArgs = |
| 9171 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9172 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}}); |
| 9173 | mDispatcher->notifyMotion(&motionArgs); |
| 9174 | } |
| 9175 | |
| 9176 | void sendStylusEvent(int32_t action) { |
| 9177 | NotifyMotionArgs motionArgs = |
| 9178 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9179 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9180 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9181 | mDispatcher->notifyMotion(&motionArgs); |
| 9182 | } |
| 9183 | }; |
| 9184 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9185 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9186 | |
| 9187 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 9188 | ScopedSilentDeath _silentDeath; |
| 9189 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9190 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9191 | overlay->setTrustedOverlay(false); |
| 9192 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9193 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9194 | ".* not a trusted overlay"); |
| 9195 | } |
| 9196 | |
| 9197 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9198 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9199 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9200 | |
| 9201 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9202 | overlay->consumeMotionDown(); |
| 9203 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9204 | overlay->consumeMotionUp(); |
| 9205 | |
| 9206 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9207 | window->consumeMotionDown(); |
| 9208 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9209 | window->consumeMotionUp(); |
| 9210 | |
| 9211 | overlay->assertNoEvents(); |
| 9212 | window->assertNoEvents(); |
| 9213 | } |
| 9214 | |
| 9215 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9216 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9217 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9218 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9219 | |
| 9220 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9221 | overlay->consumeMotionDown(); |
| 9222 | window->consumeMotionDown(); |
| 9223 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9224 | overlay->consumeMotionUp(); |
| 9225 | window->consumeMotionUp(); |
| 9226 | |
| 9227 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9228 | window->consumeMotionDown(); |
| 9229 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9230 | window->consumeMotionUp(); |
| 9231 | |
| 9232 | overlay->assertNoEvents(); |
| 9233 | window->assertNoEvents(); |
| 9234 | } |
| 9235 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9236 | /** |
| 9237 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9238 | * The scenario is as follows: |
| 9239 | * - The stylus interceptor overlay is configured as a spy window. |
| 9240 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9241 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9242 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9243 | */ |
| 9244 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9245 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9246 | overlay->setSpy(true); |
| 9247 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9248 | |
| 9249 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9250 | overlay->consumeMotionDown(); |
| 9251 | window->consumeMotionDown(); |
| 9252 | |
| 9253 | // The interceptor pilfers the pointers. |
| 9254 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9255 | window->consumeMotionCancel(); |
| 9256 | |
| 9257 | // The interceptor configures itself so that it is no longer a spy. |
| 9258 | overlay->setSpy(false); |
| 9259 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9260 | |
| 9261 | // It continues to receive the rest of the stylus gesture. |
| 9262 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9263 | overlay->consumeMotionMove(); |
| 9264 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9265 | overlay->consumeMotionUp(); |
| 9266 | |
| 9267 | window->assertNoEvents(); |
| 9268 | } |
| 9269 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9270 | struct User { |
| 9271 | int32_t mPid; |
| 9272 | int32_t mUid; |
| 9273 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9274 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9275 | |
| 9276 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 9277 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9278 | |
| 9279 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9280 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9281 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9282 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9283 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9284 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9285 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9286 | } |
| 9287 | |
| 9288 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9289 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9290 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9291 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9292 | mPolicyFlags); |
| 9293 | } |
| 9294 | |
| 9295 | sp<FakeWindowHandle> createWindow() const { |
| 9296 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9297 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9298 | sp<FakeWindowHandle> window = |
| 9299 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9300 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9301 | window->setOwnerInfo(mPid, mUid); |
| 9302 | return window; |
| 9303 | } |
| 9304 | }; |
| 9305 | |
| 9306 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9307 | |
| 9308 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 9309 | auto owner = User(mDispatcher, 10, 11); |
| 9310 | auto window = owner.createWindow(); |
| 9311 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9312 | |
| 9313 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9314 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9315 | window->consumeMotionDown(); |
| 9316 | |
| 9317 | setFocusedWindow(window); |
| 9318 | window->consumeFocusEvent(true); |
| 9319 | |
| 9320 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9321 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9322 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9323 | } |
| 9324 | |
| 9325 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 9326 | auto owner = User(mDispatcher, 10, 11); |
| 9327 | auto window = owner.createWindow(); |
| 9328 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9329 | |
| 9330 | auto rando = User(mDispatcher, 20, 21); |
| 9331 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9332 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9333 | |
| 9334 | setFocusedWindow(window); |
| 9335 | window->consumeFocusEvent(true); |
| 9336 | |
| 9337 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9338 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9339 | window->assertNoEvents(); |
| 9340 | } |
| 9341 | |
| 9342 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 9343 | auto owner = User(mDispatcher, 10, 11); |
| 9344 | auto window = owner.createWindow(); |
| 9345 | auto spy = owner.createWindow(); |
| 9346 | spy->setSpy(true); |
| 9347 | spy->setTrustedOverlay(true); |
| 9348 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9349 | |
| 9350 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9351 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9352 | spy->consumeMotionDown(); |
| 9353 | window->consumeMotionDown(); |
| 9354 | } |
| 9355 | |
| 9356 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 9357 | auto owner = User(mDispatcher, 10, 11); |
| 9358 | auto window = owner.createWindow(); |
| 9359 | |
| 9360 | auto rando = User(mDispatcher, 20, 21); |
| 9361 | auto randosSpy = rando.createWindow(); |
| 9362 | randosSpy->setSpy(true); |
| 9363 | randosSpy->setTrustedOverlay(true); |
| 9364 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9365 | |
| 9366 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9367 | // not receive the event. |
| 9368 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9369 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9370 | randosSpy->assertNoEvents(); |
| 9371 | window->consumeMotionDown(); |
| 9372 | } |
| 9373 | |
| 9374 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 9375 | auto owner = User(mDispatcher, 10, 11); |
| 9376 | auto window = owner.createWindow(); |
| 9377 | |
| 9378 | auto rando = User(mDispatcher, 20, 21); |
| 9379 | auto randosSpy = rando.createWindow(); |
| 9380 | randosSpy->setSpy(true); |
| 9381 | randosSpy->setTrustedOverlay(true); |
| 9382 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9383 | |
| 9384 | // A user that has injection permission can inject into any window. |
| 9385 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9386 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9387 | ADISPLAY_ID_DEFAULT)); |
| 9388 | randosSpy->consumeMotionDown(); |
| 9389 | window->consumeMotionDown(); |
| 9390 | |
| 9391 | setFocusedWindow(randosSpy); |
| 9392 | randosSpy->consumeFocusEvent(true); |
| 9393 | |
| 9394 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9395 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9396 | window->assertNoEvents(); |
| 9397 | } |
| 9398 | |
| 9399 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 9400 | auto owner = User(mDispatcher, 10, 11); |
| 9401 | auto window = owner.createWindow(); |
| 9402 | |
| 9403 | auto rando = User(mDispatcher, 20, 21); |
| 9404 | auto randosWindow = rando.createWindow(); |
| 9405 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9406 | randosWindow->setWatchOutsideTouch(true); |
| 9407 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9408 | |
| 9409 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 9410 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9411 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9412 | window->consumeMotionDown(); |
| 9413 | randosWindow->consumeMotionOutside(); |
| 9414 | } |
| 9415 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9416 | } // namespace android::inputdispatcher |