| 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" | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 18 | #include "../BlockingQueue.h" | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 19 |  | 
| Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 20 | #include <NotifyArgsBuilders.h> | 
| Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 21 | #include <android-base/properties.h> | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 22 | #include <android-base/silent_death_test.h> | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 24 | #include <android-base/thread_annotations.h> | 
| Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 25 | #include <binder/Binder.h> | 
| Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 26 | #include <fcntl.h> | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 27 | #include <gmock/gmock.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 28 | #include <gtest/gtest.h> | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 29 | #include <input/Input.h> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 | #include <linux/input.h> | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 31 | #include <sys/epoll.h> | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 32 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 33 | #include <cinttypes> | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 34 | #include <compare> | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 35 | #include <thread> | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 36 | #include <unordered_set> | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 37 | #include <vector> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 38 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 39 | using android::base::StringPrintf; | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 40 | using android::gui::FocusRequest; | 
|  | 41 | using android::gui::TouchOcclusionMode; | 
|  | 42 | using android::gui::WindowInfo; | 
|  | 43 | using android::gui::WindowInfoHandle; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 44 | using android::os::InputEventInjectionResult; | 
|  | 45 | using android::os::InputEventInjectionSync; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 46 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 47 | namespace android::inputdispatcher { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 48 |  | 
| Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 49 | using namespace ftl::flag_operators; | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 50 | using testing::AllOf; | 
| Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 51 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 52 | // An arbitrary time value. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 53 | static constexpr nsecs_t ARBITRARY_TIME = 1234; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 54 |  | 
|  | 55 | // An arbitrary device id. | 
| Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 56 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 57 | static constexpr int32_t SECOND_DEVICE_ID = 2; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 |  | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 59 | // An arbitrary display id. | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 60 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; | 
|  | 61 | static constexpr int32_t SECOND_DISPLAY_ID = 1; | 
| Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 62 |  | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 63 | // Ensure common actions are interchangeable between keys and motions for convenience. | 
|  | 64 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); | 
|  | 65 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 66 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; | 
|  | 67 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; | 
|  | 68 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; | 
|  | 69 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; | 
| Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 70 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 71 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 72 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 73 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 74 | /** | 
|  | 75 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the | 
|  | 76 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at | 
|  | 77 | * index 0) is the new pointer going down. The same pointer could have been placed at a different | 
|  | 78 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In | 
|  | 79 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if | 
|  | 80 | * pointer id=0 leaves but the pointer id=1 remains. | 
|  | 81 | */ | 
|  | 82 | static constexpr int32_t POINTER_0_DOWN = | 
|  | 83 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 84 | static constexpr int32_t POINTER_1_DOWN = | 
|  | 85 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 86 | static constexpr int32_t POINTER_2_DOWN = | 
|  | 87 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 88 | static constexpr int32_t POINTER_3_DOWN = | 
|  | 89 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 90 | static constexpr int32_t POINTER_0_UP = | 
|  | 91 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 92 | static constexpr int32_t POINTER_1_UP = | 
|  | 93 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 94 | static constexpr int32_t POINTER_2_UP = | 
|  | 95 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 96 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 97 | // The default pid and uid for windows created on the primary display by the test. | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 98 | static constexpr gui::Pid WINDOW_PID{999}; | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 99 | static constexpr gui::Uid WINDOW_UID{1001}; | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 100 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 101 | // The default pid and uid for the windows created on the secondary display by the test. | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 102 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 103 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 104 |  | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 105 | // An arbitrary pid of the gesture monitor window | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 106 | static constexpr gui::Pid MONITOR_PID{2001}; | 
| Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 107 |  | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 108 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; | 
|  | 109 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 110 | static constexpr int expectedWallpaperFlags = | 
|  | 111 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; | 
|  | 112 |  | 
| Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 113 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; | 
|  | 114 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 115 | struct PointF { | 
|  | 116 | float x; | 
|  | 117 | float y; | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 118 | auto operator<=>(const PointF&) const = default; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 119 | }; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 |  | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 121 | /** | 
|  | 122 | * Return a DOWN key event with KEYCODE_A. | 
|  | 123 | */ | 
|  | 124 | static KeyEvent getTestKeyEvent() { | 
|  | 125 | KeyEvent event; | 
|  | 126 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 127 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 128 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, | 
|  | 129 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 130 | return event; | 
|  | 131 | } | 
|  | 132 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 133 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { | 
|  | 134 | ASSERT_EQ(expectedAction, receivedAction) | 
|  | 135 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " | 
|  | 136 | << MotionEvent::actionToString(receivedAction); | 
|  | 137 | } | 
|  | 138 |  | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 139 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { | 
|  | 140 | bool matches = action == arg.getAction(); | 
|  | 141 | if (!matches) { | 
|  | 142 | *result_listener << "expected action " << MotionEvent::actionToString(action) | 
|  | 143 | << ", but got " << MotionEvent::actionToString(arg.getAction()); | 
|  | 144 | } | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 145 | if (action == AMOTION_EVENT_ACTION_DOWN) { | 
|  | 146 | if (!matches) { | 
|  | 147 | *result_listener << "; "; | 
|  | 148 | } | 
|  | 149 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; | 
|  | 150 | matches &= arg.getDownTime() == arg.getEventTime(); | 
|  | 151 | } | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 152 | if (action == AMOTION_EVENT_ACTION_CANCEL) { | 
|  | 153 | if (!matches) { | 
|  | 154 | *result_listener << "; "; | 
|  | 155 | } | 
|  | 156 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; | 
|  | 157 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; | 
|  | 158 | } | 
|  | 159 | return matches; | 
|  | 160 | } | 
|  | 161 |  | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 162 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { | 
|  | 163 | return arg.getDownTime() == downTime; | 
|  | 164 | } | 
|  | 165 |  | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 166 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { | 
|  | 167 | return arg.getDisplayId() == displayId; | 
|  | 168 | } | 
|  | 169 |  | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 170 | MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") { | 
|  | 171 | return arg.getDeviceId() == deviceId; | 
|  | 172 | } | 
|  | 173 |  | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 174 | MATCHER_P(WithSource, source, "InputEvent with specified source") { | 
|  | 175 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " | 
|  | 176 | << inputEventSourceToString(arg.getSource()); | 
|  | 177 | return arg.getSource() == source; | 
|  | 178 | } | 
|  | 179 |  | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 180 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { | 
|  | 181 | return arg.getFlags() == flags; | 
|  | 182 | } | 
|  | 183 |  | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 184 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { | 
|  | 185 | if (arg.getPointerCount() != 1) { | 
|  | 186 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); | 
|  | 187 | return false; | 
|  | 188 | } | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 189 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 192 | MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") { | 
|  | 193 | return arg.getPointerCount() == pointerCount; | 
|  | 194 | } | 
|  | 195 |  | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 196 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { | 
|  | 197 | // Build a map for the received pointers, by pointer id | 
|  | 198 | std::map<int32_t /*pointerId*/, PointF> actualPointers; | 
|  | 199 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { | 
|  | 200 | const int32_t pointerId = arg.getPointerId(pointerIndex); | 
|  | 201 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; | 
|  | 202 | } | 
|  | 203 | return pointers == actualPointers; | 
|  | 204 | } | 
|  | 205 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | // --- FakeInputDispatcherPolicy --- | 
|  | 207 |  | 
|  | 208 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { | 
|  | 209 | InputDispatcherConfiguration mConfig; | 
|  | 210 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 211 | struct AnrResult { | 
|  | 212 | sp<IBinder> token{}; | 
|  | 213 | gui::Pid pid{-1}; | 
|  | 214 | }; | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 215 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 216 | public: | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 217 | FakeInputDispatcherPolicy() = default; | 
|  | 218 | virtual ~FakeInputDispatcherPolicy() = default; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 219 |  | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 220 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 221 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 222 | ASSERT_EQ(event.getType(), InputEventType::KEY); | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 223 | EXPECT_EQ(event.getDisplayId(), args.displayId); | 
|  | 224 |  | 
|  | 225 | const auto& keyEvent = static_cast<const KeyEvent&>(event); | 
|  | 226 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); | 
|  | 227 | EXPECT_EQ(keyEvent.getAction(), args.action); | 
|  | 228 | }); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 231 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { | 
|  | 232 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 233 | ASSERT_EQ(event.getType(), InputEventType::MOTION); | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 234 | EXPECT_EQ(event.getDisplayId(), args.displayId); | 
|  | 235 |  | 
|  | 236 | const auto& motionEvent = static_cast<const MotionEvent&>(event); | 
|  | 237 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); | 
|  | 238 | EXPECT_EQ(motionEvent.getAction(), args.action); | 
| Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 239 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); | 
|  | 240 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); | 
|  | 241 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); | 
|  | 242 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 243 | }); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 246 | void assertFilterInputEventWasNotCalled() { | 
|  | 247 | std::scoped_lock lock(mLock); | 
|  | 248 | ASSERT_EQ(nullptr, mFilteredEvent); | 
|  | 249 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 250 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 251 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 252 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 253 | ASSERT_TRUE(mConfigurationChangedTime) | 
|  | 254 | << "Timed out waiting for configuration changed call"; | 
|  | 255 | ASSERT_EQ(*mConfigurationChangedTime, when); | 
|  | 256 | mConfigurationChangedTime = std::nullopt; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 260 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 261 | ASSERT_TRUE(mLastNotifySwitch); | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 262 | // 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] | 263 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); | 
|  | 264 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); | 
|  | 265 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); | 
|  | 266 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); | 
|  | 267 | mLastNotifySwitch = std::nullopt; | 
|  | 268 | } | 
|  | 269 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 270 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { | 
| 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_EQ(touchedToken, mOnPointerDownToken); | 
|  | 273 | mOnPointerDownToken.clear(); | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | void assertOnPointerDownWasNotCalled() { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 277 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 278 | ASSERT_TRUE(mOnPointerDownToken == nullptr) | 
|  | 279 | << "Expected onPointerDownOutsideFocus to not have been called"; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 282 | // This function must be called soon after the expected ANR timer starts, | 
|  | 283 | // because we are also checking how much time has passed. | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 284 | void assertNotifyNoFocusedWindowAnrWasCalled( | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 285 | std::chrono::nanoseconds timeout, | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 286 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 287 | std::unique_lock lock(mLock); | 
|  | 288 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 289 | std::shared_ptr<InputApplicationHandle> application; | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 290 | ASSERT_NO_FATAL_FAILURE( | 
|  | 291 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 292 | ASSERT_EQ(expectedApplication, application); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 293 | } | 
|  | 294 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 295 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 296 | const sp<WindowInfoHandle>& window) { | 
|  | 297 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); | 
|  | 298 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), | 
|  | 299 | window->getInfo()->ownerPid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 300 | } | 
|  | 301 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 302 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, | 
|  | 303 | const sp<IBinder>& expectedToken, | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 304 | gui::Pid expectedPid) { | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 305 | std::unique_lock lock(mLock); | 
|  | 306 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 307 | AnrResult result; | 
|  | 308 | ASSERT_NO_FATAL_FAILURE(result = | 
|  | 309 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 310 | ASSERT_EQ(expectedToken, result.token); | 
|  | 311 | ASSERT_EQ(expectedPid, result.pid); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 312 | } | 
|  | 313 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 314 | /** 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] | 315 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 316 | std::unique_lock lock(mLock); | 
|  | 317 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 318 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); | 
|  | 319 | const auto& [token, _] = result; | 
|  | 320 | return token; | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 321 | } | 
|  | 322 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 323 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 324 | gui::Pid expectedPid) { | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 325 | std::unique_lock lock(mLock); | 
|  | 326 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 327 | AnrResult result; | 
|  | 328 | ASSERT_NO_FATAL_FAILURE( | 
|  | 329 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 330 | ASSERT_EQ(expectedToken, result.token); | 
|  | 331 | ASSERT_EQ(expectedPid, result.pid); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 332 | } | 
|  | 333 |  | 
|  | 334 | /** 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] | 335 | sp<IBinder> getResponsiveWindowToken() { | 
|  | 336 | std::unique_lock lock(mLock); | 
|  | 337 | android::base::ScopedLockAssertion assumeLocked(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 338 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); | 
|  | 339 | const auto& [token, _] = result; | 
|  | 340 | return token; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
|  | 343 | void assertNotifyAnrWasNotCalled() { | 
|  | 344 | std::scoped_lock lock(mLock); | 
|  | 345 | ASSERT_TRUE(mAnrApplications.empty()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 346 | ASSERT_TRUE(mAnrWindows.empty()); | 
|  | 347 | ASSERT_TRUE(mResponsiveWindows.empty()) | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 348 | << "ANR was not called, but please also consume the 'connection is responsive' " | 
|  | 349 | "signal"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 350 | } | 
|  | 351 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 352 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { | 
|  | 353 | mConfig.keyRepeatTimeout = timeout; | 
|  | 354 | mConfig.keyRepeatDelay = delay; | 
|  | 355 | } | 
|  | 356 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 357 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 358 | std::unique_lock lock(mLock); | 
|  | 359 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 360 |  | 
|  | 361 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, | 
|  | 362 | [this, enabled]() REQUIRES(mLock) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 363 | return mPointerCaptureRequest->enable == | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 364 | enabled; | 
|  | 365 | })) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 366 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled | 
|  | 367 | << ") to be called."; | 
|  | 368 | return {}; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 369 | } | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 370 | auto request = *mPointerCaptureRequest; | 
|  | 371 | mPointerCaptureRequest.reset(); | 
|  | 372 | return request; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 373 | } | 
|  | 374 |  | 
|  | 375 | void assertSetPointerCaptureNotCalled() { | 
|  | 376 | std::unique_lock lock(mLock); | 
|  | 377 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 378 |  | 
|  | 379 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 380 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 381 | "enabled = " | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 382 | << std::to_string(mPointerCaptureRequest->enable); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 383 | } | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 384 | mPointerCaptureRequest.reset(); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 385 | } | 
|  | 386 |  | 
| Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 387 | void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, | 
|  | 388 | const sp<IBinder>& targetToken) { | 
|  | 389 | dispatcher.waitForIdle(); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 390 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 391 | ASSERT_TRUE(mNotifyDropWindowWasCalled); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 392 | ASSERT_EQ(targetToken, mDropTargetWindowToken); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 393 | mNotifyDropWindowWasCalled = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 394 | } | 
|  | 395 |  | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 396 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { | 
|  | 397 | std::unique_lock lock(mLock); | 
|  | 398 | base::ScopedLockAssertion assumeLocked(mLock); | 
|  | 399 | std::optional<sp<IBinder>> receivedToken = | 
|  | 400 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, | 
|  | 401 | mNotifyInputChannelBroken); | 
|  | 402 | ASSERT_TRUE(receivedToken.has_value()); | 
|  | 403 | ASSERT_EQ(token, *receivedToken); | 
|  | 404 | } | 
|  | 405 |  | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 406 | /** | 
|  | 407 | * Set policy timeout. A value of zero means next key will not be intercepted. | 
|  | 408 | */ | 
|  | 409 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { | 
|  | 410 | mInterceptKeyTimeout = timeout; | 
|  | 411 | } | 
|  | 412 |  | 
| Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 413 | void assertUserActivityPoked() { | 
|  | 414 | std::scoped_lock lock(mLock); | 
|  | 415 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | void assertUserActivityNotPoked() { | 
|  | 419 | std::scoped_lock lock(mLock); | 
|  | 420 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; | 
|  | 421 | } | 
|  | 422 |  | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 423 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 424 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | void assertNotifyDeviceInteractionWasNotCalled() { | 
|  | 428 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); | 
|  | 429 | } | 
|  | 430 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 431 | private: | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 432 | std::mutex mLock; | 
|  | 433 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); | 
|  | 434 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); | 
|  | 435 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); | 
|  | 436 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 437 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 438 | std::condition_variable mPointerCaptureChangedCondition; | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 439 |  | 
|  | 440 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 441 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 442 | // ANR handling | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 443 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 444 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); | 
|  | 445 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 446 | std::condition_variable mNotifyAnr; | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 447 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); | 
|  | 448 | std::condition_variable mNotifyInputChannelBroken; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 449 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 450 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 451 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; | 
| Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 452 | bool mPokedUserActivity GUARDED_BY(mLock) = false; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 453 |  | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 454 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; | 
|  | 455 |  | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 456 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 457 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 458 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait | 
|  | 459 | // for a specific container to become non-empty. When the container is non-empty, return the | 
|  | 460 | // first entry from the container and erase it. | 
|  | 461 | template <class T> | 
|  | 462 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, | 
|  | 463 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { | 
|  | 464 | // If there is an ANR, Dispatcher won't be idle because there are still events | 
|  | 465 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle | 
|  | 466 | // before checking if ANR was called. | 
|  | 467 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need | 
|  | 468 | // to provide it some time to act. 100ms seems reasonable. | 
|  | 469 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack | 
|  | 470 | const std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 471 | std::optional<T> token = | 
|  | 472 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); | 
|  | 473 | if (!token.has_value()) { | 
|  | 474 | ADD_FAILURE() << "Did not receive the ANR callback"; | 
|  | 475 | return {}; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; | 
|  | 479 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because | 
|  | 480 | // the dispatcher started counting before this function was called | 
|  | 481 | if (std::chrono::abs(timeout - waited) > 100ms) { | 
|  | 482 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " | 
|  | 483 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() | 
|  | 484 | << "ms, but waited " | 
|  | 485 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() | 
|  | 486 | << "ms instead"; | 
|  | 487 | } | 
|  | 488 | return *token; | 
|  | 489 | } | 
|  | 490 |  | 
|  | 491 | template <class T> | 
|  | 492 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, | 
|  | 493 | std::queue<T>& storage, | 
|  | 494 | std::unique_lock<std::mutex>& lock, | 
|  | 495 | std::condition_variable& condition) | 
|  | 496 | REQUIRES(mLock) { | 
|  | 497 | condition.wait_for(lock, timeout, | 
|  | 498 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); | 
|  | 499 | if (storage.empty()) { | 
|  | 500 | ADD_FAILURE() << "Did not receive the expected callback"; | 
|  | 501 | return std::nullopt; | 
|  | 502 | } | 
|  | 503 | T item = storage.front(); | 
|  | 504 | storage.pop(); | 
|  | 505 | return std::make_optional(item); | 
|  | 506 | } | 
|  | 507 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 508 | void notifyConfigurationChanged(nsecs_t when) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 509 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 510 | mConfigurationChangedTime = when; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 511 | } | 
|  | 512 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 513 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 514 | const std::string&) override { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 515 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 516 | ASSERT_TRUE(pid.has_value()); | 
|  | 517 | mAnrWindows.push({connectionToken, *pid}); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 518 | mNotifyAnr.notify_all(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 519 | } | 
|  | 520 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 521 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 522 | std::optional<gui::Pid> pid) override { | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 523 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 524 | ASSERT_TRUE(pid.has_value()); | 
|  | 525 | mResponsiveWindows.push({connectionToken, *pid}); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 526 | mNotifyAnr.notify_all(); | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | void notifyNoFocusedWindowAnr( | 
|  | 530 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { | 
|  | 531 | std::scoped_lock lock(mLock); | 
|  | 532 | mAnrApplications.push(applicationHandle); | 
|  | 533 | mNotifyAnr.notify_all(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 534 | } | 
|  | 535 |  | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 536 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { | 
|  | 537 | std::scoped_lock lock(mLock); | 
|  | 538 | mBrokenInputChannels.push(connectionToken); | 
|  | 539 | mNotifyInputChannelBroken.notify_all(); | 
|  | 540 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 541 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 542 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} | 
| Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 543 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 544 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, | 
|  | 545 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, | 
|  | 546 | const std::vector<float>& values) override {} | 
|  | 547 |  | 
|  | 548 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, | 
|  | 549 | InputDeviceSensorAccuracy accuracy) override {} | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 550 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 551 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} | 
|  | 552 |  | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 553 | InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 554 |  | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 555 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 556 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 557 | switch (inputEvent.getType()) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 558 | case InputEventType::KEY: { | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 559 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); | 
|  | 560 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 561 | break; | 
|  | 562 | } | 
|  | 563 |  | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 564 | case InputEventType::MOTION: { | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 565 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); | 
|  | 566 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 567 | break; | 
|  | 568 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 569 | default: { | 
|  | 570 | ADD_FAILURE() << "Should only filter keys or motions"; | 
|  | 571 | break; | 
|  | 572 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 573 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | return true; | 
|  | 575 | } | 
|  | 576 |  | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 577 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { | 
|  | 578 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 579 | // Clear intercept state when we handled the event. | 
|  | 580 | mInterceptKeyTimeout = 0ms; | 
|  | 581 | } | 
|  | 582 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 583 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 584 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 585 |  | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 586 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 587 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); | 
|  | 588 | // Clear intercept state so we could dispatch the event in next wake. | 
|  | 589 | mInterceptKeyTimeout = 0ms; | 
|  | 590 | return delay; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 591 | } | 
|  | 592 |  | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 593 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&, | 
|  | 594 | uint32_t) override { | 
|  | 595 | return {}; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 | } | 
|  | 597 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 598 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, | 
|  | 599 | uint32_t policyFlags) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 600 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 601 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is | 
|  | 602 | * essentially a passthrough for notifySwitch. | 
|  | 603 | */ | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 604 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 605 | } | 
|  | 606 |  | 
| Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 607 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { | 
|  | 608 | std::scoped_lock lock(mLock); | 
|  | 609 | mPokedUserActivity = true; | 
|  | 610 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 611 |  | 
| Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 612 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 613 | std::scoped_lock lock(mLock); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 614 | mOnPointerDownToken = newToken; | 
|  | 615 | } | 
|  | 616 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 617 | void setPointerCapture(const PointerCaptureRequest& request) override { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 618 | std::scoped_lock lock(mLock); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 619 | mPointerCaptureRequest = {request}; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 620 | mPointerCaptureChangedCondition.notify_all(); | 
|  | 621 | } | 
|  | 622 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 623 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { | 
|  | 624 | std::scoped_lock lock(mLock); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 625 | mNotifyDropWindowWasCalled = true; | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 626 | mDropTargetWindowToken = token; | 
|  | 627 | } | 
|  | 628 |  | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 629 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 630 | const std::set<gui::Uid>& uids) override { | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 631 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); | 
|  | 632 | } | 
|  | 633 |  | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 634 | void assertFilterInputEventWasCalledInternal( | 
|  | 635 | const std::function<void(const InputEvent&)>& verify) { | 
| Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 636 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 637 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 638 | verify(*mFilteredEvent); | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 639 | mFilteredEvent = nullptr; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 640 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 641 | }; | 
|  | 642 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 643 | // --- InputDispatcherTest --- | 
|  | 644 |  | 
|  | 645 | class InputDispatcherTest : public testing::Test { | 
|  | 646 | protected: | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 647 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 648 | std::unique_ptr<InputDispatcher> mDispatcher; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 649 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 650 | void SetUp() override { | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 651 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); | 
|  | 652 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 653 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 654 | // Start InputDispatcher thread | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 655 | ASSERT_EQ(OK, mDispatcher->start()); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 656 | } | 
|  | 657 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 658 | void TearDown() override { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 659 | ASSERT_EQ(OK, mDispatcher->stop()); | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 660 | mFakePolicy.reset(); | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 661 | mDispatcher.reset(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 662 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 663 |  | 
|  | 664 | /** | 
|  | 665 | * Used for debugging when writing the test | 
|  | 666 | */ | 
|  | 667 | void dumpDispatcherState() { | 
|  | 668 | std::string dump; | 
|  | 669 | mDispatcher->dump(dump); | 
|  | 670 | std::stringstream ss(dump); | 
|  | 671 | std::string to; | 
|  | 672 |  | 
|  | 673 | while (std::getline(ss, to, '\n')) { | 
|  | 674 | ALOGE("%s", to.c_str()); | 
|  | 675 | } | 
|  | 676 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 677 |  | 
| Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 678 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 679 | FocusRequest request; | 
|  | 680 | request.token = window->getToken(); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 681 | request.windowName = window->getName(); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 682 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 683 | request.displayId = window->getInfo()->displayId; | 
|  | 684 | mDispatcher->setFocusedWindow(request); | 
|  | 685 | } | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | }; | 
|  | 687 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 688 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { | 
|  | 689 | KeyEvent event; | 
|  | 690 |  | 
|  | 691 | // Rejects undefined key actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 692 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 693 | INVALID_HMAC, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 694 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, | 
|  | 695 | ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 696 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 697 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 698 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 699 | << "Should reject key events with undefined action."; | 
|  | 700 |  | 
|  | 701 | // 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] | 702 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 703 | 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] | 704 | ARBITRARY_TIME, ARBITRARY_TIME); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 705 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 706 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 707 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 708 | << "Should reject key events with ACTION_MULTIPLE."; | 
|  | 709 | } | 
|  | 710 |  | 
|  | 711 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { | 
|  | 712 | MotionEvent event; | 
|  | 713 | PointerProperties pointerProperties[MAX_POINTERS + 1]; | 
|  | 714 | PointerCoords pointerCoords[MAX_POINTERS + 1]; | 
| Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 715 | for (size_t i = 0; i <= MAX_POINTERS; i++) { | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | pointerProperties[i].clear(); | 
|  | 717 | pointerProperties[i].id = i; | 
|  | 718 | pointerCoords[i].clear(); | 
|  | 719 | } | 
|  | 720 |  | 
| Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 721 | // Some constants commonly used below | 
|  | 722 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; | 
|  | 723 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; | 
|  | 724 | constexpr int32_t metaState = AMETA_NONE; | 
|  | 725 | constexpr MotionClassification classification = MotionClassification::NONE; | 
|  | 726 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 727 | ui::Transform identityTransform; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | // Rejects undefined motion actions. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 729 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 730 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, | 
|  | 731 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 732 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 733 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 734 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 735 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 736 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 737 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 738 | << "Should reject motion events with undefined action."; | 
|  | 739 |  | 
|  | 740 | // Rejects pointer down with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 741 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 742 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
|  | 743 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 744 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 745 | ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 746 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 747 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 748 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 749 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 750 | << "Should reject motion events with pointer down index too large."; | 
|  | 751 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 752 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 753 | AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 754 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 755 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 756 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 757 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 758 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 759 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 760 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 761 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 762 | << "Should reject motion events with pointer down index too small."; | 
|  | 763 |  | 
|  | 764 | // Rejects pointer up with invalid index. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 765 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 766 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, | 
|  | 767 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 768 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 769 | ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 770 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 771 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 772 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 773 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 774 | << "Should reject motion events with pointer up index too large."; | 
|  | 775 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 776 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 777 | AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 778 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 779 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, | 
|  | 780 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 781 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 782 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 783 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 784 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 785 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 786 | << "Should reject motion events with pointer up index too small."; | 
|  | 787 |  | 
|  | 788 | // Rejects motion events with invalid number of pointers. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 789 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 790 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 791 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 792 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 793 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 794 | /*pointerCount*/ 0, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 795 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 796 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 797 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | << "Should reject motion events with 0 pointers."; | 
|  | 799 |  | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 800 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 801 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 802 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 803 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 804 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 805 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 806 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 807 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 808 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 809 | << "Should reject motion events with more than MAX_POINTERS pointers."; | 
|  | 810 |  | 
|  | 811 | // Rejects motion events with invalid pointer ids. | 
|  | 812 | pointerProperties[0].id = -1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 813 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 814 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 815 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 816 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 817 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 818 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 819 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 820 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 821 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 822 | << "Should reject motion events with pointer ids less than 0."; | 
|  | 823 |  | 
|  | 824 | pointerProperties[0].id = MAX_POINTER_ID + 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 825 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 826 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 827 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 828 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 829 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 830 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 831 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 832 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 833 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 834 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; | 
|  | 835 |  | 
|  | 836 | // Rejects motion events with duplicate pointer ids. | 
|  | 837 | pointerProperties[0].id = 1; | 
|  | 838 | pointerProperties[1].id = 1; | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 839 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, | 
|  | 840 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 841 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 842 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, | 
|  | 843 | ARBITRARY_TIME, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 844 | /*pointerCount*/ 2, pointerProperties, pointerCoords); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 845 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 846 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 847 | 0ms, 0)) | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 848 | << "Should reject motion events with duplicate pointer ids."; | 
|  | 849 | } | 
|  | 850 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 851 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ | 
|  | 852 |  | 
|  | 853 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { | 
|  | 854 | constexpr nsecs_t eventTime = 20; | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 855 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 856 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 857 |  | 
|  | 858 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); | 
|  | 859 | } | 
|  | 860 |  | 
|  | 861 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 862 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, | 
|  | 863 | /*switchMask=*/2); | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 864 | mDispatcher->notifySwitch(args); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 865 |  | 
|  | 866 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener | 
|  | 867 | args.policyFlags |= POLICY_FLAG_TRUSTED; | 
|  | 868 | mFakePolicy->assertNotifySwitchWasCalled(args); | 
|  | 869 | } | 
|  | 870 |  | 
| Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 871 | namespace { | 
|  | 872 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 873 | // --- InputDispatcherTest SetInputWindowTest --- | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 874 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; | 
| Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 875 | // Default input dispatching timeout if there is no focused application or paused window | 
|  | 876 | // from which to determine an appropriate dispatching timeout. | 
|  | 877 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( | 
|  | 878 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * | 
|  | 879 | android::base::HwTimeoutMultiplier()); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 880 |  | 
|  | 881 | class FakeApplicationHandle : public InputApplicationHandle { | 
|  | 882 | public: | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 883 | FakeApplicationHandle() { | 
|  | 884 | mInfo.name = "Fake Application"; | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 885 | mInfo.token = sp<BBinder>::make(); | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 886 | mInfo.dispatchingTimeoutMillis = | 
|  | 887 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 888 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 889 | virtual ~FakeApplicationHandle() {} | 
|  | 890 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 891 | virtual bool updateInfo() override { return true; } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 892 |  | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 893 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { | 
|  | 894 | mInfo.dispatchingTimeoutMillis = timeout.count(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 895 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 896 | }; | 
|  | 897 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 898 | class FakeInputReceiver { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 899 | public: | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 900 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 901 | : mName(name) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 902 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 903 | } | 
|  | 904 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 905 | InputEvent* consume() { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 906 | InputEvent* event; | 
|  | 907 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); | 
|  | 908 | if (!consumeSeq) { | 
|  | 909 | return nullptr; | 
|  | 910 | } | 
|  | 911 | finishEvent(*consumeSeq); | 
|  | 912 | return event; | 
|  | 913 | } | 
|  | 914 |  | 
|  | 915 | /** | 
|  | 916 | * Receive an event without acknowledging it. | 
|  | 917 | * Return the sequence number that could later be used to send finished signal. | 
|  | 918 | */ | 
|  | 919 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 920 | uint32_t consumeSeq; | 
|  | 921 | InputEvent* event; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 922 |  | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 923 | std::chrono::time_point start = std::chrono::steady_clock::now(); | 
|  | 924 | status_t status = WOULD_BLOCK; | 
|  | 925 | while (status == WOULD_BLOCK) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 926 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 927 | &event); | 
|  | 928 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; | 
|  | 929 | if (elapsed > 100ms) { | 
|  | 930 | break; | 
|  | 931 | } | 
|  | 932 | } | 
|  | 933 |  | 
|  | 934 | if (status == WOULD_BLOCK) { | 
|  | 935 | // Just means there's no event available. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 936 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 937 | } | 
|  | 938 |  | 
|  | 939 | if (status != OK) { | 
|  | 940 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 941 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 942 | } | 
|  | 943 | if (event == nullptr) { | 
|  | 944 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 945 | return std::nullopt; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 946 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 947 | if (outEvent != nullptr) { | 
|  | 948 | *outEvent = event; | 
|  | 949 | } | 
|  | 950 | return consumeSeq; | 
|  | 951 | } | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 952 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 953 | /** | 
|  | 954 | * To be used together with "receiveEvent" to complete the consumption of an event. | 
|  | 955 | */ | 
|  | 956 | void finishEvent(uint32_t consumeSeq) { | 
|  | 957 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); | 
|  | 958 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 959 | } | 
|  | 960 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 961 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 962 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); | 
|  | 963 | ASSERT_EQ(OK, status); | 
|  | 964 | } | 
|  | 965 |  | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 966 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 967 | std::optional<int32_t> expectedDisplayId, | 
|  | 968 | std::optional<int32_t> expectedFlags) { | 
| Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 969 | InputEvent* event = consume(); | 
|  | 970 |  | 
|  | 971 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 972 | << ": consumer should have returned non-NULL event."; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 973 | ASSERT_EQ(expectedEventType, event->getType()) | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 974 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) | 
|  | 975 | << " event, got " << *event; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 976 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 977 | if (expectedDisplayId.has_value()) { | 
|  | 978 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); | 
|  | 979 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 980 |  | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 981 | switch (expectedEventType) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 982 | case InputEventType::KEY: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 983 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); | 
|  | 984 | EXPECT_EQ(expectedAction, keyEvent.getAction()); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 985 | if (expectedFlags.has_value()) { | 
|  | 986 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); | 
|  | 987 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 988 | break; | 
|  | 989 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 990 | case InputEventType::MOTION: { | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 991 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 992 | assertMotionAction(expectedAction, motionEvent.getAction()); | 
|  | 993 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 994 | if (expectedFlags.has_value()) { | 
|  | 995 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); | 
|  | 996 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 997 | break; | 
|  | 998 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 999 | case InputEventType::FOCUS: { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1000 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; | 
|  | 1001 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1002 | case InputEventType::CAPTURE: { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1003 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; | 
|  | 1004 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1005 | case InputEventType::TOUCH_MODE: { | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1006 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; | 
|  | 1007 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1008 | case InputEventType::DRAG: { | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1009 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; | 
|  | 1010 | } | 
| Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1011 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1012 | } | 
|  | 1013 |  | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1014 | MotionEvent* consumeMotion() { | 
|  | 1015 | InputEvent* event = consume(); | 
|  | 1016 |  | 
|  | 1017 | if (event == nullptr) { | 
|  | 1018 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; | 
|  | 1019 | return nullptr; | 
|  | 1020 | } | 
|  | 1021 |  | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1022 | if (event->getType() != InputEventType::MOTION) { | 
|  | 1023 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1024 | return nullptr; | 
|  | 1025 | } | 
|  | 1026 | return static_cast<MotionEvent*>(event); | 
|  | 1027 | } | 
|  | 1028 |  | 
|  | 1029 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { | 
|  | 1030 | MotionEvent* motionEvent = consumeMotion(); | 
|  | 1031 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; | 
|  | 1032 | ASSERT_THAT(*motionEvent, matcher); | 
|  | 1033 | } | 
|  | 1034 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1035 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { | 
|  | 1036 | InputEvent* event = consume(); | 
|  | 1037 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 1038 | << ": consumer should have returned non-NULL event."; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1039 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) | 
|  | 1040 | << "Instead of FocusEvent, got " << *event; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1041 |  | 
|  | 1042 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 1043 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 1044 |  | 
|  | 1045 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); | 
|  | 1046 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1047 | } | 
|  | 1048 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1049 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 1050 | const InputEvent* event = consume(); | 
|  | 1051 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 1052 | << ": consumer should have returned non-NULL event."; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1053 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) | 
|  | 1054 | << "Instead of CaptureEvent, got " << *event; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1055 |  | 
|  | 1056 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 1057 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 1058 |  | 
|  | 1059 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); | 
|  | 1060 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); | 
|  | 1061 | } | 
|  | 1062 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1063 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 1064 | const InputEvent* event = consume(); | 
|  | 1065 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 1066 | << ": consumer should have returned non-NULL event."; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1067 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1068 |  | 
|  | 1069 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 1070 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 1071 |  | 
|  | 1072 | const auto& dragEvent = static_cast<const DragEvent&>(*event); | 
|  | 1073 | EXPECT_EQ(isExiting, dragEvent.isExiting()); | 
|  | 1074 | EXPECT_EQ(x, dragEvent.getX()); | 
|  | 1075 | EXPECT_EQ(y, dragEvent.getY()); | 
|  | 1076 | } | 
|  | 1077 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1078 | void consumeTouchModeEvent(bool inTouchMode) { | 
|  | 1079 | const InputEvent* event = consume(); | 
|  | 1080 | ASSERT_NE(nullptr, event) << mName.c_str() | 
|  | 1081 | << ": consumer should have returned non-NULL event."; | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1082 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) | 
|  | 1083 | << "Instead of TouchModeEvent, got " << *event; | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1084 |  | 
|  | 1085 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) | 
|  | 1086 | << mName.c_str() << ": event displayId should always be NONE."; | 
|  | 1087 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); | 
|  | 1088 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); | 
|  | 1089 | } | 
|  | 1090 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1091 | void assertNoEvents() { | 
|  | 1092 | InputEvent* event = consume(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1093 | if (event == nullptr) { | 
|  | 1094 | return; | 
|  | 1095 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1096 | if (event->getType() == InputEventType::KEY) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1097 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); | 
|  | 1098 | ADD_FAILURE() << "Received key event " | 
|  | 1099 | << KeyEvent::actionToString(keyEvent.getAction()); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1100 | } else if (event->getType() == InputEventType::MOTION) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1101 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 1102 | ADD_FAILURE() << "Received motion event " | 
|  | 1103 | << MotionEvent::actionToString(motionEvent.getAction()); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1104 | } else if (event->getType() == InputEventType::FOCUS) { | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1105 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); | 
|  | 1106 | ADD_FAILURE() << "Received focus event, hasFocus = " | 
|  | 1107 | << (focusEvent.getHasFocus() ? "true" : "false"); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1108 | } else if (event->getType() == InputEventType::CAPTURE) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1109 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); | 
|  | 1110 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " | 
|  | 1111 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1112 | } else if (event->getType() == InputEventType::TOUCH_MODE) { | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1113 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); | 
|  | 1114 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " | 
|  | 1115 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1116 | } | 
|  | 1117 | FAIL() << mName.c_str() | 
|  | 1118 | << ": should not have received any events, so consume() should return NULL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1119 | } | 
|  | 1120 |  | 
|  | 1121 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } | 
|  | 1122 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1123 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } | 
|  | 1124 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1125 | protected: | 
|  | 1126 | std::unique_ptr<InputConsumer> mConsumer; | 
|  | 1127 | PreallocatedInputEventFactory mEventFactory; | 
|  | 1128 |  | 
|  | 1129 | std::string mName; | 
|  | 1130 | }; | 
|  | 1131 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1132 | class FakeWindowHandle : public WindowInfoHandle { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1133 | public: | 
|  | 1134 | static const int32_t WIDTH = 600; | 
|  | 1135 | static const int32_t HEIGHT = 800; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1136 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1137 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1138 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1139 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1140 | : mName(name) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1141 | if (token == std::nullopt) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1142 | base::Result<std::unique_ptr<InputChannel>> channel = | 
|  | 1143 | dispatcher->createInputChannel(name); | 
|  | 1144 | token = (*channel)->getConnectionToken(); | 
|  | 1145 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1146 | } | 
|  | 1147 |  | 
|  | 1148 | inputApplicationHandle->updateInfo(); | 
|  | 1149 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
|  | 1150 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1151 | mInfo.token = *token; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1152 | mInfo.id = sId++; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1153 | mInfo.name = name; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1154 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1155 | mInfo.alpha = 1.0; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1156 | mInfo.frameLeft = 0; | 
|  | 1157 | mInfo.frameTop = 0; | 
|  | 1158 | mInfo.frameRight = WIDTH; | 
|  | 1159 | mInfo.frameBottom = HEIGHT; | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1160 | mInfo.transform.set(0, 0); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1161 | mInfo.globalScaleFactor = 1.0; | 
|  | 1162 | mInfo.touchableRegion.clear(); | 
|  | 1163 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1164 | mInfo.ownerPid = WINDOW_PID; | 
|  | 1165 | mInfo.ownerUid = WINDOW_UID; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1166 | mInfo.displayId = displayId; | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1167 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1168 | } | 
|  | 1169 |  | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1170 | sp<FakeWindowHandle> clone(int32_t displayId) { | 
|  | 1171 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); | 
|  | 1172 | handle->mInfo = mInfo; | 
|  | 1173 | handle->mInfo.displayId = displayId; | 
|  | 1174 | handle->mInfo.id = sId++; | 
|  | 1175 | handle->mInputReceiver = mInputReceiver; | 
|  | 1176 | return handle; | 
|  | 1177 | } | 
|  | 1178 |  | 
|  | 1179 | /** | 
|  | 1180 | * This is different from clone, because clone will make a "mirror" window - a window with the | 
|  | 1181 | * same token, but a different ID. The original window and the clone window are allowed to be | 
|  | 1182 | * sent to the dispatcher at the same time - they can coexist inside the dispatcher. | 
|  | 1183 | * This function will create a different object of WindowInfoHandle, but with the same | 
|  | 1184 | * properties as the original object - including the ID. | 
|  | 1185 | * You can use either the old or the new object to consume the events. | 
|  | 1186 | * IMPORTANT: The duplicated object is supposed to replace the original object, and not appear | 
|  | 1187 | * at the same time inside dispatcher. | 
|  | 1188 | */ | 
|  | 1189 | sp<FakeWindowHandle> duplicate() { | 
|  | 1190 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mName); | 
|  | 1191 | handle->mInfo = mInfo; | 
|  | 1192 | handle->mInputReceiver = mInputReceiver; | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1193 | return handle; | 
|  | 1194 | } | 
|  | 1195 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1196 | void setTouchable(bool touchable) { | 
|  | 1197 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); | 
|  | 1198 | } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1199 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1200 | void setFocusable(bool focusable) { | 
|  | 1201 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); | 
|  | 1202 | } | 
|  | 1203 |  | 
|  | 1204 | void setVisible(bool visible) { | 
|  | 1205 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); | 
|  | 1206 | } | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1207 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1208 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1209 | mInfo.dispatchingTimeout = timeout; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1210 | } | 
|  | 1211 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1212 | void setPaused(bool paused) { | 
|  | 1213 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); | 
|  | 1214 | } | 
|  | 1215 |  | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1216 | void setPreventSplitting(bool preventSplitting) { | 
|  | 1217 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1218 | } | 
|  | 1219 |  | 
|  | 1220 | void setSlippery(bool slippery) { | 
|  | 1221 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); | 
|  | 1222 | } | 
|  | 1223 |  | 
|  | 1224 | void setWatchOutsideTouch(bool watchOutside) { | 
|  | 1225 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); | 
|  | 1226 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1227 |  | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1228 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } | 
|  | 1229 |  | 
|  | 1230 | void setInterceptsStylus(bool interceptsStylus) { | 
|  | 1231 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); | 
|  | 1232 | } | 
|  | 1233 |  | 
|  | 1234 | void setDropInput(bool dropInput) { | 
|  | 1235 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); | 
|  | 1236 | } | 
|  | 1237 |  | 
|  | 1238 | void setDropInputIfObscured(bool dropInputIfObscured) { | 
|  | 1239 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); | 
|  | 1240 | } | 
|  | 1241 |  | 
|  | 1242 | void setNoInputChannel(bool noInputChannel) { | 
|  | 1243 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); | 
|  | 1244 | } | 
|  | 1245 |  | 
| Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1246 | void setDisableUserActivity(bool disableUserActivity) { | 
|  | 1247 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); | 
|  | 1248 | } | 
|  | 1249 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1250 | void setAlpha(float alpha) { mInfo.alpha = alpha; } | 
|  | 1251 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1252 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1253 |  | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1254 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } | 
|  | 1255 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1256 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1257 | mInfo.frameLeft = frame.left; | 
|  | 1258 | mInfo.frameTop = frame.top; | 
|  | 1259 | mInfo.frameRight = frame.right; | 
|  | 1260 | mInfo.frameBottom = frame.bottom; | 
|  | 1261 | mInfo.touchableRegion.clear(); | 
|  | 1262 | mInfo.addTouchableRegion(frame); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1263 |  | 
|  | 1264 | const Rect logicalDisplayFrame = displayTransform.transform(frame); | 
|  | 1265 | ui::Transform translate; | 
|  | 1266 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); | 
|  | 1267 | mInfo.transform = translate * displayTransform; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1268 | } | 
|  | 1269 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1270 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } | 
|  | 1271 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1272 | void setIsWallpaper(bool isWallpaper) { | 
|  | 1273 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); | 
|  | 1274 | } | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1275 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1276 | void setDupTouchToWallpaper(bool hasWallpaper) { | 
|  | 1277 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); | 
|  | 1278 | } | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1279 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1280 | void setTrustedOverlay(bool trustedOverlay) { | 
|  | 1281 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); | 
|  | 1282 | } | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1283 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1284 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { | 
|  | 1285 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); | 
|  | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1289 |  | 
| yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1290 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } | 
|  | 1291 |  | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1292 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1293 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1294 | } | 
|  | 1295 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1296 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1297 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1298 | } | 
|  | 1299 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1300 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1301 | int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1302 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), | 
|  | 1303 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1304 | } | 
|  | 1305 |  | 
|  | 1306 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1307 | int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1308 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), | 
|  | 1309 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1310 | } | 
|  | 1311 |  | 
|  | 1312 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1313 | int32_t expectedFlags = 0) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1314 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); | 
|  | 1315 | } | 
|  | 1316 |  | 
|  | 1317 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, | 
|  | 1318 | std::optional<int32_t> expectedFlags = std::nullopt) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1319 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1320 | expectedFlags); | 
|  | 1321 | } | 
|  | 1322 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1323 | void consumeMotionPointerDown(int32_t pointerIdx, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1324 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1325 | int32_t expectedFlags = 0) { | 
|  | 1326 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 1327 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1328 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1329 | } | 
|  | 1330 |  | 
|  | 1331 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1332 | int32_t expectedFlags = 0) { | 
|  | 1333 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | | 
|  | 1334 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1335 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1336 | } | 
|  | 1337 |  | 
|  | 1338 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1339 | int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1340 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1341 | expectedFlags); | 
|  | 1342 | } | 
|  | 1343 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1344 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1345 | int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1346 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1347 | expectedFlags); | 
|  | 1348 | } | 
|  | 1349 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1350 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, | 
|  | 1351 | int32_t expectedFlags = 0) { | 
|  | 1352 | InputEvent* event = consume(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1353 | ASSERT_NE(nullptr, event); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1354 | ASSERT_EQ(InputEventType::MOTION, event->getType()); | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1355 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 1356 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); | 
|  | 1357 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); | 
|  | 1358 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); | 
|  | 1359 | } | 
|  | 1360 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1361 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { | 
|  | 1362 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1363 | << "Cannot consume events from a window with no receiver"; | 
|  | 1364 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); | 
|  | 1365 | } | 
|  | 1366 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1367 | void consumeCaptureEvent(bool hasCapture) { | 
|  | 1368 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1369 | << "Cannot consume events from a window with no receiver"; | 
|  | 1370 | mInputReceiver->consumeCaptureEvent(hasCapture); | 
|  | 1371 | } | 
|  | 1372 |  | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1373 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { | 
|  | 1374 | MotionEvent* motionEvent = consumeMotion(); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1375 | 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] | 1376 | ASSERT_THAT(*motionEvent, matcher); | 
|  | 1377 | } | 
|  | 1378 |  | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1379 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1380 | std::optional<int32_t> expectedDisplayId, | 
|  | 1381 | std::optional<int32_t> expectedFlags) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1382 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; | 
|  | 1383 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, | 
|  | 1384 | expectedFlags); | 
|  | 1385 | } | 
|  | 1386 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1387 | void consumeDragEvent(bool isExiting, float x, float y) { | 
|  | 1388 | mInputReceiver->consumeDragEvent(isExiting, x, y); | 
|  | 1389 | } | 
|  | 1390 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1391 | void consumeTouchModeEvent(bool inTouchMode) { | 
|  | 1392 | ASSERT_NE(mInputReceiver, nullptr) | 
|  | 1393 | << "Cannot consume events from a window with no receiver"; | 
|  | 1394 | mInputReceiver->consumeTouchModeEvent(inTouchMode); | 
|  | 1395 | } | 
|  | 1396 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1397 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1398 | if (mInputReceiver == nullptr) { | 
|  | 1399 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; | 
|  | 1400 | return std::nullopt; | 
|  | 1401 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1402 | return mInputReceiver->receiveEvent(outEvent); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1403 | } | 
|  | 1404 |  | 
|  | 1405 | void finishEvent(uint32_t sequenceNum) { | 
|  | 1406 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1407 | mInputReceiver->finishEvent(sequenceNum); | 
|  | 1408 | } | 
|  | 1409 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1410 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { | 
|  | 1411 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; | 
|  | 1412 | mInputReceiver->sendTimeline(inputEventId, timeline); | 
|  | 1413 | } | 
|  | 1414 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1415 | InputEvent* consume() { | 
|  | 1416 | if (mInputReceiver == nullptr) { | 
|  | 1417 | return nullptr; | 
|  | 1418 | } | 
|  | 1419 | return mInputReceiver->consume(); | 
|  | 1420 | } | 
|  | 1421 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1422 | MotionEvent* consumeMotion() { | 
|  | 1423 | InputEvent* event = consume(); | 
|  | 1424 | if (event == nullptr) { | 
|  | 1425 | ADD_FAILURE() << "Consume failed : no event"; | 
|  | 1426 | return nullptr; | 
|  | 1427 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1428 | if (event->getType() != InputEventType::MOTION) { | 
|  | 1429 | ADD_FAILURE() << "Instead of motion event, got " << *event; | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1430 | return nullptr; | 
|  | 1431 | } | 
|  | 1432 | return static_cast<MotionEvent*>(event); | 
|  | 1433 | } | 
|  | 1434 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1435 | void assertNoEvents() { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1436 | if (mInputReceiver == nullptr && | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1437 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1438 | return; // Can't receive events if the window does not have input channel | 
|  | 1439 | } | 
|  | 1440 | ASSERT_NE(nullptr, mInputReceiver) | 
|  | 1441 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1442 | mInputReceiver->assertNoEvents(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1443 | } | 
|  | 1444 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1445 | sp<IBinder> getToken() { return mInfo.token; } | 
|  | 1446 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1447 | const std::string& getName() { return mName; } | 
|  | 1448 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1449 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1450 | mInfo.ownerPid = ownerPid; | 
|  | 1451 | mInfo.ownerUid = ownerUid; | 
|  | 1452 | } | 
|  | 1453 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1454 | gui::Pid getPid() const { return mInfo.ownerPid; } | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1455 |  | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1456 | void destroyReceiver() { mInputReceiver = nullptr; } | 
|  | 1457 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1458 | int getChannelFd() { return mInputReceiver->getChannelFd(); } | 
|  | 1459 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1460 | private: | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1461 | FakeWindowHandle(std::string name) : mName(name){}; | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1462 | const std::string mName; | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1463 | std::shared_ptr<FakeInputReceiver> mInputReceiver; | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1464 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1465 | friend class sp<FakeWindowHandle>; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1466 | }; | 
|  | 1467 |  | 
| Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1468 | std::atomic<int32_t> FakeWindowHandle::sId{1}; | 
|  | 1469 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1470 | static InputEventInjectionResult injectKey( | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1471 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1472 | int32_t displayId = ADISPLAY_ID_NONE, | 
|  | 1473 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1474 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1475 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1476 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1477 | KeyEvent event; | 
|  | 1478 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1479 |  | 
|  | 1480 | // Define a valid key down event. | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1481 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1482 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, | 
|  | 1483 | currentTime, currentTime); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1484 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1485 | if (!allowKeyRepeat) { | 
|  | 1486 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 1487 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1488 | // Inject event until dispatch out. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1489 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1490 | } | 
|  | 1491 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1492 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1493 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1494 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1495 | } | 
|  | 1496 |  | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1497 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without | 
|  | 1498 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it | 
|  | 1499 | // has to be woken up to process the repeating key. | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1500 | static InputEventInjectionResult injectKeyDownNoRepeat( | 
|  | 1501 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1502 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1503 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1504 | /*allowKeyRepeat=*/false); | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1505 | } | 
|  | 1506 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1507 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1508 | int32_t displayId = ADISPLAY_ID_NONE) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1509 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1510 | } | 
|  | 1511 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1512 | static InputEventInjectionResult injectMotionEvent( | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1513 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1514 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1515 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1516 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1517 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, | 
|  | 1518 | policyFlags); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1519 | } | 
|  | 1520 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1521 | static InputEventInjectionResult injectMotionEvent( | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1522 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1523 | int32_t displayId, const PointF& position = {100, 200}, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1524 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1525 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
|  | 1526 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1527 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1528 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1529 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { | 
| Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1530 | MotionEventBuilder motionBuilder = | 
|  | 1531 | MotionEventBuilder(action, source) | 
|  | 1532 | .displayId(displayId) | 
|  | 1533 | .eventTime(eventTime) | 
|  | 1534 | .rawXCursorPosition(cursorPosition.x) | 
|  | 1535 | .rawYCursorPosition(cursorPosition.y) | 
|  | 1536 | .pointer( | 
|  | 1537 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); | 
|  | 1538 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { | 
|  | 1539 | motionBuilder.downTime(eventTime); | 
|  | 1540 | } | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1541 |  | 
|  | 1542 | // Inject event until dispatch out. | 
| Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1543 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, | 
|  | 1544 | targetUid, policyFlags); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1545 | } | 
|  | 1546 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1547 | static InputEventInjectionResult injectMotionDown( | 
|  | 1548 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, | 
|  | 1549 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1550 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1551 | } | 
|  | 1552 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1553 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1554 | int32_t source, int32_t displayId, | 
|  | 1555 | const PointF& location = {100, 200}) { | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1556 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1557 | } | 
|  | 1558 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1559 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1560 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1561 | // Define a valid key event. | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1562 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, | 
| Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1563 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, | 
|  | 1564 | KEY_A, AMETA_NONE, currentTime); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1565 |  | 
|  | 1566 | return args; | 
|  | 1567 | } | 
|  | 1568 |  | 
| Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1569 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, | 
|  | 1570 | int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1571 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1572 | // Define a valid key event. | 
|  | 1573 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, | 
|  | 1574 | displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON, | 
|  | 1575 | currentTime); | 
|  | 1576 |  | 
|  | 1577 | return args; | 
|  | 1578 | } | 
|  | 1579 |  | 
|  | 1580 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, | 
|  | 1581 | int32_t displayId = ADISPLAY_ID_NONE) { | 
|  | 1582 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1583 | // Define a valid key event. | 
|  | 1584 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, | 
|  | 1585 | displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT, | 
|  | 1586 | AMETA_NONE, currentTime); | 
|  | 1587 |  | 
|  | 1588 | return args; | 
|  | 1589 | } | 
|  | 1590 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1591 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, | 
|  | 1592 | int32_t displayId, | 
|  | 1593 | const std::vector<PointF>& points) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1594 | size_t pointerCount = points.size(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1595 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { | 
|  | 1596 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; | 
|  | 1597 | } | 
|  | 1598 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1599 | PointerProperties pointerProperties[pointerCount]; | 
|  | 1600 | PointerCoords pointerCoords[pointerCount]; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1601 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1602 | for (size_t i = 0; i < pointerCount; i++) { | 
|  | 1603 | pointerProperties[i].clear(); | 
|  | 1604 | pointerProperties[i].id = i; | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1605 | pointerProperties[i].toolType = ToolType::FINGER; | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1606 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1607 | pointerCoords[i].clear(); | 
|  | 1608 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); | 
|  | 1609 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); | 
|  | 1610 | } | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1611 |  | 
|  | 1612 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1613 | // Define a valid motion event. | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1614 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1615 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, | 
|  | 1616 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1617 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, | 
|  | 1618 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1619 | AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 1620 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1621 |  | 
|  | 1622 | return args; | 
|  | 1623 | } | 
|  | 1624 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1625 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { | 
|  | 1626 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); | 
|  | 1627 | } | 
|  | 1628 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1629 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { | 
|  | 1630 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); | 
|  | 1631 | } | 
|  | 1632 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1633 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( | 
|  | 1634 | const PointerCaptureRequest& request) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1635 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1636 | } | 
|  | 1637 |  | 
| Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1638 | } // namespace | 
|  | 1639 |  | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1640 | /** | 
|  | 1641 | * When a window unexpectedly disposes of its input channel, policy should be notified about the | 
|  | 1642 | * broken channel. | 
|  | 1643 | */ | 
|  | 1644 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { | 
|  | 1645 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1646 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1647 | sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1648 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1649 |  | 
|  | 1650 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1651 |  | 
|  | 1652 | // Window closes its channel, but the window remains. | 
|  | 1653 | window->destroyReceiver(); | 
|  | 1654 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); | 
|  | 1655 | } | 
|  | 1656 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1657 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1658 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1659 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1660 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1661 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1662 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1664 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1665 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1666 |  | 
|  | 1667 | // Window should receive motion event. | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1668 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1669 | } | 
|  | 1670 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1671 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { | 
|  | 1672 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1673 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1674 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1675 |  | 
|  | 1676 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1677 | // Inject a MotionEvent to an unknown display. | 
|  | 1678 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1679 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) | 
|  | 1680 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1681 |  | 
|  | 1682 | // Window should receive motion event. | 
|  | 1683 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1684 | } | 
|  | 1685 |  | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1686 | /** | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1687 | * Calling setInputWindows once should not cause any issues. | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1688 | * This test serves as a sanity check for the next test, where setInputWindows is | 
|  | 1689 | * called twice. | 
|  | 1690 | */ | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1691 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1692 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1693 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1694 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1695 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1696 |  | 
|  | 1697 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1698 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1699 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1700 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1701 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1702 |  | 
|  | 1703 | // Window should receive motion event. | 
|  | 1704 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1705 | } | 
|  | 1706 |  | 
|  | 1707 | /** | 
|  | 1708 | * Calling setInputWindows twice, with the same info, should not cause any issues. | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1709 | */ | 
|  | 1710 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1711 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1712 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 1713 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1714 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1715 |  | 
|  | 1716 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 1717 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1718 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1719 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1720 | {50, 50})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1721 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1722 |  | 
|  | 1723 | // Window should receive motion event. | 
|  | 1724 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 1725 | } | 
|  | 1726 |  | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1727 | // The foreground window should receive the first touch down event. | 
|  | 1728 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1729 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1730 | sp<FakeWindowHandle> windowTop = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1731 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1732 | sp<FakeWindowHandle> windowSecond = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1733 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1734 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1735 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1736 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1737 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 1738 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1739 |  | 
|  | 1740 | // 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] | 1741 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1742 | windowSecond->assertNoEvents(); | 
|  | 1743 | } | 
|  | 1744 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1745 | /** | 
|  | 1746 | * Two windows: A top window, and a wallpaper behind the window. | 
|  | 1747 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window | 
|  | 1748 | * gets ACTION_CANCEL. | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1749 | * 1. foregroundWindow <-- dup touch to wallpaper | 
|  | 1750 | * 2. wallpaperWindow <-- is wallpaper | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1751 | */ | 
|  | 1752 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { | 
|  | 1753 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1754 | sp<FakeWindowHandle> foregroundWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1755 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1756 | foregroundWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1757 | sp<FakeWindowHandle> wallpaperWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1758 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1759 | wallpaperWindow->setIsWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1760 |  | 
|  | 1761 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); | 
|  | 1762 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1763 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1764 | {100, 200})) | 
|  | 1765 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1766 |  | 
|  | 1767 | // Both foreground window and its wallpaper should receive the touch down | 
|  | 1768 | foregroundWindow->consumeMotionDown(); | 
|  | 1769 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1770 |  | 
|  | 1771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1772 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1773 | ADISPLAY_ID_DEFAULT, {110, 200})) | 
|  | 1774 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1775 |  | 
|  | 1776 | foregroundWindow->consumeMotionMove(); | 
|  | 1777 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1778 |  | 
|  | 1779 | // Now the foreground window goes away, but the wallpaper stays | 
|  | 1780 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); | 
|  | 1781 | foregroundWindow->consumeMotionCancel(); | 
|  | 1782 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. | 
|  | 1783 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1784 | } | 
|  | 1785 |  | 
|  | 1786 | /** | 
| Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1787 | * Two fingers down on the window, and lift off the first finger. | 
|  | 1788 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event | 
|  | 1789 | * contains a single pointer. | 
|  | 1790 | */ | 
|  | 1791 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { | 
|  | 1792 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1793 | sp<FakeWindowHandle> window = | 
|  | 1794 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 1795 |  | 
|  | 1796 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1797 | // First touch pointer down on right window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1798 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 1799 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 1800 | .build()); | 
| Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1801 | // Second touch pointer down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1802 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 1803 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 1804 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) | 
|  | 1805 | .build()); | 
| Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1806 | // First touch pointer lifts. The second one remains down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1807 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 1808 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 1809 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) | 
|  | 1810 | .build()); | 
| Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1811 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 1812 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); | 
|  | 1813 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); | 
|  | 1814 |  | 
|  | 1815 | // Remove the window. The gesture should be canceled | 
|  | 1816 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 1817 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; | 
|  | 1818 | window->consumeMotionEvent( | 
|  | 1819 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); | 
|  | 1820 | } | 
|  | 1821 |  | 
|  | 1822 | /** | 
| Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1823 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, | 
|  | 1824 | * with the following differences: | 
|  | 1825 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to | 
|  | 1826 | * clean up the connection. | 
|  | 1827 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. | 
|  | 1828 | * Ensure that there's no crash in the dispatcher. | 
|  | 1829 | */ | 
|  | 1830 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { | 
|  | 1831 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1832 | sp<FakeWindowHandle> foregroundWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1833 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1834 | foregroundWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1835 | sp<FakeWindowHandle> wallpaperWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1836 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1837 | wallpaperWindow->setIsWallpaper(true); | 
| Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1838 |  | 
|  | 1839 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); | 
|  | 1840 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1841 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1842 | {100, 200})) | 
|  | 1843 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1844 |  | 
|  | 1845 | // Both foreground window and its wallpaper should receive the touch down | 
|  | 1846 | foregroundWindow->consumeMotionDown(); | 
|  | 1847 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1848 |  | 
|  | 1849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1850 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 1851 | ADISPLAY_ID_DEFAULT, {110, 200})) | 
|  | 1852 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1853 |  | 
|  | 1854 | foregroundWindow->consumeMotionMove(); | 
|  | 1855 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1856 |  | 
|  | 1857 | // Wallpaper closes its channel, but the window remains. | 
|  | 1858 | wallpaperWindow->destroyReceiver(); | 
|  | 1859 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); | 
|  | 1860 |  | 
|  | 1861 | // Now the foreground window goes away, but the wallpaper stays, even though its channel | 
|  | 1862 | // is no longer valid. | 
|  | 1863 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); | 
|  | 1864 | foregroundWindow->consumeMotionCancel(); | 
|  | 1865 | } | 
|  | 1866 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1867 | class ShouldSplitTouchFixture : public InputDispatcherTest, | 
|  | 1868 | public ::testing::WithParamInterface<bool> {}; | 
|  | 1869 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, | 
|  | 1870 | ::testing::Values(true, false)); | 
| Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1871 | /** | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1872 | * A single window that receives touch (on top), and a wallpaper window underneath it. | 
|  | 1873 | * The top window gets a multitouch gesture. | 
|  | 1874 | * Ensure that wallpaper gets the same gesture. | 
|  | 1875 | */ | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1876 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1877 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1878 | sp<FakeWindowHandle> foregroundWindow = | 
|  | 1879 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); | 
|  | 1880 | foregroundWindow->setDupTouchToWallpaper(true); | 
|  | 1881 | foregroundWindow->setPreventSplitting(GetParam()); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1882 |  | 
|  | 1883 | sp<FakeWindowHandle> wallpaperWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1884 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1885 | wallpaperWindow->setIsWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1886 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1887 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1888 |  | 
|  | 1889 | // Touch down on top window | 
|  | 1890 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1891 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1892 | {100, 100})) | 
|  | 1893 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1894 |  | 
|  | 1895 | // Both top window and its wallpaper should receive the touch down | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1896 | foregroundWindow->consumeMotionDown(); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1897 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1898 |  | 
|  | 1899 | // Second finger down on the top window | 
|  | 1900 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1901 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1902 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1903 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) | 
|  | 1904 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1905 | .build(); | 
|  | 1906 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1907 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 1908 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 1909 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1910 |  | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1911 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); | 
|  | 1912 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1913 | expectedWallpaperFlags); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1914 |  | 
|  | 1915 | const MotionEvent secondFingerUpEvent = | 
|  | 1916 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 1917 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 1918 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1919 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) | 
|  | 1920 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1921 | .build(); | 
|  | 1922 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1923 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, | 
|  | 1924 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 1925 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1926 | foregroundWindow->consumeMotionPointerUp(0); | 
|  | 1927 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1928 |  | 
|  | 1929 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1930 | injectMotionEvent(mDispatcher, | 
|  | 1931 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, | 
|  | 1932 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 1933 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 1934 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
|  | 1935 | .pointer(PointerBuilder(/* id */ 1, | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1936 | ToolType::FINGER) | 
| Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1937 | .x(100) | 
|  | 1938 | .y(100)) | 
|  | 1939 | .build(), | 
|  | 1940 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1941 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1942 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 1943 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1944 | } | 
|  | 1945 |  | 
|  | 1946 | /** | 
|  | 1947 | * Two windows: a window on the left and window on the right. | 
|  | 1948 | * A third window, wallpaper, is behind both windows, and spans both top windows. | 
|  | 1949 | * The first touch down goes to the left window. A second pointer touches down on the right window. | 
|  | 1950 | * The touch is split, so both left and right windows should receive ACTION_DOWN. | 
|  | 1951 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by | 
|  | 1952 | * ACTION_POINTER_DOWN(1). | 
|  | 1953 | */ | 
|  | 1954 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { | 
|  | 1955 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 1956 | sp<FakeWindowHandle> leftWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1957 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1958 | leftWindow->setFrame(Rect(0, 0, 200, 200)); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1959 | leftWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1960 |  | 
|  | 1961 | sp<FakeWindowHandle> rightWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1962 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1963 | rightWindow->setFrame(Rect(200, 0, 400, 200)); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1964 | rightWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1965 |  | 
|  | 1966 | sp<FakeWindowHandle> wallpaperWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1967 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1968 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1969 | wallpaperWindow->setIsWallpaper(true); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1970 |  | 
|  | 1971 | mDispatcher->setInputWindows( | 
|  | 1972 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); | 
|  | 1973 |  | 
|  | 1974 | // Touch down on left window | 
|  | 1975 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1976 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 1977 | {100, 100})) | 
|  | 1978 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1979 |  | 
|  | 1980 | // Both foreground window and its wallpaper should receive the touch down | 
|  | 1981 | leftWindow->consumeMotionDown(); | 
|  | 1982 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 1983 |  | 
|  | 1984 | // Second finger down on the right window | 
|  | 1985 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1986 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1987 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1988 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) | 
|  | 1989 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1990 | .build(); | 
|  | 1991 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 1992 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 1993 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 1994 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 1995 |  | 
|  | 1996 | leftWindow->consumeMotionMove(); | 
|  | 1997 | // Since the touch is split, right window gets ACTION_DOWN | 
|  | 1998 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1999 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2000 | expectedWallpaperFlags); | 
|  | 2001 |  | 
|  | 2002 | // Now, leftWindow, which received the first finger, disappears. | 
|  | 2003 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); | 
|  | 2004 | leftWindow->consumeMotionCancel(); | 
|  | 2005 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. | 
|  | 2006 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 2007 |  | 
|  | 2008 | // The pointer that's still down on the right window moves, and goes to the right window only. | 
|  | 2009 | // As far as the dispatcher's concerned though, both pointers are still present. | 
|  | 2010 | const MotionEvent secondFingerMoveEvent = | 
|  | 2011 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2012 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2013 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) | 
|  | 2014 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2015 | .build(); | 
|  | 2016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2017 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, | 
|  | 2018 | InputEventInjectionSync::WAIT_FOR_RESULT)); | 
|  | 2019 | rightWindow->consumeMotionMove(); | 
|  | 2020 |  | 
|  | 2021 | leftWindow->assertNoEvents(); | 
|  | 2022 | rightWindow->assertNoEvents(); | 
|  | 2023 | wallpaperWindow->assertNoEvents(); | 
|  | 2024 | } | 
|  | 2025 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2026 | /** | 
|  | 2027 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. | 
|  | 2028 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL | 
|  | 2029 | * The right window should receive ACTION_DOWN. | 
|  | 2030 | */ | 
|  | 2031 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2032 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2033 | sp<FakeWindowHandle> leftWindow = | 
|  | 2034 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 2035 | leftWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2036 | leftWindow->setDupTouchToWallpaper(true); | 
|  | 2037 | leftWindow->setSlippery(true); | 
|  | 2038 |  | 
|  | 2039 | sp<FakeWindowHandle> rightWindow = | 
|  | 2040 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 2041 | rightWindow->setFrame(Rect(200, 0, 400, 200)); | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2042 |  | 
|  | 2043 | sp<FakeWindowHandle> wallpaperWindow = | 
|  | 2044 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
|  | 2045 | wallpaperWindow->setIsWallpaper(true); | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2046 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2047 | mDispatcher->setInputWindows( | 
|  | 2048 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2049 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2050 | // Touch down on left window | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2051 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2052 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2053 | {100, 100})) | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2054 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2055 |  | 
|  | 2056 | // Both foreground window and its wallpaper should receive the touch down | 
|  | 2057 | leftWindow->consumeMotionDown(); | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2058 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 2059 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2060 | // Move to right window, the left window should receive cancel. | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2061 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2062 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 2063 | ADISPLAY_ID_DEFAULT, {201, 100})) | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2064 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 2065 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2066 | leftWindow->consumeMotionCancel(); | 
|  | 2067 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 2068 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
| Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2069 | } | 
|  | 2070 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2071 | /** | 
| Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2072 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not | 
|  | 2073 | * interactive, it might stop sending this flag. | 
|  | 2074 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure | 
|  | 2075 | * to have a consistent input stream. | 
|  | 2076 | * | 
|  | 2077 | * Test procedure: | 
|  | 2078 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. | 
|  | 2079 | * DOWN (new gesture). | 
|  | 2080 | * | 
|  | 2081 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent | 
|  | 2082 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. | 
|  | 2083 | * | 
|  | 2084 | * We technically just need a single window here, but we are using two windows (spy on top and a | 
|  | 2085 | * regular window below) to emulate the actual situation where it happens on the device. | 
|  | 2086 | */ | 
|  | 2087 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { | 
|  | 2088 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2089 | sp<FakeWindowHandle> spyWindow = | 
|  | 2090 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
|  | 2091 | spyWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2092 | spyWindow->setTrustedOverlay(true); | 
|  | 2093 | spyWindow->setSpy(true); | 
|  | 2094 |  | 
|  | 2095 | sp<FakeWindowHandle> window = | 
|  | 2096 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2097 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2098 |  | 
|  | 2099 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); | 
|  | 2100 | const int32_t touchDeviceId = 4; | 
| Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2101 |  | 
|  | 2102 | // Two pointers down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2103 | mDispatcher->notifyMotion( | 
|  | 2104 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2105 | .deviceId(touchDeviceId) | 
|  | 2106 | .policyFlags(DEFAULT_POLICY_FLAGS) | 
|  | 2107 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 2108 | .build()); | 
| Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2109 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2110 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2111 | .deviceId(touchDeviceId) | 
|  | 2112 | .policyFlags(DEFAULT_POLICY_FLAGS) | 
|  | 2113 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 2114 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) | 
|  | 2115 | .build()); | 
| Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2116 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2117 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); | 
|  | 2118 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2119 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); | 
|  | 2120 |  | 
|  | 2121 | // Cancel the current gesture. Send the cancel without the default policy flags. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2122 | mDispatcher->notifyMotion( | 
|  | 2123 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2124 | .deviceId(touchDeviceId) | 
|  | 2125 | .policyFlags(0) | 
|  | 2126 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 2127 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) | 
|  | 2128 | .build()); | 
| Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2129 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); | 
|  | 2130 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); | 
|  | 2131 |  | 
|  | 2132 | // We don't need to reset the device to reproduce the issue, but the reset event typically | 
|  | 2133 | // follows, so we keep it here to model the actual listener behaviour more closely. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2134 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); | 
| Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2135 |  | 
|  | 2136 | // Start new gesture | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2137 | mDispatcher->notifyMotion( | 
|  | 2138 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2139 | .deviceId(touchDeviceId) | 
|  | 2140 | .policyFlags(DEFAULT_POLICY_FLAGS) | 
|  | 2141 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 2142 | .build()); | 
| Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2143 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2144 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2145 |  | 
|  | 2146 | // No more events | 
|  | 2147 | spyWindow->assertNoEvents(); | 
|  | 2148 | window->assertNoEvents(); | 
|  | 2149 | } | 
|  | 2150 |  | 
|  | 2151 | /** | 
| Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2152 | * Two windows: a window on the left and a window on the right. | 
|  | 2153 | * Mouse is hovered from the right window into the left window. | 
|  | 2154 | * Next, we tap on the left window, where the cursor was last seen. | 
|  | 2155 | * The second tap is done onto the right window. | 
|  | 2156 | * The mouse and tap are from two different devices. | 
|  | 2157 | * We technically don't need to set the downtime / eventtime for these events, but setting these | 
|  | 2158 | * explicitly helps during debugging. | 
|  | 2159 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. | 
|  | 2160 | * In the buggy implementation, a tap on the right window would cause a crash. | 
|  | 2161 | */ | 
|  | 2162 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { | 
|  | 2163 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2164 | sp<FakeWindowHandle> leftWindow = | 
|  | 2165 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 2166 | leftWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2167 |  | 
|  | 2168 | sp<FakeWindowHandle> rightWindow = | 
|  | 2169 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 2170 | rightWindow->setFrame(Rect(200, 0, 400, 200)); | 
|  | 2171 |  | 
|  | 2172 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); | 
|  | 2173 | // All times need to start at the current time, otherwise the dispatcher will drop the events as | 
|  | 2174 | // stale. | 
|  | 2175 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 2176 | const int32_t mouseDeviceId = 6; | 
|  | 2177 | const int32_t touchDeviceId = 4; | 
|  | 2178 | // Move the cursor from right | 
|  | 2179 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2180 | injectMotionEvent(mDispatcher, | 
|  | 2181 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 2182 | AINPUT_SOURCE_MOUSE) | 
|  | 2183 | .deviceId(mouseDeviceId) | 
|  | 2184 | .downTime(baseTime + 10) | 
|  | 2185 | .eventTime(baseTime + 20) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2186 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2187 | .x(300) | 
|  | 2188 | .y(100)) | 
|  | 2189 | .build())); | 
|  | 2190 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
|  | 2191 |  | 
|  | 2192 | // .. to the left window | 
|  | 2193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2194 | injectMotionEvent(mDispatcher, | 
|  | 2195 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 2196 | AINPUT_SOURCE_MOUSE) | 
|  | 2197 | .deviceId(mouseDeviceId) | 
|  | 2198 | .downTime(baseTime + 10) | 
|  | 2199 | .eventTime(baseTime + 30) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2200 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2201 | .x(110) | 
|  | 2202 | .y(100)) | 
|  | 2203 | .build())); | 
|  | 2204 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); | 
|  | 2205 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
|  | 2206 | // Now tap the left window | 
|  | 2207 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2208 | injectMotionEvent(mDispatcher, | 
|  | 2209 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 2210 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2211 | .deviceId(touchDeviceId) | 
|  | 2212 | .downTime(baseTime + 40) | 
|  | 2213 | .eventTime(baseTime + 40) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2214 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2215 | .x(100) | 
|  | 2216 | .y(100)) | 
|  | 2217 | .build())); | 
|  | 2218 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); | 
|  | 2219 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2220 |  | 
|  | 2221 | // release tap | 
|  | 2222 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2223 | injectMotionEvent(mDispatcher, | 
|  | 2224 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, | 
|  | 2225 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2226 | .deviceId(touchDeviceId) | 
|  | 2227 | .downTime(baseTime + 40) | 
|  | 2228 | .eventTime(baseTime + 50) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2229 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2230 | .x(100) | 
|  | 2231 | .y(100)) | 
|  | 2232 | .build())); | 
|  | 2233 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); | 
|  | 2234 |  | 
|  | 2235 | // Tap the window on the right | 
|  | 2236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2237 | injectMotionEvent(mDispatcher, | 
|  | 2238 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 2239 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2240 | .deviceId(touchDeviceId) | 
|  | 2241 | .downTime(baseTime + 60) | 
|  | 2242 | .eventTime(baseTime + 60) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2243 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2244 | .x(300) | 
|  | 2245 | .y(100)) | 
|  | 2246 | .build())); | 
|  | 2247 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2248 |  | 
|  | 2249 | // release tap | 
|  | 2250 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2251 | injectMotionEvent(mDispatcher, | 
|  | 2252 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, | 
|  | 2253 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2254 | .deviceId(touchDeviceId) | 
|  | 2255 | .downTime(baseTime + 60) | 
|  | 2256 | .eventTime(baseTime + 70) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2257 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2258 | .x(300) | 
|  | 2259 | .y(100)) | 
|  | 2260 | .build())); | 
|  | 2261 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); | 
|  | 2262 |  | 
|  | 2263 | // No more events | 
|  | 2264 | leftWindow->assertNoEvents(); | 
|  | 2265 | rightWindow->assertNoEvents(); | 
|  | 2266 | } | 
|  | 2267 |  | 
|  | 2268 | /** | 
| Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2269 | * Start hovering in a window. While this hover is still active, make another window appear on top. | 
|  | 2270 | * The top, obstructing window has no input channel, so it's not supposed to receive input. | 
|  | 2271 | * While the top window is present, the hovering is stopped. | 
|  | 2272 | * Later, hovering gets resumed again. | 
|  | 2273 | * Ensure that new hover gesture is handled correctly. | 
|  | 2274 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly | 
|  | 2275 | * to the window that's currently being hovered over. | 
|  | 2276 | */ | 
|  | 2277 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { | 
|  | 2278 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2279 | sp<FakeWindowHandle> window = | 
|  | 2280 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2281 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2282 |  | 
|  | 2283 | // Only a single window is present at first | 
|  | 2284 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2285 |  | 
|  | 2286 | // Start hovering in the window | 
|  | 2287 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) | 
|  | 2288 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) | 
|  | 2289 | .build()); | 
|  | 2290 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); | 
|  | 2291 |  | 
|  | 2292 | // Now, an obscuring window appears! | 
|  | 2293 | sp<FakeWindowHandle> obscuringWindow = | 
|  | 2294 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", | 
|  | 2295 | ADISPLAY_ID_DEFAULT, | 
|  | 2296 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); | 
|  | 2297 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2298 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 2299 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); | 
|  | 2300 | obscuringWindow->setNoInputChannel(true); | 
|  | 2301 | obscuringWindow->setFocusable(false); | 
|  | 2302 | obscuringWindow->setAlpha(1.0); | 
|  | 2303 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 2304 |  | 
|  | 2305 | // While this new obscuring window is present, the hovering is stopped | 
|  | 2306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) | 
|  | 2307 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) | 
|  | 2308 | .build()); | 
|  | 2309 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 2310 |  | 
|  | 2311 | // Now the obscuring window goes away. | 
|  | 2312 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2313 |  | 
|  | 2314 | // And a new hover gesture starts. | 
|  | 2315 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) | 
|  | 2316 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) | 
|  | 2317 | .build()); | 
|  | 2318 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); | 
|  | 2319 | } | 
|  | 2320 |  | 
|  | 2321 | /** | 
|  | 2322 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to | 
|  | 2323 | * the obscuring window. | 
|  | 2324 | */ | 
|  | 2325 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { | 
|  | 2326 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2327 | sp<FakeWindowHandle> window = | 
|  | 2328 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2329 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2330 |  | 
|  | 2331 | // Only a single window is present at first | 
|  | 2332 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2333 |  | 
|  | 2334 | // Start hovering in the window | 
|  | 2335 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) | 
|  | 2336 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) | 
|  | 2337 | .build()); | 
|  | 2338 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); | 
|  | 2339 |  | 
|  | 2340 | // Now, an obscuring window appears! | 
|  | 2341 | sp<FakeWindowHandle> obscuringWindow = | 
|  | 2342 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", | 
|  | 2343 | ADISPLAY_ID_DEFAULT, | 
|  | 2344 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); | 
|  | 2345 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2346 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 2347 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); | 
|  | 2348 | obscuringWindow->setNoInputChannel(true); | 
|  | 2349 | obscuringWindow->setFocusable(false); | 
|  | 2350 | obscuringWindow->setAlpha(1.0); | 
|  | 2351 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 2352 |  | 
|  | 2353 | // While this new obscuring window is present, the hovering continues. The event can't go to the | 
|  | 2354 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. | 
|  | 2355 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 2356 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) | 
|  | 2357 | .build()); | 
|  | 2358 | obscuringWindow->assertNoEvents(); | 
|  | 2359 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 2360 |  | 
|  | 2361 | // Now the obscuring window goes away. | 
|  | 2362 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2363 |  | 
|  | 2364 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, | 
|  | 2365 | // so it should generate a HOVER_ENTER | 
|  | 2366 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 2367 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) | 
|  | 2368 | .build()); | 
|  | 2369 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); | 
|  | 2370 |  | 
|  | 2371 | // Now the MOVE should be getting dispatched normally | 
|  | 2372 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 2373 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) | 
|  | 2374 | .build()); | 
|  | 2375 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); | 
|  | 2376 | } | 
|  | 2377 |  | 
|  | 2378 | /** | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2379 | * Two windows: a window on the left and a window on the right. | 
|  | 2380 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains | 
|  | 2381 | * down. Then, on the left window, also place second touch pointer down. | 
|  | 2382 | * This test tries to reproduce a crash. | 
|  | 2383 | * In the buggy implementation, second pointer down on the left window would cause a crash. | 
|  | 2384 | */ | 
|  | 2385 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { | 
|  | 2386 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2387 | sp<FakeWindowHandle> leftWindow = | 
|  | 2388 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 2389 | leftWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2390 |  | 
|  | 2391 | sp<FakeWindowHandle> rightWindow = | 
|  | 2392 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 2393 | rightWindow->setFrame(Rect(200, 0, 400, 200)); | 
|  | 2394 |  | 
|  | 2395 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); | 
|  | 2396 |  | 
|  | 2397 | const int32_t touchDeviceId = 4; | 
|  | 2398 | const int32_t mouseDeviceId = 6; | 
|  | 2399 | NotifyMotionArgs args; | 
|  | 2400 |  | 
|  | 2401 | // Start hovering over the left window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2402 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) | 
|  | 2403 | .deviceId(mouseDeviceId) | 
|  | 2404 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) | 
|  | 2405 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2406 | leftWindow->consumeMotionEvent( | 
|  | 2407 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); | 
|  | 2408 |  | 
|  | 2409 | // Mouse down on left window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2410 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 2411 | .deviceId(mouseDeviceId) | 
|  | 2412 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2413 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) | 
|  | 2414 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2415 |  | 
|  | 2416 | leftWindow->consumeMotionEvent( | 
|  | 2417 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); | 
|  | 2418 | leftWindow->consumeMotionEvent( | 
|  | 2419 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); | 
|  | 2420 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2421 | mDispatcher->notifyMotion( | 
|  | 2422 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) | 
|  | 2423 | .deviceId(mouseDeviceId) | 
|  | 2424 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2425 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2426 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) | 
|  | 2427 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2428 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
|  | 2429 |  | 
|  | 2430 | // First touch pointer down on right window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2431 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2432 | .deviceId(touchDeviceId) | 
|  | 2433 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) | 
|  | 2434 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2435 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); | 
|  | 2436 |  | 
|  | 2437 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 2438 |  | 
|  | 2439 | // Second touch pointer down on left window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2440 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2441 | .deviceId(touchDeviceId) | 
|  | 2442 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) | 
|  | 2443 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) | 
|  | 2444 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2445 | leftWindow->consumeMotionEvent( | 
|  | 2446 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); | 
|  | 2447 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the | 
|  | 2448 | // current implementation. | 
|  | 2449 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; | 
|  | 2450 | rightWindow->consumeMotionEvent( | 
|  | 2451 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); | 
|  | 2452 |  | 
|  | 2453 | leftWindow->assertNoEvents(); | 
|  | 2454 | rightWindow->assertNoEvents(); | 
|  | 2455 | } | 
|  | 2456 |  | 
|  | 2457 | /** | 
|  | 2458 | * On a single window, use two different devices: mouse and touch. | 
|  | 2459 | * Touch happens first, with two pointers going down, and then the first pointer leaving. | 
|  | 2460 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. | 
|  | 2461 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, | 
|  | 2462 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not | 
|  | 2463 | * represent a new gesture. | 
|  | 2464 | */ | 
|  | 2465 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { | 
|  | 2466 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2467 | sp<FakeWindowHandle> window = | 
|  | 2468 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2469 | window->setFrame(Rect(0, 0, 400, 400)); | 
|  | 2470 |  | 
|  | 2471 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2472 |  | 
|  | 2473 | const int32_t touchDeviceId = 4; | 
|  | 2474 | const int32_t mouseDeviceId = 6; | 
|  | 2475 | NotifyMotionArgs args; | 
|  | 2476 |  | 
| Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2477 | // First touch pointer down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2478 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2479 | .deviceId(touchDeviceId) | 
|  | 2480 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) | 
|  | 2481 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2482 | // Second touch pointer down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2483 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2484 | .deviceId(touchDeviceId) | 
|  | 2485 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) | 
|  | 2486 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) | 
|  | 2487 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2488 | // First touch pointer lifts. The second one remains down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2489 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2490 | .deviceId(touchDeviceId) | 
|  | 2491 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) | 
|  | 2492 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) | 
|  | 2493 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2494 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 2495 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); | 
|  | 2496 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); | 
|  | 2497 |  | 
|  | 2498 | // Mouse down. The touch should be canceled | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2499 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 2500 | .deviceId(mouseDeviceId) | 
|  | 2501 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2502 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) | 
|  | 2503 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2504 |  | 
|  | 2505 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), | 
| Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2506 | WithPointerCount(1u))); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2507 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); | 
|  | 2508 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2509 | mDispatcher->notifyMotion( | 
|  | 2510 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) | 
|  | 2511 | .deviceId(mouseDeviceId) | 
|  | 2512 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2513 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2514 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) | 
|  | 2515 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2516 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
|  | 2517 |  | 
|  | 2518 | // Second touch pointer down. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2519 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2520 | .deviceId(touchDeviceId) | 
|  | 2521 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) | 
|  | 2522 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) | 
|  | 2523 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2524 | // The pointer_down event should be ignored | 
|  | 2525 | window->assertNoEvents(); | 
|  | 2526 | } | 
|  | 2527 |  | 
|  | 2528 | /** | 
| Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2529 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels | 
|  | 2530 | * the injected event. | 
|  | 2531 | */ | 
|  | 2532 | TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) { | 
|  | 2533 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2534 | sp<FakeWindowHandle> window = | 
|  | 2535 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2536 | window->setFrame(Rect(0, 0, 400, 400)); | 
|  | 2537 |  | 
|  | 2538 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2539 |  | 
|  | 2540 | const int32_t touchDeviceId = 4; | 
|  | 2541 | NotifyMotionArgs args; | 
|  | 2542 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after | 
|  | 2543 | // completion. | 
|  | 2544 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2545 | injectMotionEvent(mDispatcher, | 
|  | 2546 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 2547 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2548 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2549 | .x(50) | 
|  | 2550 | .y(50)) | 
|  | 2551 | .build())); | 
|  | 2552 | window->consumeMotionEvent( | 
|  | 2553 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); | 
|  | 2554 |  | 
|  | 2555 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer | 
|  | 2556 | // should be canceled and the new gesture should take over. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2557 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2558 | .deviceId(touchDeviceId) | 
|  | 2559 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) | 
|  | 2560 | .build()); | 
| Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2561 |  | 
|  | 2562 | window->consumeMotionEvent( | 
|  | 2563 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); | 
|  | 2564 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); | 
|  | 2565 | } | 
|  | 2566 |  | 
|  | 2567 | /** | 
| Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2568 | * This test is similar to the test above, but the sequence of injected events is different. | 
|  | 2569 | * | 
|  | 2570 | * Two windows: a window on the left and a window on the right. | 
|  | 2571 | * Mouse is hovered over the left window. | 
|  | 2572 | * Next, we tap on the left window, where the cursor was last seen. | 
|  | 2573 | * | 
|  | 2574 | * After that, we inject one finger down onto the right window, and then a second finger down onto | 
|  | 2575 | * the left window. | 
|  | 2576 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right | 
|  | 2577 | * window (first), and then another on the left window (second). | 
|  | 2578 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. | 
|  | 2579 | * In the buggy implementation, second finger down on the left window would cause a crash. | 
|  | 2580 | */ | 
|  | 2581 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { | 
|  | 2582 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2583 | sp<FakeWindowHandle> leftWindow = | 
|  | 2584 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 2585 | leftWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2586 |  | 
|  | 2587 | sp<FakeWindowHandle> rightWindow = | 
|  | 2588 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 2589 | rightWindow->setFrame(Rect(200, 0, 400, 200)); | 
|  | 2590 |  | 
|  | 2591 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); | 
|  | 2592 |  | 
|  | 2593 | const int32_t mouseDeviceId = 6; | 
|  | 2594 | const int32_t touchDeviceId = 4; | 
|  | 2595 | // Hover over the left window. Keep the cursor there. | 
|  | 2596 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2597 | injectMotionEvent(mDispatcher, | 
|  | 2598 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 2599 | AINPUT_SOURCE_MOUSE) | 
|  | 2600 | .deviceId(mouseDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2601 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2602 | .x(50) | 
|  | 2603 | .y(50)) | 
|  | 2604 | .build())); | 
|  | 2605 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
|  | 2606 |  | 
|  | 2607 | // Tap on left window | 
|  | 2608 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2609 | injectMotionEvent(mDispatcher, | 
|  | 2610 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 2611 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2612 | .deviceId(touchDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2613 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2614 | .x(100) | 
|  | 2615 | .y(100)) | 
|  | 2616 | .build())); | 
|  | 2617 |  | 
|  | 2618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2619 | injectMotionEvent(mDispatcher, | 
|  | 2620 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, | 
|  | 2621 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2622 | .deviceId(touchDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2623 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2624 | .x(100) | 
|  | 2625 | .y(100)) | 
|  | 2626 | .build())); | 
|  | 2627 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); | 
|  | 2628 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2629 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); | 
|  | 2630 |  | 
|  | 2631 | // First finger down on right window | 
|  | 2632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2633 | injectMotionEvent(mDispatcher, | 
|  | 2634 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 2635 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2636 | .deviceId(touchDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2637 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2638 | .x(300) | 
|  | 2639 | .y(100)) | 
|  | 2640 | .build())); | 
|  | 2641 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2642 |  | 
|  | 2643 | // Second finger down on the left window | 
|  | 2644 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2645 | injectMotionEvent(mDispatcher, | 
|  | 2646 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2647 | .deviceId(touchDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2648 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2649 | .x(300) | 
|  | 2650 | .y(100)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2651 | .pointer(PointerBuilder(1, ToolType::FINGER) | 
| Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2652 | .x(100) | 
|  | 2653 | .y(100)) | 
|  | 2654 | .build())); | 
|  | 2655 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2656 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); | 
|  | 2657 |  | 
|  | 2658 | // No more events | 
|  | 2659 | leftWindow->assertNoEvents(); | 
|  | 2660 | rightWindow->assertNoEvents(); | 
|  | 2661 | } | 
|  | 2662 |  | 
|  | 2663 | /** | 
| Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2664 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. | 
|  | 2665 | * While the touch is down, new hover events from the stylus device should be ignored. After the | 
|  | 2666 | * touch is gone, stylus hovering should start working again. | 
|  | 2667 | */ | 
|  | 2668 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { | 
|  | 2669 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2670 | sp<FakeWindowHandle> window = | 
|  | 2671 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2672 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2673 |  | 
|  | 2674 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 2675 |  | 
|  | 2676 | const int32_t stylusDeviceId = 5; | 
|  | 2677 | const int32_t touchDeviceId = 4; | 
|  | 2678 | // Start hovering with stylus | 
|  | 2679 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2680 | injectMotionEvent(mDispatcher, | 
|  | 2681 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 2682 | AINPUT_SOURCE_STYLUS) | 
|  | 2683 | .deviceId(stylusDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2684 | .pointer(PointerBuilder(0, ToolType::STYLUS) | 
| Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2685 | .x(50) | 
|  | 2686 | .y(50)) | 
|  | 2687 | .build())); | 
|  | 2688 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
|  | 2689 |  | 
|  | 2690 | // Finger down on the window | 
|  | 2691 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2692 | injectMotionEvent(mDispatcher, | 
|  | 2693 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 2694 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2695 | .deviceId(touchDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2696 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2697 | .x(100) | 
|  | 2698 | .y(100)) | 
|  | 2699 | .build())); | 
|  | 2700 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); | 
|  | 2701 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 2702 |  | 
|  | 2703 | // Try to continue hovering with stylus. Since we are already down, injection should fail | 
|  | 2704 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 2705 | injectMotionEvent(mDispatcher, | 
|  | 2706 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 2707 | AINPUT_SOURCE_STYLUS) | 
|  | 2708 | .deviceId(stylusDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2709 | .pointer(PointerBuilder(0, ToolType::STYLUS) | 
| Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2710 | .x(50) | 
|  | 2711 | .y(50)) | 
|  | 2712 | .build())); | 
|  | 2713 | // No event should be sent. This event should be ignored because a pointer from another device | 
|  | 2714 | // is already down. | 
|  | 2715 |  | 
|  | 2716 | // Lift up the finger | 
|  | 2717 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2718 | injectMotionEvent(mDispatcher, | 
|  | 2719 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, | 
|  | 2720 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2721 | .deviceId(touchDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2722 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2723 | .x(100) | 
|  | 2724 | .y(100)) | 
|  | 2725 | .build())); | 
|  | 2726 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); | 
|  | 2727 |  | 
|  | 2728 | // Now that the touch is gone, stylus hovering should start working again | 
|  | 2729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 2730 | injectMotionEvent(mDispatcher, | 
|  | 2731 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 2732 | AINPUT_SOURCE_STYLUS) | 
|  | 2733 | .deviceId(stylusDeviceId) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2734 | .pointer(PointerBuilder(0, ToolType::STYLUS) | 
| Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2735 | .x(50) | 
|  | 2736 | .y(50)) | 
|  | 2737 | .build())); | 
|  | 2738 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
|  | 2739 | // No more events | 
|  | 2740 | window->assertNoEvents(); | 
|  | 2741 | } | 
|  | 2742 |  | 
|  | 2743 | /** | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2744 | * A spy window above a window with no input channel. | 
|  | 2745 | * Start hovering with a stylus device, and then tap with it. | 
|  | 2746 | * Ensure spy window receives the entire sequence. | 
|  | 2747 | */ | 
|  | 2748 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { | 
|  | 2749 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2750 | sp<FakeWindowHandle> spyWindow = | 
|  | 2751 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
|  | 2752 | spyWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2753 | spyWindow->setTrustedOverlay(true); | 
|  | 2754 | spyWindow->setSpy(true); | 
|  | 2755 | sp<FakeWindowHandle> window = | 
|  | 2756 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2757 | window->setNoInputChannel(true); | 
|  | 2758 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2759 |  | 
|  | 2760 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); | 
|  | 2761 |  | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2762 | // Start hovering with stylus | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2763 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) | 
|  | 2764 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) | 
|  | 2765 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2766 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); | 
|  | 2767 | // Stop hovering | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2768 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) | 
|  | 2769 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) | 
|  | 2770 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2771 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 2772 |  | 
|  | 2773 | // Stylus touches down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2774 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) | 
|  | 2775 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) | 
|  | 2776 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2777 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 2778 |  | 
|  | 2779 | // Stylus goes up | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2780 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) | 
|  | 2781 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) | 
|  | 2782 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2783 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); | 
|  | 2784 |  | 
|  | 2785 | // Again hover | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2786 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) | 
|  | 2787 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) | 
|  | 2788 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2789 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); | 
|  | 2790 | // Stop hovering | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2791 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) | 
|  | 2792 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) | 
|  | 2793 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2794 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 2795 |  | 
|  | 2796 | // No more events | 
|  | 2797 | spyWindow->assertNoEvents(); | 
|  | 2798 | window->assertNoEvents(); | 
|  | 2799 | } | 
|  | 2800 |  | 
|  | 2801 | /** | 
|  | 2802 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. | 
|  | 2803 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse | 
|  | 2804 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. | 
|  | 2805 | * While the mouse is down, new move events from the touch device should be ignored. | 
|  | 2806 | */ | 
|  | 2807 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { | 
|  | 2808 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2809 | sp<FakeWindowHandle> spyWindow = | 
|  | 2810 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
|  | 2811 | spyWindow->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2812 | spyWindow->setTrustedOverlay(true); | 
|  | 2813 | spyWindow->setSpy(true); | 
|  | 2814 | sp<FakeWindowHandle> window = | 
|  | 2815 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 2816 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 2817 |  | 
|  | 2818 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); | 
|  | 2819 |  | 
|  | 2820 | const int32_t mouseDeviceId = 7; | 
|  | 2821 | const int32_t touchDeviceId = 4; | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2822 |  | 
|  | 2823 | // Hover a bit with mouse first | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2824 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) | 
|  | 2825 | .deviceId(mouseDeviceId) | 
|  | 2826 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) | 
|  | 2827 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2828 | spyWindow->consumeMotionEvent( | 
|  | 2829 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); | 
|  | 2830 | window->consumeMotionEvent( | 
|  | 2831 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); | 
|  | 2832 |  | 
|  | 2833 | // Start touching | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2834 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2835 | .deviceId(touchDeviceId) | 
|  | 2836 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) | 
|  | 2837 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2838 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 2839 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 2840 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 2841 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 2842 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2843 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2844 | .deviceId(touchDeviceId) | 
|  | 2845 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) | 
|  | 2846 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2847 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); | 
|  | 2848 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); | 
|  | 2849 |  | 
|  | 2850 | // Pilfer the stream | 
|  | 2851 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); | 
|  | 2852 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); | 
|  | 2853 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2854 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2855 | .deviceId(touchDeviceId) | 
|  | 2856 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) | 
|  | 2857 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2858 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); | 
|  | 2859 |  | 
|  | 2860 | // Mouse down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2861 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 2862 | .deviceId(mouseDeviceId) | 
|  | 2863 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2864 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) | 
|  | 2865 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2866 |  | 
|  | 2867 | spyWindow->consumeMotionEvent( | 
|  | 2868 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); | 
|  | 2869 | spyWindow->consumeMotionEvent( | 
|  | 2870 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); | 
|  | 2871 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); | 
|  | 2872 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2873 | mDispatcher->notifyMotion( | 
|  | 2874 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) | 
|  | 2875 | .deviceId(mouseDeviceId) | 
|  | 2876 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2877 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2878 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) | 
|  | 2879 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2880 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
|  | 2881 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
|  | 2882 |  | 
|  | 2883 | // Mouse move! | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2884 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) | 
|  | 2885 | .deviceId(mouseDeviceId) | 
|  | 2886 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 2887 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) | 
|  | 2888 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2889 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); | 
|  | 2890 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); | 
|  | 2891 |  | 
|  | 2892 | // Touch move! | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2893 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 2894 | .deviceId(touchDeviceId) | 
|  | 2895 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) | 
|  | 2896 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2897 |  | 
|  | 2898 | // No more events | 
|  | 2899 | spyWindow->assertNoEvents(); | 
|  | 2900 | window->assertNoEvents(); | 
|  | 2901 | } | 
|  | 2902 |  | 
|  | 2903 | /** | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2904 | * On the display, have a single window, and also an area where there's no window. | 
|  | 2905 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. | 
|  | 2906 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. | 
|  | 2907 | */ | 
|  | 2908 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { | 
|  | 2909 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2910 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2911 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2912 |  | 
|  | 2913 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2914 |  | 
|  | 2915 | // Touch down on the empty space | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2916 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2917 |  | 
|  | 2918 | mDispatcher->waitForIdle(); | 
|  | 2919 | window->assertNoEvents(); | 
|  | 2920 |  | 
|  | 2921 | // Now touch down on the window with another pointer | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2922 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2923 | mDispatcher->waitForIdle(); | 
|  | 2924 | window->consumeMotionDown(); | 
|  | 2925 | } | 
|  | 2926 |  | 
|  | 2927 | /** | 
|  | 2928 | * Same test as above, but instead of touching the empty space, the first touch goes to | 
|  | 2929 | * non-touchable window. | 
|  | 2930 | */ | 
|  | 2931 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { | 
|  | 2932 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2933 | sp<FakeWindowHandle> window1 = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2934 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2935 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); | 
|  | 2936 | window1->setTouchable(false); | 
|  | 2937 | sp<FakeWindowHandle> window2 = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2938 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2939 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); | 
|  | 2940 |  | 
|  | 2941 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); | 
|  | 2942 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2943 | // Touch down on the non-touchable window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2944 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2945 |  | 
|  | 2946 | mDispatcher->waitForIdle(); | 
|  | 2947 | window1->assertNoEvents(); | 
|  | 2948 | window2->assertNoEvents(); | 
|  | 2949 |  | 
|  | 2950 | // Now touch down on the window with another pointer | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2951 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2952 | mDispatcher->waitForIdle(); | 
|  | 2953 | window2->consumeMotionDown(); | 
|  | 2954 | } | 
|  | 2955 |  | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2956 | /** | 
|  | 2957 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds | 
|  | 2958 | * to the event time of the first ACTION_DOWN sent to the particular window. | 
|  | 2959 | */ | 
|  | 2960 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { | 
|  | 2961 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 2962 | sp<FakeWindowHandle> window1 = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2963 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2964 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); | 
|  | 2965 | sp<FakeWindowHandle> window2 = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2966 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2967 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); | 
|  | 2968 |  | 
|  | 2969 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); | 
|  | 2970 |  | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2971 | // Touch down on the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2972 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2973 |  | 
|  | 2974 | mDispatcher->waitForIdle(); | 
|  | 2975 | InputEvent* inputEvent1 = window1->consume(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2976 | ASSERT_NE(inputEvent1, nullptr); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2977 | window2->assertNoEvents(); | 
|  | 2978 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); | 
|  | 2979 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); | 
|  | 2980 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); | 
|  | 2981 |  | 
|  | 2982 | // Now touch down on the window with another pointer | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2983 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2984 | mDispatcher->waitForIdle(); | 
|  | 2985 | InputEvent* inputEvent2 = window2->consume(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2986 | ASSERT_NE(inputEvent2, nullptr); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2987 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); | 
|  | 2988 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); | 
|  | 2989 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); | 
|  | 2990 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); | 
|  | 2991 |  | 
|  | 2992 | // Now move the pointer on the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2993 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2994 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2995 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2996 |  | 
|  | 2997 | // Now add new touch down on the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2998 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2999 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3000 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3001 |  | 
|  | 3002 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line | 
|  | 3003 | window1->consumeMotionMove(); | 
|  | 3004 | window1->assertNoEvents(); | 
|  | 3005 |  | 
|  | 3006 | // Now move the pointer on the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3007 | mDispatcher->notifyMotion( | 
|  | 3008 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3009 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3010 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3011 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3012 | mDispatcher->notifyMotion( | 
|  | 3013 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3014 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3015 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); | 
| Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3016 | } | 
|  | 3017 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3018 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3019 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3020 | sp<FakeWindowHandle> windowLeft = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3021 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3022 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3023 | sp<FakeWindowHandle> windowRight = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3024 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3025 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3026 |  | 
|  | 3027 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3028 |  | 
|  | 3029 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
|  | 3030 |  | 
|  | 3031 | // 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] | 3032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3033 | injectMotionEvent(mDispatcher, | 
|  | 3034 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 3035 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3036 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3037 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3038 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3039 |  | 
|  | 3040 | // Move cursor into left window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3042 | injectMotionEvent(mDispatcher, | 
|  | 3043 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 3044 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3045 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3046 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3047 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); | 
|  | 3048 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3049 |  | 
|  | 3050 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3051 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3052 | injectMotionEvent(mDispatcher, | 
|  | 3053 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3054 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3055 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3056 | .build())); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3057 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 3058 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3059 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3060 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3061 | injectMotionEvent(mDispatcher, | 
|  | 3062 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 3063 | AINPUT_SOURCE_MOUSE) | 
|  | 3064 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 3065 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3066 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3067 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3068 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3069 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3070 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3071 | injectMotionEvent(mDispatcher, | 
|  | 3072 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 3073 | AINPUT_SOURCE_MOUSE) | 
|  | 3074 | .buttonState(0) | 
|  | 3075 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3076 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3077 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3078 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3079 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3080 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3081 | injectMotionEvent(mDispatcher, | 
|  | 3082 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 3083 | .buttonState(0) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3084 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3085 | .build())); | 
|  | 3086 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3087 |  | 
|  | 3088 | // Move mouse cursor back to right window | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3089 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3090 | injectMotionEvent(mDispatcher, | 
|  | 3091 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 3092 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3093 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3094 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3095 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3096 |  | 
|  | 3097 | // No more events | 
|  | 3098 | windowLeft->assertNoEvents(); | 
|  | 3099 | windowRight->assertNoEvents(); | 
|  | 3100 | } | 
|  | 3101 |  | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3102 | /** | 
|  | 3103 | * Put two fingers down (and don't release them) and click the mouse button. | 
|  | 3104 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the | 
|  | 3105 | * currently active gesture should be canceled, and the new one should proceed. | 
|  | 3106 | */ | 
|  | 3107 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { | 
|  | 3108 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3109 | sp<FakeWindowHandle> window = | 
|  | 3110 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3111 | window->setFrame(Rect(0, 0, 600, 800)); | 
|  | 3112 |  | 
|  | 3113 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3114 |  | 
|  | 3115 | const int32_t touchDeviceId = 4; | 
|  | 3116 | const int32_t mouseDeviceId = 6; | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3117 |  | 
|  | 3118 | // Two pointers down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3119 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3120 | .deviceId(touchDeviceId) | 
|  | 3121 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 3122 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3123 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3124 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3125 | .deviceId(touchDeviceId) | 
|  | 3126 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 3127 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) | 
|  | 3128 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3129 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 3130 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); | 
|  | 3131 |  | 
|  | 3132 | // Inject a series of mouse events for a mouse click | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3133 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3134 | .deviceId(mouseDeviceId) | 
|  | 3135 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 3136 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
|  | 3137 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3138 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), | 
|  | 3139 | WithPointerCount(2u))); | 
|  | 3140 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); | 
|  | 3141 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3142 | mDispatcher->notifyMotion( | 
|  | 3143 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) | 
|  | 3144 | .deviceId(mouseDeviceId) | 
|  | 3145 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 3146 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 3147 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
|  | 3148 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3149 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
|  | 3150 |  | 
|  | 3151 | // Try to send more touch events while the mouse is down. Since it's a continuation of an | 
|  | 3152 | // already canceled gesture, it should be ignored. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3153 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3154 | .deviceId(touchDeviceId) | 
|  | 3155 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) | 
|  | 3156 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) | 
|  | 3157 | .build()); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3158 | window->assertNoEvents(); | 
|  | 3159 | } | 
|  | 3160 |  | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3161 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { | 
|  | 3162 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3163 |  | 
|  | 3164 | sp<FakeWindowHandle> spyWindow = | 
|  | 3165 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
|  | 3166 | spyWindow->setFrame(Rect(0, 0, 600, 800)); | 
|  | 3167 | spyWindow->setTrustedOverlay(true); | 
|  | 3168 | spyWindow->setSpy(true); | 
|  | 3169 | sp<FakeWindowHandle> window = | 
|  | 3170 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3171 | window->setFrame(Rect(0, 0, 600, 800)); | 
|  | 3172 |  | 
|  | 3173 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3174 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); | 
|  | 3175 |  | 
|  | 3176 | // Send mouse cursor to the window | 
|  | 3177 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3178 | injectMotionEvent(mDispatcher, | 
|  | 3179 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 3180 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3181 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3182 | .x(100) | 
|  | 3183 | .y(100)) | 
|  | 3184 | .build())); | 
|  | 3185 |  | 
|  | 3186 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), | 
|  | 3187 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 3188 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), | 
|  | 3189 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 3190 |  | 
|  | 3191 | window->assertNoEvents(); | 
|  | 3192 | spyWindow->assertNoEvents(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3193 | } | 
|  | 3194 |  | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3195 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { | 
|  | 3196 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3197 |  | 
|  | 3198 | sp<FakeWindowHandle> spyWindow = | 
|  | 3199 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
|  | 3200 | spyWindow->setFrame(Rect(0, 0, 600, 800)); | 
|  | 3201 | spyWindow->setTrustedOverlay(true); | 
|  | 3202 | spyWindow->setSpy(true); | 
|  | 3203 | sp<FakeWindowHandle> window = | 
|  | 3204 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3205 | window->setFrame(Rect(0, 0, 600, 800)); | 
|  | 3206 |  | 
|  | 3207 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3208 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); | 
|  | 3209 |  | 
|  | 3210 | // Send mouse cursor to the window | 
|  | 3211 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3212 | injectMotionEvent(mDispatcher, | 
|  | 3213 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 3214 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3215 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3216 | .x(100) | 
|  | 3217 | .y(100)) | 
|  | 3218 | .build())); | 
|  | 3219 |  | 
|  | 3220 | // Move mouse cursor | 
|  | 3221 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3222 | injectMotionEvent(mDispatcher, | 
|  | 3223 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 3224 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3225 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3226 | .x(110) | 
|  | 3227 | .y(110)) | 
|  | 3228 | .build())); | 
|  | 3229 |  | 
|  | 3230 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), | 
|  | 3231 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 3232 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), | 
|  | 3233 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 3234 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), | 
|  | 3235 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 3236 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), | 
|  | 3237 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 3238 | // Touch down on the window | 
|  | 3239 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3240 | injectMotionEvent(mDispatcher, | 
|  | 3241 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 3242 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3243 | .deviceId(SECOND_DEVICE_ID) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3244 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3245 | .x(200) | 
|  | 3246 | .y(200)) | 
|  | 3247 | .build())); | 
|  | 3248 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), | 
|  | 3249 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 3250 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), | 
|  | 3251 | WithSource(AINPUT_SOURCE_MOUSE))); | 
|  | 3252 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | 
|  | 3253 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); | 
|  | 3254 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | 
|  | 3255 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); | 
|  | 3256 |  | 
|  | 3257 | // pilfer the motion, retaining the gesture on the spy window. | 
|  | 3258 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); | 
|  | 3259 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), | 
|  | 3260 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); | 
|  | 3261 |  | 
|  | 3262 | // Touch UP on the window | 
|  | 3263 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3264 | injectMotionEvent(mDispatcher, | 
|  | 3265 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, | 
|  | 3266 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3267 | .deviceId(SECOND_DEVICE_ID) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3268 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3269 | .x(200) | 
|  | 3270 | .y(200)) | 
|  | 3271 | .build())); | 
|  | 3272 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), | 
|  | 3273 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); | 
|  | 3274 |  | 
|  | 3275 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going | 
|  | 3276 | // to send a new gesture. It should again go to both windows (spy and the window below), just | 
|  | 3277 | // like the first gesture did, before pilfering. The window configuration has not changed. | 
|  | 3278 |  | 
|  | 3279 | // One more tap - DOWN | 
|  | 3280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3281 | injectMotionEvent(mDispatcher, | 
|  | 3282 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 3283 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3284 | .deviceId(SECOND_DEVICE_ID) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3285 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3286 | .x(250) | 
|  | 3287 | .y(250)) | 
|  | 3288 | .build())); | 
|  | 3289 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | 
|  | 3290 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); | 
|  | 3291 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | 
|  | 3292 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); | 
|  | 3293 |  | 
|  | 3294 | // Touch UP on the window | 
|  | 3295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3296 | injectMotionEvent(mDispatcher, | 
|  | 3297 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, | 
|  | 3298 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3299 | .deviceId(SECOND_DEVICE_ID) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3300 | .pointer(PointerBuilder(0, ToolType::FINGER) | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3301 | .x(250) | 
|  | 3302 | .y(250)) | 
|  | 3303 | .build())); | 
|  | 3304 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), | 
|  | 3305 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); | 
|  | 3306 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), | 
|  | 3307 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); | 
|  | 3308 |  | 
|  | 3309 | window->assertNoEvents(); | 
|  | 3310 | spyWindow->assertNoEvents(); | 
|  | 3311 | } | 
|  | 3312 |  | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3313 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected | 
|  | 3314 | // directly in this test. | 
|  | 3315 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3316 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3317 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3318 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3319 | window->setFrame(Rect(0, 0, 1200, 800)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3320 |  | 
|  | 3321 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3322 |  | 
|  | 3323 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3324 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3325 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3326 | injectMotionEvent(mDispatcher, | 
|  | 3327 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 3328 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3329 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3330 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3331 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3332 | // Inject a series of mouse events for a mouse click | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3333 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3334 | injectMotionEvent(mDispatcher, | 
|  | 3335 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3336 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3337 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3338 | .build())); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3339 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 3340 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3341 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3343 | injectMotionEvent(mDispatcher, | 
|  | 3344 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, | 
|  | 3345 | AINPUT_SOURCE_MOUSE) | 
|  | 3346 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 3347 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3348 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3349 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3350 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3351 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3352 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3353 | injectMotionEvent(mDispatcher, | 
|  | 3354 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, | 
|  | 3355 | AINPUT_SOURCE_MOUSE) | 
|  | 3356 | .buttonState(0) | 
|  | 3357 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3358 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3359 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3360 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3361 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3362 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3363 | injectMotionEvent(mDispatcher, | 
|  | 3364 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 3365 | .buttonState(0) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3366 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3367 | .build())); | 
|  | 3368 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 3369 |  | 
| Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3370 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the | 
|  | 3371 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. | 
|  | 3372 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3373 | injectMotionEvent(mDispatcher, | 
|  | 3374 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, | 
|  | 3375 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3376 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3377 | .build())); | 
| Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3378 | window->assertNoEvents(); | 
| Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3379 | } | 
|  | 3380 |  | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3381 | /** | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3382 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event | 
|  | 3383 | * is generated. | 
|  | 3384 | */ | 
|  | 3385 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { | 
|  | 3386 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3387 | sp<FakeWindowHandle> window = | 
|  | 3388 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3389 | window->setFrame(Rect(0, 0, 1200, 800)); | 
|  | 3390 |  | 
|  | 3391 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3392 |  | 
|  | 3393 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3394 |  | 
|  | 3395 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3396 | injectMotionEvent(mDispatcher, | 
|  | 3397 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, | 
|  | 3398 | AINPUT_SOURCE_MOUSE) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3399 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3400 | .x(300) | 
|  | 3401 | .y(400)) | 
|  | 3402 | .build())); | 
|  | 3403 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
|  | 3404 |  | 
|  | 3405 | // Remove the window, but keep the channel. | 
|  | 3406 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 3407 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); | 
|  | 3408 | } | 
|  | 3409 |  | 
|  | 3410 | /** | 
| Daniel Norman | 2f99cdb | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 3411 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. | 
|  | 3412 | */ | 
|  | 3413 | TEST_F(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash) { | 
|  | 3414 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3415 | sp<FakeWindowHandle> window = | 
|  | 3416 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3417 | window->setFrame(Rect(0, 0, 1200, 800)); | 
|  | 3418 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3419 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3420 |  | 
|  | 3421 | MotionEventBuilder hoverEnterBuilder = | 
|  | 3422 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) | 
|  | 3423 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) | 
|  | 3424 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
|  | 3425 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3426 | injectMotionEvent(mDispatcher, hoverEnterBuilder.build())); | 
|  | 3427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3428 | injectMotionEvent(mDispatcher, hoverEnterBuilder.build())); | 
|  | 3429 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
|  | 3430 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
|  | 3431 | } | 
|  | 3432 |  | 
|  | 3433 | /** | 
| Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3434 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. | 
|  | 3435 | */ | 
|  | 3436 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { | 
|  | 3437 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3438 | sp<FakeWindowHandle> window = | 
|  | 3439 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3440 | window->setFrame(Rect(0, 0, 100, 100)); | 
|  | 3441 |  | 
|  | 3442 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3443 |  | 
|  | 3444 | const int32_t mouseDeviceId = 7; | 
|  | 3445 | const int32_t touchDeviceId = 4; | 
| Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3446 |  | 
|  | 3447 | // Start hovering with the mouse | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3448 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) | 
|  | 3449 | .deviceId(mouseDeviceId) | 
|  | 3450 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) | 
|  | 3451 | .build()); | 
| Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3452 | window->consumeMotionEvent( | 
|  | 3453 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); | 
|  | 3454 |  | 
|  | 3455 | // Touch goes down | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3456 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3457 | .deviceId(touchDeviceId) | 
|  | 3458 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) | 
|  | 3459 | .build()); | 
| Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3460 |  | 
|  | 3461 | window->consumeMotionEvent( | 
|  | 3462 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); | 
|  | 3463 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); | 
|  | 3464 | } | 
|  | 3465 |  | 
|  | 3466 | /** | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3467 | * Inject a mouse hover event followed by a tap from touchscreen. | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3468 | * The tap causes a HOVER_EXIT event to be generated because the current event | 
|  | 3469 | * stream's source has been switched. | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3470 | */ | 
|  | 3471 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { | 
|  | 3472 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3473 | sp<FakeWindowHandle> window = | 
|  | 3474 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3475 | window->setFrame(Rect(0, 0, 100, 100)); | 
|  | 3476 |  | 
|  | 3477 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3478 |  | 
|  | 3479 | // Inject a hover_move from mouse. | 
|  | 3480 | NotifyMotionArgs motionArgs = | 
|  | 3481 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, | 
|  | 3482 | ADISPLAY_ID_DEFAULT, {{50, 50}}); | 
|  | 3483 | motionArgs.xCursorPosition = 50; | 
|  | 3484 | motionArgs.yCursorPosition = 50; | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3485 | mDispatcher->notifyMotion(motionArgs); | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3486 | ASSERT_NO_FATAL_FAILURE( | 
|  | 3487 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), | 
|  | 3488 | WithSource(AINPUT_SOURCE_MOUSE)))); | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3489 |  | 
|  | 3490 | // Tap on the window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3491 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 3492 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3493 | {{10, 10}})); | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3494 | ASSERT_NO_FATAL_FAILURE( | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3495 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), | 
|  | 3496 | WithSource(AINPUT_SOURCE_MOUSE)))); | 
|  | 3497 |  | 
|  | 3498 | ASSERT_NO_FATAL_FAILURE( | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3499 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), | 
|  | 3500 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); | 
|  | 3501 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3502 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3503 | ADISPLAY_ID_DEFAULT, {{10, 10}})); | 
| Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3504 | ASSERT_NO_FATAL_FAILURE( | 
|  | 3505 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), | 
|  | 3506 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); | 
|  | 3507 | } | 
|  | 3508 |  | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3509 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { | 
|  | 3510 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3511 | sp<FakeWindowHandle> windowDefaultDisplay = | 
|  | 3512 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", | 
|  | 3513 | ADISPLAY_ID_DEFAULT); | 
|  | 3514 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); | 
|  | 3515 | sp<FakeWindowHandle> windowSecondDisplay = | 
|  | 3516 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", | 
|  | 3517 | SECOND_DISPLAY_ID); | 
|  | 3518 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); | 
|  | 3519 |  | 
|  | 3520 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, | 
|  | 3521 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); | 
|  | 3522 |  | 
|  | 3523 | // Set cursor position in window in default display and check that hover enter and move | 
|  | 3524 | // events are generated. | 
|  | 3525 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3526 | injectMotionEvent(mDispatcher, | 
|  | 3527 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 3528 | AINPUT_SOURCE_MOUSE) | 
|  | 3529 | .displayId(ADISPLAY_ID_DEFAULT) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3530 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3531 | .x(300) | 
|  | 3532 | .y(600)) | 
|  | 3533 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3534 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3535 |  | 
|  | 3536 | // Remove all windows in secondary display and check that no event happens on window in | 
|  | 3537 | // primary display. | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3538 | mDispatcher->setInputWindows( | 
|  | 3539 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3540 | windowDefaultDisplay->assertNoEvents(); | 
|  | 3541 |  | 
|  | 3542 | // Move cursor position in window in default display and check that only hover move | 
|  | 3543 | // event is generated and not hover enter event. | 
|  | 3544 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, | 
|  | 3545 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); | 
|  | 3546 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3547 | injectMotionEvent(mDispatcher, | 
|  | 3548 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, | 
|  | 3549 | AINPUT_SOURCE_MOUSE) | 
|  | 3550 | .displayId(ADISPLAY_ID_DEFAULT) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3551 | .pointer(PointerBuilder(0, ToolType::MOUSE) | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3552 | .x(400) | 
|  | 3553 | .y(700)) | 
|  | 3554 | .build())); | 
| Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3555 | windowDefaultDisplay->consumeMotionEvent( | 
|  | 3556 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), | 
|  | 3557 | WithSource(AINPUT_SOURCE_MOUSE))); | 
| Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3558 | windowDefaultDisplay->assertNoEvents(); | 
|  | 3559 | } | 
|  | 3560 |  | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3561 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3562 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3563 |  | 
|  | 3564 | sp<FakeWindowHandle> windowLeft = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3565 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3566 | windowLeft->setFrame(Rect(0, 0, 600, 800)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3567 | sp<FakeWindowHandle> windowRight = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3568 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3569 | windowRight->setFrame(Rect(600, 0, 1200, 800)); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3570 |  | 
|  | 3571 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 3572 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3573 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3574 |  | 
|  | 3575 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of | 
|  | 3576 | // left window. This event should be dispatched to the left window. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3577 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3578 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3579 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3580 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3581 | windowRight->assertNoEvents(); | 
|  | 3582 | } | 
|  | 3583 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3584 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3585 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3586 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3587 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3588 | window->setFocusable(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3589 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3590 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3591 | setFocusedWindow(window); | 
|  | 3592 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3593 | window->consumeFocusEvent(true); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3594 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3595 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3596 |  | 
|  | 3597 | // Window should receive key down event. | 
|  | 3598 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3599 |  | 
|  | 3600 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED | 
|  | 3601 | // on the app side. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3602 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3603 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3604 | AKEY_EVENT_FLAG_CANCELED); | 
|  | 3605 | } | 
|  | 3606 |  | 
|  | 3607 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3608 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3609 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3610 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3611 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3612 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3613 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3614 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 3615 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3616 |  | 
|  | 3617 | // Window should receive motion down event. | 
|  | 3618 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3619 |  | 
|  | 3620 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL | 
|  | 3621 | // on the app side. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3622 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3623 | window->consumeMotionEvent( | 
|  | 3624 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3625 | } | 
|  | 3626 |  | 
| Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 3627 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { | 
|  | 3628 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3629 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3630 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 3631 |  | 
|  | 3632 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3633 |  | 
|  | 3634 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) | 
|  | 3635 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) | 
|  | 3636 | .build()); | 
|  | 3637 |  | 
|  | 3638 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); | 
|  | 3639 |  | 
|  | 3640 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT | 
|  | 3641 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); | 
|  | 3642 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); | 
|  | 3643 |  | 
|  | 3644 | // After the device has been reset, a new hovering stream can be sent to the window | 
|  | 3645 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) | 
|  | 3646 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) | 
|  | 3647 | .build()); | 
|  | 3648 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); | 
|  | 3649 | } | 
|  | 3650 |  | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3651 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { | 
|  | 3652 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3653 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3654 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3655 | window->setFocusable(true); | 
|  | 3656 |  | 
|  | 3657 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3658 | setFocusedWindow(window); | 
|  | 3659 |  | 
|  | 3660 | window->consumeFocusEvent(true); | 
|  | 3661 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3662 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3663 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; | 
|  | 3664 | const nsecs_t injectTime = keyArgs.eventTime; | 
|  | 3665 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3666 | mDispatcher->notifyKey(keyArgs); | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3667 | // The dispatching time should be always greater than or equal to intercept key timeout. | 
|  | 3668 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3669 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= | 
|  | 3670 | std::chrono::nanoseconds(interceptKeyTimeout).count()); | 
|  | 3671 | } | 
|  | 3672 |  | 
| Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 3673 | /** | 
|  | 3674 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. | 
|  | 3675 | */ | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3676 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { | 
|  | 3677 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3678 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3679 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3680 | window->setFocusable(true); | 
|  | 3681 |  | 
|  | 3682 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3683 | setFocusedWindow(window); | 
|  | 3684 |  | 
|  | 3685 | window->consumeFocusEvent(true); | 
|  | 3686 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3687 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3688 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 3689 |  | 
|  | 3690 | // Set a value that's significantly larger than the default consumption timeout. If the | 
|  | 3691 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. | 
|  | 3692 | mFakePolicy->setInterceptKeyTimeout(600ms); | 
|  | 3693 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); | 
|  | 3694 | // Window should receive key event immediately when same key up. | 
| Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3695 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 3696 | } | 
|  | 3697 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3698 | /** | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3699 | * Two windows. First is a regular window. Second does not overlap with the first, and has | 
|  | 3700 | * WATCH_OUTSIDE_TOUCH. | 
|  | 3701 | * Both windows are owned by the same UID. | 
|  | 3702 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero | 
|  | 3703 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. | 
|  | 3704 | */ | 
|  | 3705 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { | 
|  | 3706 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3707 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3708 | "First Window", ADISPLAY_ID_DEFAULT); | 
|  | 3709 | window->setFrame(Rect{0, 0, 100, 100}); | 
|  | 3710 |  | 
|  | 3711 | sp<FakeWindowHandle> outsideWindow = | 
|  | 3712 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 3713 | ADISPLAY_ID_DEFAULT); | 
|  | 3714 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); | 
|  | 3715 | outsideWindow->setWatchOutsideTouch(true); | 
|  | 3716 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped | 
|  | 3717 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); | 
|  | 3718 |  | 
|  | 3719 | // Tap on first window. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3720 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 3721 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3722 | {PointF{50, 50}})); | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3723 | window->consumeMotionDown(); | 
|  | 3724 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. | 
|  | 3725 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. | 
|  | 3726 | outsideWindow->consumeMotionEvent( | 
|  | 3727 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); | 
|  | 3728 | } | 
|  | 3729 |  | 
|  | 3730 | /** | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3731 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when | 
|  | 3732 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one | 
|  | 3733 | * ACTION_OUTSIDE event is sent per gesture. | 
|  | 3734 | */ | 
|  | 3735 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { | 
|  | 3736 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. | 
|  | 3737 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3738 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3739 | "First Window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3740 | window->setWatchOutsideTouch(true); | 
|  | 3741 | window->setFrame(Rect{0, 0, 100, 100}); | 
|  | 3742 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3743 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 3744 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3745 | secondWindow->setFrame(Rect{100, 100, 200, 200}); | 
|  | 3746 | sp<FakeWindowHandle> thirdWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3747 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", | 
|  | 3748 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3749 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); | 
|  | 3750 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); | 
|  | 3751 |  | 
|  | 3752 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3753 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 3754 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3755 | {PointF{-10, -10}})); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3756 | window->assertNoEvents(); | 
|  | 3757 | secondWindow->assertNoEvents(); | 
|  | 3758 |  | 
|  | 3759 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. | 
|  | 3760 | // Now, `window` should get ACTION_OUTSIDE. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3761 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 3762 | ADISPLAY_ID_DEFAULT, | 
|  | 3763 | {PointF{-10, -10}, PointF{105, 105}})); | 
| Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3764 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; | 
|  | 3765 | window->consumeMotionEvent( | 
|  | 3766 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3767 | secondWindow->consumeMotionDown(); | 
|  | 3768 | thirdWindow->assertNoEvents(); | 
|  | 3769 |  | 
|  | 3770 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is | 
|  | 3771 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3772 | mDispatcher->notifyMotion( | 
|  | 3773 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3774 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3775 | window->assertNoEvents(); | 
|  | 3776 | secondWindow->consumeMotionMove(); | 
|  | 3777 | thirdWindow->consumeMotionDown(); | 
|  | 3778 | } | 
|  | 3779 |  | 
| Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3780 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { | 
|  | 3781 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3782 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3783 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3784 | window->setFocusable(true); | 
|  | 3785 |  | 
| Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3786 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); | 
| Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3787 | setFocusedWindow(window); | 
|  | 3788 |  | 
|  | 3789 | window->consumeFocusEvent(true); | 
|  | 3790 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3791 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
|  | 3792 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
|  | 3793 | mDispatcher->notifyKey(keyDown); | 
|  | 3794 | mDispatcher->notifyKey(keyUp); | 
| Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3795 |  | 
|  | 3796 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 3797 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 3798 |  | 
|  | 3799 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. | 
| Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3800 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); | 
| Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3801 |  | 
|  | 3802 | window->consumeFocusEvent(false); | 
|  | 3803 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3804 | mDispatcher->notifyKey(keyDown); | 
|  | 3805 | mDispatcher->notifyKey(keyUp); | 
| Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3806 | window->assertNoEvents(); | 
|  | 3807 | } | 
|  | 3808 |  | 
| Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3809 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { | 
|  | 3810 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3811 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 3812 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 3813 | // Ensure window is non-split and have some transform. | 
|  | 3814 | window->setPreventSplitting(true); | 
|  | 3815 | window->setWindowOffset(20, 40); | 
| Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3816 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); | 
| Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3817 |  | 
|  | 3818 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3819 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 3820 | {50, 50})) | 
|  | 3821 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3822 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 3823 |  | 
|  | 3824 | const MotionEvent secondFingerDownEvent = | 
|  | 3825 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3826 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 3827 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3828 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) | 
|  | 3829 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) | 
| Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3830 | .build(); | 
|  | 3831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 3832 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 3833 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 3834 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 3835 |  | 
|  | 3836 | const MotionEvent* event = window->consumeMotion(); | 
|  | 3837 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); | 
|  | 3838 | EXPECT_EQ(70, event->getX(0));  // 50 + 20 | 
|  | 3839 | EXPECT_EQ(90, event->getY(0));  // 50 + 40 | 
|  | 3840 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 | 
|  | 3841 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 | 
|  | 3842 | } | 
|  | 3843 |  | 
| Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 3844 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { | 
|  | 3845 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3846 | sp<FakeWindowHandle> window = | 
|  | 3847 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3848 | window->setFrame(Rect(0, 0, 400, 400)); | 
|  | 3849 | sp<FakeWindowHandle> trustedOverlay = | 
|  | 3850 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", | 
|  | 3851 | ADISPLAY_ID_DEFAULT); | 
|  | 3852 | trustedOverlay->setSpy(true); | 
|  | 3853 | trustedOverlay->setTrustedOverlay(true); | 
|  | 3854 |  | 
|  | 3855 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}}); | 
|  | 3856 |  | 
|  | 3857 | // Start a three-finger touchpad swipe | 
|  | 3858 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3859 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) | 
|  | 3860 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3861 | .build()); | 
|  | 3862 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3863 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) | 
|  | 3864 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) | 
|  | 3865 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3866 | .build()); | 
|  | 3867 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3868 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) | 
|  | 3869 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) | 
|  | 3870 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) | 
|  | 3871 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3872 | .build()); | 
|  | 3873 |  | 
|  | 3874 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 3875 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); | 
|  | 3876 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); | 
|  | 3877 |  | 
|  | 3878 | // Move the swipe a bit | 
|  | 3879 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) | 
|  | 3880 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) | 
|  | 3881 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) | 
|  | 3882 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) | 
|  | 3883 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3884 | .build()); | 
|  | 3885 |  | 
|  | 3886 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); | 
|  | 3887 |  | 
|  | 3888 | // End the swipe | 
|  | 3889 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) | 
|  | 3890 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) | 
|  | 3891 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) | 
|  | 3892 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) | 
|  | 3893 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3894 | .build()); | 
|  | 3895 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) | 
|  | 3896 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) | 
|  | 3897 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) | 
|  | 3898 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3899 | .build()); | 
|  | 3900 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 3901 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) | 
|  | 3902 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3903 | .build()); | 
|  | 3904 |  | 
|  | 3905 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); | 
|  | 3906 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); | 
|  | 3907 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); | 
|  | 3908 |  | 
|  | 3909 | window->assertNoEvents(); | 
|  | 3910 | } | 
|  | 3911 |  | 
|  | 3912 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { | 
|  | 3913 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3914 | sp<FakeWindowHandle> window = | 
|  | 3915 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3916 | window->setFrame(Rect(0, 0, 400, 400)); | 
|  | 3917 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3918 |  | 
|  | 3919 | // Start a three-finger touchpad swipe | 
|  | 3920 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3921 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) | 
|  | 3922 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3923 | .build()); | 
|  | 3924 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3925 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) | 
|  | 3926 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) | 
|  | 3927 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3928 | .build()); | 
|  | 3929 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 3930 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) | 
|  | 3931 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) | 
|  | 3932 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) | 
|  | 3933 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3934 | .build()); | 
|  | 3935 |  | 
|  | 3936 | // Move the swipe a bit | 
|  | 3937 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) | 
|  | 3938 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) | 
|  | 3939 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) | 
|  | 3940 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) | 
|  | 3941 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3942 | .build()); | 
|  | 3943 |  | 
|  | 3944 | // End the swipe | 
|  | 3945 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) | 
|  | 3946 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) | 
|  | 3947 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) | 
|  | 3948 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) | 
|  | 3949 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3950 | .build()); | 
|  | 3951 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) | 
|  | 3952 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) | 
|  | 3953 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) | 
|  | 3954 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3955 | .build()); | 
|  | 3956 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 3957 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) | 
|  | 3958 | .classification(MotionClassification::MULTI_FINGER_SWIPE) | 
|  | 3959 | .build()); | 
|  | 3960 |  | 
|  | 3961 | window->assertNoEvents(); | 
|  | 3962 | } | 
|  | 3963 |  | 
| Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3964 | /** | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 3965 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to | 
|  | 3966 | * the first pointer. | 
|  | 3967 | * Ensure that the second pointer is not sent to the window. | 
|  | 3968 | * | 
|  | 3969 | * The subsequent gesture should be correctly delivered to the window. | 
|  | 3970 | */ | 
|  | 3971 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { | 
|  | 3972 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 3973 | sp<FakeWindowHandle> window = | 
|  | 3974 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 3975 | window->setFrame(Rect(0, 0, 400, 400)); | 
|  | 3976 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 3977 |  | 
|  | 3978 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 3979 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 3980 | .downTime(baseTime + 10) | 
|  | 3981 | .eventTime(baseTime + 10) | 
|  | 3982 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 3983 | .build()); | 
|  | 3984 |  | 
|  | 3985 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 3986 |  | 
|  | 3987 | // We need a new window object for the same window, because dispatcher will store objects by | 
|  | 3988 | // reference. That means that the testing code and the dispatcher will refer to the same shared | 
|  | 3989 | // object. Calling window->setTransform here would affect dispatcher's comparison | 
|  | 3990 | // of the old window to the new window, since both the old window and the new window would be | 
|  | 3991 | // updated to the same value. | 
|  | 3992 | sp<FakeWindowHandle> windowDup = window->duplicate(); | 
|  | 3993 |  | 
|  | 3994 | // Change the transform so that the orientation is now different from original. | 
|  | 3995 | windowDup->setWindowTransform(0, -1, 1, 0); | 
|  | 3996 |  | 
|  | 3997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDup}}}); | 
|  | 3998 |  | 
|  | 3999 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); | 
|  | 4000 |  | 
|  | 4001 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 4002 | .downTime(baseTime + 10) | 
|  | 4003 | .eventTime(baseTime + 30) | 
|  | 4004 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 4005 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) | 
|  | 4006 | .build()); | 
|  | 4007 |  | 
|  | 4008 | // Finish the gesture and start a new one. Ensure the new gesture is sent to the window | 
|  | 4009 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 4010 | .downTime(baseTime + 10) | 
|  | 4011 | .eventTime(baseTime + 40) | 
|  | 4012 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 4013 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) | 
|  | 4014 | .build()); | 
|  | 4015 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 4016 | .downTime(baseTime + 10) | 
|  | 4017 | .eventTime(baseTime + 50) | 
|  | 4018 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
|  | 4019 | .build()); | 
|  | 4020 |  | 
|  | 4021 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 4022 | .downTime(baseTime + 60) | 
|  | 4023 | .eventTime(baseTime + 60) | 
|  | 4024 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) | 
|  | 4025 | .build()); | 
|  | 4026 |  | 
|  | 4027 | windowDup->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 4028 | } | 
|  | 4029 |  | 
|  | 4030 | /** | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4031 | * Ensure the correct coordinate spaces are used by InputDispatcher. | 
|  | 4032 | * | 
|  | 4033 | * InputDispatcher works in the display space, so its coordinate system is relative to the display | 
|  | 4034 | * panel. Windows get events in the window space, and get raw coordinates in the logical display | 
|  | 4035 | * space. | 
|  | 4036 | */ | 
|  | 4037 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { | 
|  | 4038 | public: | 
|  | 4039 | void SetUp() override { | 
|  | 4040 | InputDispatcherTest::SetUp(); | 
| Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4041 | removeAllWindowsAndDisplays(); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4042 | } | 
|  | 4043 |  | 
|  | 4044 | void addDisplayInfo(int displayId, const ui::Transform& transform) { | 
|  | 4045 | gui::DisplayInfo info; | 
|  | 4046 | info.displayId = displayId; | 
|  | 4047 | info.transform = transform; | 
|  | 4048 | mDisplayInfos.push_back(std::move(info)); | 
| Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4049 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4050 | } | 
|  | 4051 |  | 
|  | 4052 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { | 
|  | 4053 | mWindowInfos.push_back(*windowHandle->getInfo()); | 
| Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4054 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4055 | } | 
|  | 4056 |  | 
| Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4057 | void removeAllWindowsAndDisplays() { | 
|  | 4058 | mDisplayInfos.clear(); | 
|  | 4059 | mWindowInfos.clear(); | 
|  | 4060 | } | 
|  | 4061 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4062 | // Set up a test scenario where the display has a scaled projection and there are two windows | 
|  | 4063 | // on the display. | 
|  | 4064 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { | 
|  | 4065 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions | 
|  | 4066 | // respectively. | 
|  | 4067 | ui::Transform displayTransform; | 
|  | 4068 | displayTransform.set(2, 0, 0, 4); | 
|  | 4069 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); | 
|  | 4070 |  | 
|  | 4071 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 4072 | std::make_shared<FakeApplicationHandle>(); | 
|  | 4073 |  | 
|  | 4074 | // Add two windows to the display. Their frames are represented in the display space. | 
|  | 4075 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4076 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 4077 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4078 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); | 
|  | 4079 | addWindow(firstWindow); | 
|  | 4080 |  | 
|  | 4081 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4082 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 4083 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4084 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); | 
|  | 4085 | addWindow(secondWindow); | 
|  | 4086 | return {std::move(firstWindow), std::move(secondWindow)}; | 
|  | 4087 | } | 
|  | 4088 |  | 
|  | 4089 | private: | 
|  | 4090 | std::vector<gui::DisplayInfo> mDisplayInfos; | 
|  | 4091 | std::vector<gui::WindowInfo> mWindowInfos; | 
|  | 4092 | }; | 
|  | 4093 |  | 
| Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4094 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4095 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); | 
|  | 4096 | // 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] | 4097 | // selected so that if the hit test was performed with the point and the bounds being in | 
|  | 4098 | // different coordinate spaces, the event would end up in the incorrect window. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4099 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4100 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4101 | {PointF{75, 55}})); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4102 |  | 
|  | 4103 | firstWindow->consumeMotionDown(); | 
|  | 4104 | secondWindow->assertNoEvents(); | 
|  | 4105 | } | 
|  | 4106 |  | 
|  | 4107 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, | 
|  | 4108 | // the event should be treated as being in the logical display space. | 
|  | 4109 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { | 
|  | 4110 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); | 
|  | 4111 | // Send down to the first window. The point is represented in the logical display space. The | 
|  | 4112 | // point is selected so that if the hit test was done in logical display space, then it would | 
|  | 4113 | // end up in the incorrect window. | 
|  | 4114 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4115 | PointF{75 * 2, 55 * 4}); | 
|  | 4116 |  | 
|  | 4117 | firstWindow->consumeMotionDown(); | 
|  | 4118 | secondWindow->assertNoEvents(); | 
|  | 4119 | } | 
|  | 4120 |  | 
| Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4121 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed | 
|  | 4122 | // event should be treated as being in the logical display space. | 
|  | 4123 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { | 
|  | 4124 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); | 
|  | 4125 |  | 
|  | 4126 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; | 
|  | 4127 | ui::Transform injectedEventTransform; | 
|  | 4128 | injectedEventTransform.set(matrix); | 
|  | 4129 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. | 
|  | 4130 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); | 
|  | 4131 |  | 
|  | 4132 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 4133 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 4134 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4135 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) | 
| Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4136 | .x(untransformedPoint.x) | 
|  | 4137 | .y(untransformedPoint.y)) | 
|  | 4138 | .build(); | 
|  | 4139 | event.transform(matrix); | 
|  | 4140 |  | 
|  | 4141 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, | 
|  | 4142 | InputEventInjectionSync::WAIT_FOR_RESULT); | 
|  | 4143 |  | 
|  | 4144 | firstWindow->consumeMotionDown(); | 
|  | 4145 | secondWindow->assertNoEvents(); | 
|  | 4146 | } | 
|  | 4147 |  | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4148 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { | 
|  | 4149 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); | 
|  | 4150 |  | 
|  | 4151 | // Send down to the second window. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4152 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4153 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4154 | {PointF{150, 220}})); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4155 |  | 
|  | 4156 | firstWindow->assertNoEvents(); | 
|  | 4157 | const MotionEvent* event = secondWindow->consumeMotion(); | 
| Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 4158 | ASSERT_NE(nullptr, event); | 
| Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4159 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); | 
|  | 4160 |  | 
|  | 4161 | // Ensure that the events from the "getRaw" API are in logical display coordinates. | 
|  | 4162 | EXPECT_EQ(300, event->getRawX(0)); | 
|  | 4163 | EXPECT_EQ(880, event->getRawY(0)); | 
|  | 4164 |  | 
|  | 4165 | // Ensure that the x and y values are in the window's coordinate space. | 
|  | 4166 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in | 
|  | 4167 | // the logical display space. This will be the origin of the window space. | 
|  | 4168 | EXPECT_EQ(100, event->getX(0)); | 
|  | 4169 | EXPECT_EQ(80, event->getY(0)); | 
|  | 4170 | } | 
|  | 4171 |  | 
| Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4172 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ | 
|  | 4173 | class InputDispatcherDisplayOrientationFixture | 
|  | 4174 | : public InputDispatcherDisplayProjectionTest, | 
|  | 4175 | public ::testing::WithParamInterface<ui::Rotation> {}; | 
|  | 4176 |  | 
|  | 4177 | // This test verifies the touchable region of a window for all rotations of the display by tapping | 
|  | 4178 | // in different locations on the display, specifically points close to the four corners of a | 
|  | 4179 | // window. | 
|  | 4180 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { | 
|  | 4181 | constexpr static int32_t displayWidth = 400; | 
|  | 4182 | constexpr static int32_t displayHeight = 800; | 
|  | 4183 |  | 
|  | 4184 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4185 |  | 
|  | 4186 | const auto rotation = GetParam(); | 
|  | 4187 |  | 
|  | 4188 | // Set up the display with the specified rotation. | 
|  | 4189 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; | 
|  | 4190 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; | 
|  | 4191 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; | 
|  | 4192 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), | 
|  | 4193 | logicalDisplayWidth, logicalDisplayHeight); | 
|  | 4194 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); | 
|  | 4195 |  | 
|  | 4196 | // Create a window with its bounds determined in the logical display. | 
|  | 4197 | const Rect frameInLogicalDisplay(100, 100, 200, 300); | 
|  | 4198 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); | 
|  | 4199 | sp<FakeWindowHandle> window = | 
|  | 4200 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 4201 | window->setFrame(frameInDisplay, displayTransform); | 
|  | 4202 | addWindow(window); | 
|  | 4203 |  | 
|  | 4204 | // The following points in logical display space should be inside the window. | 
|  | 4205 | static const std::array<vec2, 4> insidePoints{ | 
|  | 4206 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; | 
|  | 4207 | for (const auto pointInsideWindow : insidePoints) { | 
|  | 4208 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); | 
|  | 4209 | const PointF pointInDisplaySpace{p.x, p.y}; | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4210 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4211 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4212 | {pointInDisplaySpace})); | 
| Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4213 | window->consumeMotionDown(); | 
|  | 4214 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4215 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, | 
|  | 4216 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4217 | {pointInDisplaySpace})); | 
| Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4218 | window->consumeMotionUp(); | 
|  | 4219 | } | 
|  | 4220 |  | 
|  | 4221 | // The following points in logical display space should be outside the window. | 
|  | 4222 | static const std::array<vec2, 5> outsidePoints{ | 
|  | 4223 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; | 
|  | 4224 | for (const auto pointOutsideWindow : outsidePoints) { | 
|  | 4225 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); | 
|  | 4226 | const PointF pointInDisplaySpace{p.x, p.y}; | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4227 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4228 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4229 | {pointInDisplaySpace})); | 
| Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4230 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4231 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, | 
|  | 4232 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4233 | {pointInDisplaySpace})); | 
| Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4234 | } | 
|  | 4235 | window->assertNoEvents(); | 
|  | 4236 | } | 
|  | 4237 |  | 
|  | 4238 | // Run the precision tests for all rotations. | 
|  | 4239 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, | 
|  | 4240 | InputDispatcherDisplayOrientationFixture, | 
|  | 4241 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, | 
|  | 4242 | ui::ROTATION_270), | 
|  | 4243 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { | 
|  | 4244 | return ftl::enum_string(testParamInfo.param); | 
|  | 4245 | }); | 
|  | 4246 |  | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4247 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, | 
|  | 4248 | sp<IBinder>, sp<IBinder>)>; | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4249 |  | 
|  | 4250 | class TransferTouchFixture : public InputDispatcherTest, | 
|  | 4251 | public ::testing::WithParamInterface<TransferFunction> {}; | 
|  | 4252 |  | 
|  | 4253 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4254 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4255 |  | 
|  | 4256 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4257 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4258 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 4259 | ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4260 | firstWindow->setDupTouchToWallpaper(true); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4261 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4262 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 4263 | ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4264 | sp<FakeWindowHandle> wallpaper = | 
|  | 4265 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); | 
|  | 4266 | wallpaper->setIsWallpaper(true); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4267 | // Add the windows to the dispatcher | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4268 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4269 |  | 
|  | 4270 | // Send down to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4271 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4272 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4273 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4274 | // Only the first window should get the down event | 
|  | 4275 | firstWindow->consumeMotionDown(); | 
|  | 4276 | secondWindow->assertNoEvents(); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4277 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4278 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4279 | // Transfer touch to the second window | 
|  | 4280 | TransferFunction f = GetParam(); | 
|  | 4281 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 4282 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4283 | // The first window gets cancel and the second gets down | 
|  | 4284 | firstWindow->consumeMotionCancel(); | 
|  | 4285 | secondWindow->consumeMotionDown(); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4286 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4287 |  | 
|  | 4288 | // Send up event to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4289 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4290 | ADISPLAY_ID_DEFAULT)); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4291 | // The first  window gets no events and the second gets up | 
|  | 4292 | firstWindow->assertNoEvents(); | 
|  | 4293 | secondWindow->consumeMotionUp(); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4294 | wallpaper->assertNoEvents(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4295 | } | 
|  | 4296 |  | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4297 | /** | 
|  | 4298 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch | 
|  | 4299 | * from. When we have spy windows, there are several windows to choose from: either spy, or the | 
|  | 4300 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most | 
|  | 4301 | * natural to the user. | 
|  | 4302 | * In this test, we are sending a pointer to both spy window and first window. We then try to | 
|  | 4303 | * transfer touch to the second window. The dispatcher should identify the first window as the | 
|  | 4304 | * one that should lose the gesture, and therefore the action should be to move the gesture from | 
|  | 4305 | * the first window to the second. | 
|  | 4306 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test | 
|  | 4307 | * the other API, as well. | 
|  | 4308 | */ | 
|  | 4309 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { | 
|  | 4310 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4311 |  | 
|  | 4312 | // Create a couple of windows + a spy window | 
|  | 4313 | sp<FakeWindowHandle> spyWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4314 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4315 | spyWindow->setTrustedOverlay(true); | 
|  | 4316 | spyWindow->setSpy(true); | 
|  | 4317 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4318 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4319 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4320 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4321 |  | 
|  | 4322 | // Add the windows to the dispatcher | 
|  | 4323 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); | 
|  | 4324 |  | 
|  | 4325 | // Send down to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4326 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4327 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4328 | // Only the first window and spy should get the down event | 
|  | 4329 | spyWindow->consumeMotionDown(); | 
|  | 4330 | firstWindow->consumeMotionDown(); | 
|  | 4331 |  | 
|  | 4332 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window | 
|  | 4333 | // if f === 'transferTouch'. | 
|  | 4334 | TransferFunction f = GetParam(); | 
|  | 4335 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 4336 | ASSERT_TRUE(success); | 
|  | 4337 | // The first window gets cancel and the second gets down | 
|  | 4338 | firstWindow->consumeMotionCancel(); | 
|  | 4339 | secondWindow->consumeMotionDown(); | 
|  | 4340 |  | 
|  | 4341 | // Send up event to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4342 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4343 | ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4344 | // The first  window gets no events and the second+spy get up | 
|  | 4345 | firstWindow->assertNoEvents(); | 
|  | 4346 | spyWindow->consumeMotionUp(); | 
|  | 4347 | secondWindow->consumeMotionUp(); | 
|  | 4348 | } | 
|  | 4349 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4350 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4351 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4352 |  | 
|  | 4353 | PointF touchPoint = {10, 10}; | 
|  | 4354 |  | 
|  | 4355 | // Create a couple of windows | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4356 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4357 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 4358 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4359 | firstWindow->setPreventSplitting(true); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4360 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4361 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 4362 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4363 | secondWindow->setPreventSplitting(true); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4364 |  | 
|  | 4365 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4366 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4367 |  | 
|  | 4368 | // Send down to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4369 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4370 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4371 | {touchPoint})); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4372 | // Only the first window should get the down event | 
|  | 4373 | firstWindow->consumeMotionDown(); | 
|  | 4374 | secondWindow->assertNoEvents(); | 
|  | 4375 |  | 
|  | 4376 | // Send pointer down to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4377 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4378 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4379 | // Only the first window should get the pointer down event | 
|  | 4380 | firstWindow->consumeMotionPointerDown(1); | 
|  | 4381 | secondWindow->assertNoEvents(); | 
|  | 4382 |  | 
|  | 4383 | // Transfer touch focus to the second window | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4384 | TransferFunction f = GetParam(); | 
|  | 4385 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 4386 | ASSERT_TRUE(success); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4387 | // The first window gets cancel and the second gets down and pointer down | 
|  | 4388 | firstWindow->consumeMotionCancel(); | 
|  | 4389 | secondWindow->consumeMotionDown(); | 
|  | 4390 | secondWindow->consumeMotionPointerDown(1); | 
|  | 4391 |  | 
|  | 4392 | // Send pointer up to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4393 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4394 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4395 | // The first window gets nothing and the second gets pointer up | 
|  | 4396 | firstWindow->assertNoEvents(); | 
|  | 4397 | secondWindow->consumeMotionPointerUp(1); | 
|  | 4398 |  | 
|  | 4399 | // Send up event to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4400 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4401 | ADISPLAY_ID_DEFAULT)); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4402 | // The first window gets nothing and the second gets up | 
|  | 4403 | firstWindow->assertNoEvents(); | 
|  | 4404 | secondWindow->consumeMotionUp(); | 
|  | 4405 | } | 
|  | 4406 |  | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4407 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { | 
|  | 4408 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4409 |  | 
|  | 4410 | // Create a couple of windows | 
|  | 4411 | sp<FakeWindowHandle> firstWindow = | 
|  | 4412 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 4413 | ADISPLAY_ID_DEFAULT); | 
|  | 4414 | firstWindow->setDupTouchToWallpaper(true); | 
|  | 4415 | sp<FakeWindowHandle> secondWindow = | 
|  | 4416 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 4417 | ADISPLAY_ID_DEFAULT); | 
|  | 4418 | secondWindow->setDupTouchToWallpaper(true); | 
|  | 4419 |  | 
|  | 4420 | sp<FakeWindowHandle> wallpaper1 = | 
|  | 4421 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); | 
|  | 4422 | wallpaper1->setIsWallpaper(true); | 
|  | 4423 |  | 
|  | 4424 | sp<FakeWindowHandle> wallpaper2 = | 
|  | 4425 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); | 
|  | 4426 | wallpaper2->setIsWallpaper(true); | 
|  | 4427 | // Add the windows to the dispatcher | 
|  | 4428 | mDispatcher->setInputWindows( | 
|  | 4429 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); | 
|  | 4430 |  | 
|  | 4431 | // Send down to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4432 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4433 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4434 |  | 
|  | 4435 | // Only the first window should get the down event | 
|  | 4436 | firstWindow->consumeMotionDown(); | 
|  | 4437 | secondWindow->assertNoEvents(); | 
|  | 4438 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 4439 | wallpaper2->assertNoEvents(); | 
|  | 4440 |  | 
|  | 4441 | // Transfer touch focus to the second window | 
|  | 4442 | TransferFunction f = GetParam(); | 
|  | 4443 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); | 
|  | 4444 | ASSERT_TRUE(success); | 
|  | 4445 |  | 
|  | 4446 | // The first window gets cancel and the second gets down | 
|  | 4447 | firstWindow->consumeMotionCancel(); | 
|  | 4448 | secondWindow->consumeMotionDown(); | 
|  | 4449 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 4450 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 4451 |  | 
|  | 4452 | // Send up event to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4453 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4454 | ADISPLAY_ID_DEFAULT)); | 
| Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4455 | // The first  window gets no events and the second gets up | 
|  | 4456 | firstWindow->assertNoEvents(); | 
|  | 4457 | secondWindow->consumeMotionUp(); | 
|  | 4458 | wallpaper1->assertNoEvents(); | 
|  | 4459 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); | 
|  | 4460 | } | 
|  | 4461 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4462 | // For the cases of single pointer touch and two pointers non-split touch, the api's | 
|  | 4463 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ | 
|  | 4464 | // for the case where there are multiple pointers split across several windows. | 
|  | 4465 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, | 
|  | 4466 | ::testing::Values( | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4467 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, | 
|  | 4468 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4469 | return dispatcher->transferTouch(destChannelToken, | 
|  | 4470 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4471 | }, | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4472 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, | 
|  | 4473 | sp<IBinder> from, sp<IBinder> to) { | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4474 | return dispatcher->transferTouchFocus(from, to, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4475 | /*isDragAndDrop=*/false); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4476 | })); | 
|  | 4477 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4478 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4479 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4480 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4481 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4482 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 4483 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4484 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4485 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4486 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4487 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 4488 | ADISPLAY_ID_DEFAULT); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4489 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4490 |  | 
|  | 4491 | // Add the windows to the dispatcher | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4492 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4493 |  | 
|  | 4494 | PointF pointInFirst = {300, 200}; | 
|  | 4495 | PointF pointInSecond = {300, 600}; | 
|  | 4496 |  | 
|  | 4497 | // Send down to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4498 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4499 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4500 | {pointInFirst})); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4501 | // Only the first window should get the down event | 
|  | 4502 | firstWindow->consumeMotionDown(); | 
|  | 4503 | secondWindow->assertNoEvents(); | 
|  | 4504 |  | 
|  | 4505 | // Send down to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4506 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4507 | ADISPLAY_ID_DEFAULT, | 
|  | 4508 | {pointInFirst, pointInSecond})); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4509 | // The first window gets a move and the second a down | 
|  | 4510 | firstWindow->consumeMotionMove(); | 
|  | 4511 | secondWindow->consumeMotionDown(); | 
|  | 4512 |  | 
|  | 4513 | // Transfer touch focus to the second window | 
|  | 4514 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); | 
|  | 4515 | // The first window gets cancel and the new gets pointer down (it already saw down) | 
|  | 4516 | firstWindow->consumeMotionCancel(); | 
|  | 4517 | secondWindow->consumeMotionPointerDown(1); | 
|  | 4518 |  | 
|  | 4519 | // Send pointer up to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4520 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4521 | ADISPLAY_ID_DEFAULT, | 
|  | 4522 | {pointInFirst, pointInSecond})); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4523 | // The first window gets nothing and the second gets pointer up | 
|  | 4524 | firstWindow->assertNoEvents(); | 
|  | 4525 | secondWindow->consumeMotionPointerUp(1); | 
|  | 4526 |  | 
|  | 4527 | // Send up event to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4528 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4529 | ADISPLAY_ID_DEFAULT)); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4530 | // The first window gets nothing and the second gets up | 
|  | 4531 | firstWindow->assertNoEvents(); | 
|  | 4532 | secondWindow->consumeMotionUp(); | 
|  | 4533 | } | 
|  | 4534 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4535 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. | 
|  | 4536 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving | 
|  | 4537 | // touch is not supported, so the touch should continue on those windows and the transferred-to | 
|  | 4538 | // window should get nothing. | 
|  | 4539 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { | 
|  | 4540 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4541 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4542 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4543 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 4544 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4545 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4546 |  | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4547 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4548 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 4549 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4550 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4551 |  | 
|  | 4552 | // Add the windows to the dispatcher | 
|  | 4553 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 4554 |  | 
|  | 4555 | PointF pointInFirst = {300, 200}; | 
|  | 4556 | PointF pointInSecond = {300, 600}; | 
|  | 4557 |  | 
|  | 4558 | // Send down to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4559 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4560 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4561 | {pointInFirst})); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4562 | // Only the first window should get the down event | 
|  | 4563 | firstWindow->consumeMotionDown(); | 
|  | 4564 | secondWindow->assertNoEvents(); | 
|  | 4565 |  | 
|  | 4566 | // Send down to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4567 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4568 | ADISPLAY_ID_DEFAULT, | 
|  | 4569 | {pointInFirst, pointInSecond})); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4570 | // The first window gets a move and the second a down | 
|  | 4571 | firstWindow->consumeMotionMove(); | 
|  | 4572 | secondWindow->consumeMotionDown(); | 
|  | 4573 |  | 
|  | 4574 | // Transfer touch focus to the second window | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4575 | const bool transferred = | 
|  | 4576 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4577 | // The 'transferTouch' call should not succeed, because there are 2 touched windows | 
|  | 4578 | ASSERT_FALSE(transferred); | 
|  | 4579 | firstWindow->assertNoEvents(); | 
|  | 4580 | secondWindow->assertNoEvents(); | 
|  | 4581 |  | 
|  | 4582 | // The rest of the dispatch should proceed as normal | 
|  | 4583 | // Send pointer up to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4584 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4585 | ADISPLAY_ID_DEFAULT, | 
|  | 4586 | {pointInFirst, pointInSecond})); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4587 | // The first window gets MOVE and the second gets pointer up | 
|  | 4588 | firstWindow->consumeMotionMove(); | 
|  | 4589 | secondWindow->consumeMotionUp(); | 
|  | 4590 |  | 
|  | 4591 | // Send up event to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4592 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4593 | ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4594 | // The first window gets nothing and the second gets up | 
|  | 4595 | firstWindow->consumeMotionUp(); | 
|  | 4596 | secondWindow->assertNoEvents(); | 
|  | 4597 | } | 
|  | 4598 |  | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4599 | // This case will create two windows and one mirrored window on the default display and mirror | 
|  | 4600 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put | 
|  | 4601 | // the windows info of second display before default display. | 
|  | 4602 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { | 
|  | 4603 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4604 | sp<FakeWindowHandle> firstWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4605 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4606 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4607 | sp<FakeWindowHandle> secondWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4608 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4609 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4610 |  | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4611 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4612 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4613 |  | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4614 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4615 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4616 |  | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4617 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4618 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4619 |  | 
|  | 4620 | // Update window info, let it find window handle of second display first. | 
|  | 4621 | mDispatcher->setInputWindows( | 
|  | 4622 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, | 
|  | 4623 | {ADISPLAY_ID_DEFAULT, | 
|  | 4624 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); | 
|  | 4625 |  | 
|  | 4626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4627 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4628 | {50, 50})) | 
|  | 4629 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4630 |  | 
|  | 4631 | // Window should receive motion event. | 
|  | 4632 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 4633 |  | 
|  | 4634 | // Transfer touch focus | 
|  | 4635 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), | 
|  | 4636 | secondWindowInPrimary->getToken())); | 
|  | 4637 | // The first window gets cancel. | 
|  | 4638 | firstWindowInPrimary->consumeMotionCancel(); | 
|  | 4639 | secondWindowInPrimary->consumeMotionDown(); | 
|  | 4640 |  | 
|  | 4641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4642 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4643 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 4644 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4645 | firstWindowInPrimary->assertNoEvents(); | 
|  | 4646 | secondWindowInPrimary->consumeMotionMove(); | 
|  | 4647 |  | 
|  | 4648 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4649 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4650 | {150, 50})) | 
|  | 4651 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4652 | firstWindowInPrimary->assertNoEvents(); | 
|  | 4653 | secondWindowInPrimary->consumeMotionUp(); | 
|  | 4654 | } | 
|  | 4655 |  | 
|  | 4656 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use | 
|  | 4657 | // 'transferTouch' api. | 
|  | 4658 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { | 
|  | 4659 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4660 | sp<FakeWindowHandle> firstWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4661 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4662 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4663 | sp<FakeWindowHandle> secondWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4664 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4665 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4666 |  | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4667 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4668 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4669 |  | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4670 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4671 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4672 |  | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4673 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4674 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4675 |  | 
|  | 4676 | // Update window info, let it find window handle of second display first. | 
|  | 4677 | mDispatcher->setInputWindows( | 
|  | 4678 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, | 
|  | 4679 | {ADISPLAY_ID_DEFAULT, | 
|  | 4680 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); | 
|  | 4681 |  | 
|  | 4682 | // Touch on second display. | 
|  | 4683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4684 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) | 
|  | 4685 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4686 |  | 
|  | 4687 | // Window should receive motion event. | 
|  | 4688 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 4689 |  | 
|  | 4690 | // Transfer touch focus | 
| Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4691 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); | 
| Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4692 |  | 
|  | 4693 | // The first window gets cancel. | 
|  | 4694 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); | 
|  | 4695 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 4696 |  | 
|  | 4697 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4698 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4699 | SECOND_DISPLAY_ID, {150, 50})) | 
|  | 4700 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4701 | firstWindowInPrimary->assertNoEvents(); | 
|  | 4702 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); | 
|  | 4703 |  | 
|  | 4704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4705 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) | 
|  | 4706 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4707 | firstWindowInPrimary->assertNoEvents(); | 
|  | 4708 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); | 
|  | 4709 | } | 
|  | 4710 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4711 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4712 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4713 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 4714 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4715 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4716 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4717 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4718 | setFocusedWindow(window); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4719 |  | 
|  | 4720 | window->consumeFocusEvent(true); | 
|  | 4721 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4722 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4723 |  | 
|  | 4724 | // Window should receive key down event. | 
|  | 4725 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4726 |  | 
|  | 4727 | // Should have poked user activity | 
| Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4728 | mDispatcher->waitForIdle(); | 
| Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4729 | mFakePolicy->assertUserActivityPoked(); | 
|  | 4730 | } | 
|  | 4731 |  | 
|  | 4732 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { | 
|  | 4733 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4734 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 4735 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 4736 |  | 
|  | 4737 | window->setDisableUserActivity(true); | 
|  | 4738 | window->setFocusable(true); | 
|  | 4739 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 4740 | setFocusedWindow(window); | 
|  | 4741 |  | 
|  | 4742 | window->consumeFocusEvent(true); | 
|  | 4743 |  | 
|  | 4744 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
|  | 4745 |  | 
|  | 4746 | // Window should receive key down event. | 
|  | 4747 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 4748 |  | 
|  | 4749 | // Should have poked user activity | 
| Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4750 | mDispatcher->waitForIdle(); | 
| Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4751 | mFakePolicy->assertUserActivityNotPoked(); | 
|  | 4752 | } | 
|  | 4753 |  | 
|  | 4754 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { | 
|  | 4755 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4756 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 4757 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 4758 |  | 
|  | 4759 | window->setFocusable(true); | 
|  | 4760 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 4761 | setFocusedWindow(window); | 
|  | 4762 |  | 
|  | 4763 | window->consumeFocusEvent(true); | 
|  | 4764 |  | 
|  | 4765 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
|  | 4766 | mDispatcher->waitForIdle(); | 
|  | 4767 |  | 
|  | 4768 | // System key is not passed down | 
|  | 4769 | window->assertNoEvents(); | 
|  | 4770 |  | 
|  | 4771 | // Should have poked user activity | 
|  | 4772 | mFakePolicy->assertUserActivityPoked(); | 
|  | 4773 | } | 
|  | 4774 |  | 
|  | 4775 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { | 
|  | 4776 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4777 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 4778 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 4779 |  | 
|  | 4780 | window->setFocusable(true); | 
|  | 4781 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 4782 | setFocusedWindow(window); | 
|  | 4783 |  | 
|  | 4784 | window->consumeFocusEvent(true); | 
|  | 4785 |  | 
|  | 4786 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
|  | 4787 | mDispatcher->waitForIdle(); | 
|  | 4788 |  | 
|  | 4789 | // System key is not passed down | 
|  | 4790 | window->assertNoEvents(); | 
|  | 4791 |  | 
|  | 4792 | // Should have poked user activity | 
|  | 4793 | mFakePolicy->assertUserActivityPoked(); | 
|  | 4794 | } | 
|  | 4795 |  | 
|  | 4796 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { | 
|  | 4797 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4798 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 4799 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 4800 |  | 
|  | 4801 | window->setDisableUserActivity(true); | 
|  | 4802 | window->setFocusable(true); | 
|  | 4803 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 4804 | setFocusedWindow(window); | 
|  | 4805 |  | 
|  | 4806 | window->consumeFocusEvent(true); | 
|  | 4807 |  | 
|  | 4808 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
|  | 4809 | mDispatcher->waitForIdle(); | 
|  | 4810 |  | 
|  | 4811 | // System key is not passed down | 
|  | 4812 | window->assertNoEvents(); | 
|  | 4813 |  | 
|  | 4814 | // Should have poked user activity | 
|  | 4815 | mFakePolicy->assertUserActivityPoked(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4816 | } | 
|  | 4817 |  | 
| Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4818 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { | 
|  | 4819 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4820 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 4821 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
|  | 4822 |  | 
|  | 4823 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 4824 |  | 
|  | 4825 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 4826 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4827 | ADISPLAY_ID_DEFAULT, {100, 100})) | 
|  | 4828 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 4829 |  | 
|  | 4830 | window->consumeMotionEvent( | 
|  | 4831 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); | 
|  | 4832 |  | 
|  | 4833 | // Should have poked user activity | 
| Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4834 | mDispatcher->waitForIdle(); | 
| Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4835 | mFakePolicy->assertUserActivityPoked(); | 
|  | 4836 | } | 
|  | 4837 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4838 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4839 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4840 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 4841 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4842 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4843 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4844 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4845 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4846 | mDispatcher->waitForIdle(); | 
|  | 4847 |  | 
|  | 4848 | window->assertNoEvents(); | 
|  | 4849 | } | 
|  | 4850 |  | 
|  | 4851 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys | 
|  | 4852 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4853 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4854 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 4855 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4856 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4857 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4858 |  | 
|  | 4859 | // Send key | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4860 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4861 | // Send motion | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4862 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4863 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4864 |  | 
|  | 4865 | // Window should receive only the motion event | 
|  | 4866 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 4867 | window->assertNoEvents(); // Key event or focus event will not be received | 
|  | 4868 | } | 
|  | 4869 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4870 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { | 
|  | 4871 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4872 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4873 | sp<FakeWindowHandle> firstWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4874 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", | 
|  | 4875 | ADISPLAY_ID_DEFAULT); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4876 | firstWindow->setFrame(Rect(0, 0, 600, 400)); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4877 |  | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4878 | sp<FakeWindowHandle> secondWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4879 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", | 
|  | 4880 | ADISPLAY_ID_DEFAULT); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4881 | secondWindow->setFrame(Rect(0, 400, 600, 800)); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4882 |  | 
|  | 4883 | // Add the windows to the dispatcher | 
|  | 4884 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); | 
|  | 4885 |  | 
|  | 4886 | PointF pointInFirst = {300, 200}; | 
|  | 4887 | PointF pointInSecond = {300, 600}; | 
|  | 4888 |  | 
|  | 4889 | // Send down to the first window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4890 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 4891 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 4892 | {pointInFirst})); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4893 | // Only the first window should get the down event | 
|  | 4894 | firstWindow->consumeMotionDown(); | 
|  | 4895 | secondWindow->assertNoEvents(); | 
|  | 4896 |  | 
|  | 4897 | // Send down to the second window | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4898 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4899 | ADISPLAY_ID_DEFAULT, | 
|  | 4900 | {pointInFirst, pointInSecond})); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4901 | // The first window gets a move and the second a down | 
|  | 4902 | firstWindow->consumeMotionMove(); | 
|  | 4903 | secondWindow->consumeMotionDown(); | 
|  | 4904 |  | 
|  | 4905 | // Send pointer cancel to the second window | 
|  | 4906 | NotifyMotionArgs pointerUpMotionArgs = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4907 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4908 | {pointInFirst, pointInSecond}); | 
|  | 4909 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4910 | mDispatcher->notifyMotion(pointerUpMotionArgs); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4911 | // The first window gets move and the second gets cancel. | 
|  | 4912 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 4913 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 4914 |  | 
|  | 4915 | // Send up event. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4916 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 4917 | ADISPLAY_ID_DEFAULT)); | 
| arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4918 | // The first window gets up and the second gets nothing. | 
|  | 4919 | firstWindow->consumeMotionUp(); | 
|  | 4920 | secondWindow->assertNoEvents(); | 
|  | 4921 | } | 
|  | 4922 |  | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4923 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { | 
|  | 4924 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 4925 |  | 
|  | 4926 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4927 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4928 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 4929 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; | 
|  | 4930 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; | 
|  | 4931 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; | 
|  | 4932 |  | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4933 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4934 | window->assertNoEvents(); | 
|  | 4935 | mDispatcher->waitForIdle(); | 
|  | 4936 | } | 
|  | 4937 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4938 | class FakeMonitorReceiver { | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4939 | public: | 
| Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4940 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4941 | int32_t displayId) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4942 | base::Result<std::unique_ptr<InputChannel>> channel = | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4943 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4944 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4945 | } | 
|  | 4946 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4947 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } | 
|  | 4948 |  | 
|  | 4949 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4950 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, | 
|  | 4951 | expectedFlags); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4952 | } | 
|  | 4953 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4954 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } | 
|  | 4955 |  | 
|  | 4956 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } | 
|  | 4957 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4958 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4959 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4960 | expectedDisplayId, expectedFlags); | 
|  | 4961 | } | 
|  | 4962 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4963 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4964 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4965 | expectedDisplayId, expectedFlags); | 
|  | 4966 | } | 
|  | 4967 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4968 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4969 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4970 | expectedDisplayId, expectedFlags); | 
|  | 4971 | } | 
|  | 4972 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4973 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4974 | mInputReceiver->consumeMotionEvent( | 
|  | 4975 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), | 
|  | 4976 | WithDisplayId(expectedDisplayId), | 
|  | 4977 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4978 | } | 
|  | 4979 |  | 
| Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4980 | void consumeMotionPointerDown(int32_t pointerIdx) { | 
|  | 4981 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | | 
|  | 4982 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4983 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4984 | /*expectedFlags=*/0); | 
| Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4985 | } | 
|  | 4986 |  | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4987 | MotionEvent* consumeMotion() { | 
|  | 4988 | InputEvent* event = mInputReceiver->consume(); | 
|  | 4989 | if (!event) { | 
|  | 4990 | ADD_FAILURE() << "No event was produced"; | 
|  | 4991 | return nullptr; | 
|  | 4992 | } | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4993 | if (event->getType() != InputEventType::MOTION) { | 
|  | 4994 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4995 | return nullptr; | 
|  | 4996 | } | 
|  | 4997 | return static_cast<MotionEvent*>(event); | 
|  | 4998 | } | 
|  | 4999 |  | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5000 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } | 
|  | 5001 |  | 
|  | 5002 | private: | 
|  | 5003 | std::unique_ptr<FakeInputReceiver> mInputReceiver; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5004 | }; | 
|  | 5005 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5006 | using InputDispatcherMonitorTest = InputDispatcherTest; | 
|  | 5007 |  | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5008 | /** | 
|  | 5009 | * Two entities that receive touch: A window, and a global monitor. | 
|  | 5010 | * The touch goes to the window, and then the window disappears. | 
|  | 5011 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled | 
|  | 5012 | * for the monitor, as well. | 
|  | 5013 | * 1. foregroundWindow | 
|  | 5014 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) | 
|  | 5015 | */ | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5016 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5017 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5018 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5019 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5020 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5021 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5022 |  | 
|  | 5023 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5025 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5026 | {100, 200})) | 
|  | 5027 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5028 |  | 
|  | 5029 | // Both the foreground window and the global monitor should receive the touch down | 
|  | 5030 | window->consumeMotionDown(); | 
|  | 5031 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5032 |  | 
|  | 5033 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5034 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5035 | ADISPLAY_ID_DEFAULT, {110, 200})) | 
|  | 5036 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5037 |  | 
|  | 5038 | window->consumeMotionMove(); | 
|  | 5039 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5040 |  | 
|  | 5041 | // Now the foreground window goes away | 
|  | 5042 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 5043 | window->consumeMotionCancel(); | 
|  | 5044 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet | 
|  | 5045 |  | 
|  | 5046 | // If more events come in, there will be no more foreground window to send them to. This will | 
|  | 5047 | // cause a cancel for the monitor, as well. | 
|  | 5048 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 5049 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5050 | ADISPLAY_ID_DEFAULT, {120, 200})) | 
|  | 5051 | << "Injection should fail because the window was removed"; | 
|  | 5052 | window->assertNoEvents(); | 
|  | 5053 | // Global monitor now gets the cancel | 
|  | 5054 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); | 
|  | 5055 | } | 
|  | 5056 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5057 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5058 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5059 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 5060 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5061 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5062 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5063 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5064 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5065 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5066 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5067 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5068 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5069 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5070 | } | 
|  | 5071 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5072 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { | 
|  | 5073 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5074 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5075 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5076 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 5077 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5078 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5079 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5080 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5081 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5082 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5083 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5084 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5085 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5086 | // Pilfer pointers from the monitor. | 
|  | 5087 | // This should not do anything and the window should continue to receive events. | 
|  | 5088 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5089 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5090 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5091 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5092 | ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5093 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5094 |  | 
|  | 5095 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 5096 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
| Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5097 | } | 
|  | 5098 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5099 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5100 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5101 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 5102 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5103 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5104 | window->setWindowOffset(20, 40); | 
|  | 5105 | window->setWindowTransform(0, 1, -1, 0); | 
|  | 5106 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5107 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5108 |  | 
|  | 5109 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5110 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 5111 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5112 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5113 | MotionEvent* event = monitor.consumeMotion(); | 
|  | 5114 | // Even though window has transform, gesture monitor must not. | 
|  | 5115 | ASSERT_EQ(ui::Transform(), event->getTransform()); | 
|  | 5116 | } | 
|  | 5117 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5118 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5119 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5120 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5121 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5122 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5123 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5124 | << "Injection should fail if there is a monitor, but no touchable window"; | 
|  | 5125 | monitor.assertNoEvents(); | 
| Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5126 | } | 
|  | 5127 |  | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5128 | TEST_F(InputDispatcherTest, TestMoveEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5129 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5130 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 5131 | "Fake Window", ADISPLAY_ID_DEFAULT); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5132 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5133 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5134 |  | 
|  | 5135 | NotifyMotionArgs motionArgs = | 
|  | 5136 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5137 | ADISPLAY_ID_DEFAULT); | 
|  | 5138 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5139 | mDispatcher->notifyMotion(motionArgs); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5140 | // Window should receive motion down event. | 
|  | 5141 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 5142 |  | 
|  | 5143 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; | 
| Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5144 | motionArgs.id += 1; | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5145 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 5146 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, | 
|  | 5147 | motionArgs.pointerCoords[0].getX() - 10); | 
|  | 5148 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5149 | mDispatcher->notifyMotion(motionArgs); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5150 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5151 | /*expectedFlags=*/0); | 
| chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5152 | } | 
|  | 5153 |  | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5154 | /** | 
|  | 5155 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to | 
|  | 5156 | * the device default right away. In the test scenario, we check both the default value, | 
|  | 5157 | * and the action of enabling / disabling. | 
|  | 5158 | */ | 
|  | 5159 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5160 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5161 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 5162 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5163 | const WindowInfo& windowInfo = *window->getInfo(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5164 |  | 
|  | 5165 | // Set focused application. | 
|  | 5166 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5167 | window->setFocusable(true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5168 |  | 
|  | 5169 | SCOPED_TRACE("Check default value of touch mode"); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5170 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5171 | setFocusedWindow(window); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5172 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5173 |  | 
|  | 5174 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5175 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5176 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5177 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5178 |  | 
|  | 5179 | SCOPED_TRACE("Disable touch mode"); | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5180 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5181 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5182 | window->consumeTouchModeEvent(false); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5183 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5184 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5185 | setFocusedWindow(window); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5186 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5187 |  | 
|  | 5188 | SCOPED_TRACE("Remove the window to trigger focus loss"); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5189 | window->setFocusable(false); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5190 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5191 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5192 |  | 
|  | 5193 | SCOPED_TRACE("Enable touch mode again"); | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5194 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5195 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5196 | window->consumeTouchModeEvent(true); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5197 | window->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5198 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5199 | setFocusedWindow(window); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5200 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5201 |  | 
|  | 5202 | window->assertNoEvents(); | 
|  | 5203 | } | 
|  | 5204 |  | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5205 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5206 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5207 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 5208 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5209 |  | 
|  | 5210 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5211 | window->setFocusable(true); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5212 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5213 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5214 | setFocusedWindow(window); | 
|  | 5215 |  | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5216 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5217 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5218 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
|  | 5219 | mDispatcher->notifyKey(keyArgs); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5220 |  | 
|  | 5221 | InputEvent* event = window->consume(); | 
|  | 5222 | ASSERT_NE(event, nullptr); | 
|  | 5223 |  | 
|  | 5224 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 5225 | ASSERT_NE(verified, nullptr); | 
|  | 5226 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); | 
|  | 5227 |  | 
|  | 5228 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); | 
|  | 5229 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); | 
|  | 5230 | ASSERT_EQ(keyArgs.source, verified->source); | 
|  | 5231 | ASSERT_EQ(keyArgs.displayId, verified->displayId); | 
|  | 5232 |  | 
|  | 5233 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); | 
|  | 5234 |  | 
|  | 5235 | ASSERT_EQ(keyArgs.action, verifiedKey.action); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5236 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); | 
| Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5237 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); | 
| Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5238 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); | 
|  | 5239 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); | 
|  | 5240 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); | 
|  | 5241 | ASSERT_EQ(0, verifiedKey.repeatCount); | 
|  | 5242 | } | 
|  | 5243 |  | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5244 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5245 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5246 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 5247 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5248 |  | 
|  | 5249 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5250 |  | 
| Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5251 | ui::Transform transform; | 
|  | 5252 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); | 
|  | 5253 |  | 
|  | 5254 | gui::DisplayInfo displayInfo; | 
|  | 5255 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; | 
|  | 5256 | displayInfo.transform = transform; | 
|  | 5257 |  | 
| Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5258 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5259 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5260 | const NotifyMotionArgs motionArgs = | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5261 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 5262 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5263 | mDispatcher->notifyMotion(motionArgs); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5264 |  | 
|  | 5265 | InputEvent* event = window->consume(); | 
|  | 5266 | ASSERT_NE(event, nullptr); | 
|  | 5267 |  | 
|  | 5268 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); | 
|  | 5269 | ASSERT_NE(verified, nullptr); | 
|  | 5270 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); | 
|  | 5271 |  | 
|  | 5272 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); | 
|  | 5273 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); | 
|  | 5274 | EXPECT_EQ(motionArgs.source, verified->source); | 
|  | 5275 | EXPECT_EQ(motionArgs.displayId, verified->displayId); | 
|  | 5276 |  | 
|  | 5277 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); | 
|  | 5278 |  | 
| Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5279 | const vec2 rawXY = | 
|  | 5280 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, | 
|  | 5281 | motionArgs.pointerCoords[0].getXYValue()); | 
|  | 5282 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); | 
|  | 5283 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5284 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5285 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); | 
| Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5286 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); | 
| Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5287 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); | 
|  | 5288 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); | 
|  | 5289 | } | 
|  | 5290 |  | 
| chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5291 | /** | 
|  | 5292 | * Ensure that separate calls to sign the same data are generating the same key. | 
|  | 5293 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance | 
|  | 5294 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky | 
|  | 5295 | * tests. | 
|  | 5296 | */ | 
|  | 5297 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { | 
|  | 5298 | KeyEvent event = getTestKeyEvent(); | 
|  | 5299 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 5300 |  | 
|  | 5301 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); | 
|  | 5302 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); | 
|  | 5303 | ASSERT_EQ(hmac1, hmac2); | 
|  | 5304 | } | 
|  | 5305 |  | 
|  | 5306 | /** | 
|  | 5307 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. | 
|  | 5308 | */ | 
|  | 5309 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { | 
|  | 5310 | KeyEvent event = getTestKeyEvent(); | 
|  | 5311 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); | 
|  | 5312 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); | 
|  | 5313 |  | 
|  | 5314 | verifiedEvent.deviceId += 1; | 
|  | 5315 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5316 |  | 
|  | 5317 | verifiedEvent.source += 1; | 
|  | 5318 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5319 |  | 
|  | 5320 | verifiedEvent.eventTimeNanos += 1; | 
|  | 5321 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5322 |  | 
|  | 5323 | verifiedEvent.displayId += 1; | 
|  | 5324 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5325 |  | 
|  | 5326 | verifiedEvent.action += 1; | 
|  | 5327 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5328 |  | 
|  | 5329 | verifiedEvent.downTimeNanos += 1; | 
|  | 5330 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5331 |  | 
|  | 5332 | verifiedEvent.flags += 1; | 
|  | 5333 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5334 |  | 
|  | 5335 | verifiedEvent.keyCode += 1; | 
|  | 5336 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5337 |  | 
|  | 5338 | verifiedEvent.scanCode += 1; | 
|  | 5339 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5340 |  | 
|  | 5341 | verifiedEvent.metaState += 1; | 
|  | 5342 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5343 |  | 
|  | 5344 | verifiedEvent.repeatCount += 1; | 
|  | 5345 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); | 
|  | 5346 | } | 
|  | 5347 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5348 | TEST_F(InputDispatcherTest, SetFocusedWindow) { | 
|  | 5349 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5350 | sp<FakeWindowHandle> windowTop = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5351 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5352 | sp<FakeWindowHandle> windowSecond = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5353 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5354 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5355 |  | 
|  | 5356 | // Top window is also focusable but is not granted focus. | 
|  | 5357 | windowTop->setFocusable(true); | 
|  | 5358 | windowSecond->setFocusable(true); | 
|  | 5359 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 5360 | setFocusedWindow(windowSecond); | 
|  | 5361 |  | 
|  | 5362 | windowSecond->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5363 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5364 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5365 |  | 
|  | 5366 | // Focused window should receive event. | 
|  | 5367 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 5368 | windowTop->assertNoEvents(); | 
|  | 5369 | } | 
|  | 5370 |  | 
|  | 5371 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { | 
|  | 5372 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5373 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5374 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5375 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5376 |  | 
|  | 5377 | window->setFocusable(true); | 
|  | 5378 | // Release channel for window is no longer valid. | 
|  | 5379 | window->releaseChannel(); | 
|  | 5380 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5381 | setFocusedWindow(window); | 
|  | 5382 |  | 
|  | 5383 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5384 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 5385 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5386 |  | 
|  | 5387 | // window channel is invalid, so it should not receive any input event. | 
|  | 5388 | window->assertNoEvents(); | 
|  | 5389 | } | 
|  | 5390 |  | 
|  | 5391 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { | 
|  | 5392 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5393 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5394 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5395 | window->setFocusable(false); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5396 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5397 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5398 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5399 | setFocusedWindow(window); | 
|  | 5400 |  | 
|  | 5401 | // Test inject a key down, should timeout. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5402 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 5403 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5404 |  | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5405 | // window is not focusable, so it should not receive any input event. | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5406 | window->assertNoEvents(); | 
|  | 5407 | } | 
|  | 5408 |  | 
|  | 5409 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { | 
|  | 5410 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5411 | sp<FakeWindowHandle> windowTop = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5412 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5413 | sp<FakeWindowHandle> windowSecond = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5414 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5415 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5416 |  | 
|  | 5417 | windowTop->setFocusable(true); | 
|  | 5418 | windowSecond->setFocusable(true); | 
|  | 5419 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
|  | 5420 | setFocusedWindow(windowTop); | 
|  | 5421 | windowTop->consumeFocusEvent(true); | 
|  | 5422 |  | 
| Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5423 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); | 
|  | 5424 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5425 | windowSecond->consumeFocusEvent(true); | 
|  | 5426 | windowTop->consumeFocusEvent(false); | 
|  | 5427 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5428 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5429 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5430 |  | 
|  | 5431 | // Focused window should receive event. | 
|  | 5432 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 5433 | } | 
|  | 5434 |  | 
| Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5435 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5436 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5437 | sp<FakeWindowHandle> windowTop = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5438 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5439 | sp<FakeWindowHandle> windowSecond = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5440 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5441 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5442 |  | 
|  | 5443 | windowTop->setFocusable(true); | 
| Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5444 | windowSecond->setFocusable(false); | 
|  | 5445 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5446 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); | 
| Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5447 | setFocusedWindow(windowTop); | 
|  | 5448 | windowTop->consumeFocusEvent(true); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5449 |  | 
| Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5450 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 5451 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5452 |  | 
|  | 5453 | // Event should be dropped. | 
| Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5454 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5455 | windowSecond->assertNoEvents(); | 
|  | 5456 | } | 
|  | 5457 |  | 
|  | 5458 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { | 
|  | 5459 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5460 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5461 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5462 | sp<FakeWindowHandle> previousFocusedWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5463 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", | 
|  | 5464 | ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5465 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5466 |  | 
|  | 5467 | window->setFocusable(true); | 
|  | 5468 | previousFocusedWindow->setFocusable(true); | 
|  | 5469 | window->setVisible(false); | 
|  | 5470 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); | 
|  | 5471 | setFocusedWindow(previousFocusedWindow); | 
|  | 5472 | previousFocusedWindow->consumeFocusEvent(true); | 
|  | 5473 |  | 
|  | 5474 | // Requesting focus on invisible window takes focus from currently focused window. | 
|  | 5475 | setFocusedWindow(window); | 
|  | 5476 | previousFocusedWindow->consumeFocusEvent(false); | 
|  | 5477 |  | 
|  | 5478 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5479 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5480 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
|  | 5481 | InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5482 |  | 
|  | 5483 | // Window does not get focus event or key down. | 
|  | 5484 | window->assertNoEvents(); | 
|  | 5485 |  | 
|  | 5486 | // Window becomes visible. | 
|  | 5487 | window->setVisible(true); | 
|  | 5488 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5489 |  | 
|  | 5490 | // Window receives focus event. | 
|  | 5491 | window->consumeFocusEvent(true); | 
|  | 5492 | // Focused window receives key down. | 
|  | 5493 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 5494 | } | 
|  | 5495 |  | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5496 | TEST_F(InputDispatcherTest, DisplayRemoved) { | 
|  | 5497 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5498 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5499 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5500 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5501 |  | 
|  | 5502 | // window is granted focus. | 
|  | 5503 | window->setFocusable(true); | 
|  | 5504 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5505 | setFocusedWindow(window); | 
|  | 5506 | window->consumeFocusEvent(true); | 
|  | 5507 |  | 
|  | 5508 | // When a display is removed window loses focus. | 
|  | 5509 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); | 
|  | 5510 | window->consumeFocusEvent(false); | 
|  | 5511 | } | 
|  | 5512 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5513 | /** | 
|  | 5514 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, | 
|  | 5515 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top | 
|  | 5516 | * of the 'slipperyEnterWindow'. | 
|  | 5517 | * | 
|  | 5518 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such | 
|  | 5519 | * a way so that the touched location is no longer covered by the top window. | 
|  | 5520 | * | 
|  | 5521 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now | 
|  | 5522 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had | 
|  | 5523 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive | 
|  | 5524 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting | 
|  | 5525 | * with ACTION_DOWN). | 
|  | 5526 | * Thus, the touch has been transferred from the top window into the bottom window, because the top | 
|  | 5527 | * window moved itself away from the touched location and had Flag::SLIPPERY. | 
|  | 5528 | * | 
|  | 5529 | * Even though the top window moved away from the touched location, it is still obscuring the bottom | 
|  | 5530 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ | 
|  | 5531 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. | 
|  | 5532 | * | 
|  | 5533 | * In this test, we ensure that the event received by the bottom window has | 
|  | 5534 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. | 
|  | 5535 | */ | 
|  | 5536 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5537 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5538 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5539 |  | 
|  | 5540 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5541 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 5542 |  | 
|  | 5543 | sp<FakeWindowHandle> slipperyExitWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5544 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5545 | slipperyExitWindow->setSlippery(true); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5546 | // Make sure this one overlaps the bottom window | 
|  | 5547 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); | 
|  | 5548 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom | 
|  | 5549 | // one. Windows with the same owner are not considered to be occluding each other. | 
|  | 5550 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); | 
|  | 5551 |  | 
|  | 5552 | sp<FakeWindowHandle> slipperyEnterWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5553 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5554 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 5555 |  | 
|  | 5556 | mDispatcher->setInputWindows( | 
|  | 5557 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 5558 |  | 
|  | 5559 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5560 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 5561 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5562 | {{50, 50}})); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5563 | slipperyExitWindow->consumeMotionDown(); | 
|  | 5564 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); | 
|  | 5565 | mDispatcher->setInputWindows( | 
|  | 5566 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); | 
|  | 5567 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5568 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, | 
|  | 5569 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 5570 | {{51, 51}})); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5571 |  | 
|  | 5572 | slipperyExitWindow->consumeMotionCancel(); | 
|  | 5573 |  | 
|  | 5574 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, | 
|  | 5575 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 5576 | } | 
|  | 5577 |  | 
| Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 5578 | /** | 
|  | 5579 | * Two windows, one on the left and another on the right. The left window is slippery. The right | 
|  | 5580 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the | 
|  | 5581 | * touch moves from the left window into the right window, the gesture should continue to go to the | 
|  | 5582 | * left window. Touch shouldn't slip because the right window can't receive touches. This test | 
|  | 5583 | * reproduces a crash. | 
|  | 5584 | */ | 
|  | 5585 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { | 
|  | 5586 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5587 |  | 
|  | 5588 | sp<FakeWindowHandle> leftSlipperyWindow = | 
|  | 5589 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 5590 | leftSlipperyWindow->setSlippery(true); | 
|  | 5591 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 5592 |  | 
|  | 5593 | sp<FakeWindowHandle> rightDropTouchesWindow = | 
|  | 5594 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 5595 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); | 
|  | 5596 | rightDropTouchesWindow->setDropInput(true); | 
|  | 5597 |  | 
|  | 5598 | mDispatcher->setInputWindows( | 
|  | 5599 | {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}}); | 
|  | 5600 |  | 
|  | 5601 | // Start touch in the left window | 
|  | 5602 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 5603 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) | 
|  | 5604 | .build()); | 
|  | 5605 | leftSlipperyWindow->consumeMotionDown(); | 
|  | 5606 |  | 
|  | 5607 | // And move it into the right window | 
|  | 5608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 5609 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) | 
|  | 5610 | .build()); | 
|  | 5611 |  | 
|  | 5612 | // Since the right window isn't eligible to receive input, touch does not slip. | 
|  | 5613 | // The left window continues to receive the gesture. | 
|  | 5614 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); | 
|  | 5615 | rightDropTouchesWindow->assertNoEvents(); | 
|  | 5616 | } | 
|  | 5617 |  | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5618 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5619 | using Uid = gui::Uid; | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5620 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5621 |  | 
|  | 5622 | sp<FakeWindowHandle> leftWindow = | 
|  | 5623 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); | 
|  | 5624 | leftWindow->setFrame(Rect(0, 0, 100, 100)); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5625 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5626 |  | 
|  | 5627 | sp<FakeWindowHandle> rightSpy = | 
|  | 5628 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); | 
|  | 5629 | rightSpy->setFrame(Rect(100, 0, 200, 100)); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5630 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5631 | rightSpy->setSpy(true); | 
|  | 5632 | rightSpy->setTrustedOverlay(true); | 
|  | 5633 |  | 
|  | 5634 | sp<FakeWindowHandle> rightWindow = | 
|  | 5635 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); | 
|  | 5636 | rightWindow->setFrame(Rect(100, 0, 200, 100)); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5637 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5638 |  | 
|  | 5639 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}}); | 
|  | 5640 |  | 
|  | 5641 | // Touch in the left window | 
|  | 5642 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 5643 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) | 
|  | 5644 | .build()); | 
|  | 5645 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); | 
|  | 5646 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5647 | ASSERT_NO_FATAL_FAILURE( | 
|  | 5648 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5649 |  | 
|  | 5650 | // Touch another finger over the right windows | 
|  | 5651 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 5652 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) | 
|  | 5653 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) | 
|  | 5654 | .build()); | 
|  | 5655 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); | 
|  | 5656 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); | 
|  | 5657 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); | 
|  | 5658 | mDispatcher->waitForIdle(); | 
|  | 5659 | ASSERT_NO_FATAL_FAILURE( | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5660 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, | 
|  | 5661 | {Uid{101}, Uid{102}, Uid{103}})); | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5662 |  | 
|  | 5663 | // Release finger over left window. The UP actions are not treated as device interaction. | 
|  | 5664 | // The windows that did not receive the UP pointer will receive MOVE events, but since this | 
|  | 5665 | // is part of the UP action, we do not treat this as device interaction. | 
|  | 5666 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 5667 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) | 
|  | 5668 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) | 
|  | 5669 | .build()); | 
|  | 5670 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); | 
|  | 5671 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); | 
|  | 5672 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); | 
|  | 5673 | mDispatcher->waitForIdle(); | 
|  | 5674 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); | 
|  | 5675 |  | 
|  | 5676 | // Move remaining finger | 
|  | 5677 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 5678 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) | 
|  | 5679 | .build()); | 
|  | 5680 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); | 
|  | 5681 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); | 
|  | 5682 | mDispatcher->waitForIdle(); | 
|  | 5683 | ASSERT_NO_FATAL_FAILURE( | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5684 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5685 |  | 
|  | 5686 | // Release all fingers | 
|  | 5687 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 5688 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) | 
|  | 5689 | .build()); | 
|  | 5690 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); | 
|  | 5691 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); | 
|  | 5692 | mDispatcher->waitForIdle(); | 
|  | 5693 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); | 
|  | 5694 | } | 
|  | 5695 |  | 
|  | 5696 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { | 
|  | 5697 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
|  | 5698 |  | 
|  | 5699 | sp<FakeWindowHandle> window = | 
|  | 5700 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); | 
|  | 5701 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5702 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5703 |  | 
|  | 5704 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 5705 | setFocusedWindow(window); | 
|  | 5706 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); | 
|  | 5707 |  | 
|  | 5708 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); | 
|  | 5709 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); | 
|  | 5710 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5711 | ASSERT_NO_FATAL_FAILURE( | 
|  | 5712 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); | 
| Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5713 |  | 
|  | 5714 | // The UP actions are not treated as device interaction. | 
|  | 5715 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); | 
|  | 5716 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); | 
|  | 5717 | mDispatcher->waitForIdle(); | 
|  | 5718 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); | 
|  | 5719 | } | 
|  | 5720 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5721 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { | 
|  | 5722 | protected: | 
|  | 5723 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms | 
|  | 5724 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000;   // 40 ms | 
|  | 5725 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5726 | std::shared_ptr<FakeApplicationHandle> mApp; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5727 | sp<FakeWindowHandle> mWindow; | 
|  | 5728 |  | 
|  | 5729 | virtual void SetUp() override { | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5730 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5731 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); | 
| Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5732 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); | 
| Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 5733 | mDispatcher->requestRefreshConfiguration(); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5734 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
|  | 5735 | ASSERT_EQ(OK, mDispatcher->start()); | 
|  | 5736 |  | 
|  | 5737 | setUpWindow(); | 
|  | 5738 | } | 
|  | 5739 |  | 
|  | 5740 | void setUpWindow() { | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5741 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5742 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5743 |  | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5744 | mWindow->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5745 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5746 | setFocusedWindow(mWindow); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5747 | mWindow->consumeFocusEvent(true); | 
|  | 5748 | } | 
|  | 5749 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5750 | void sendAndConsumeKeyDown(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5751 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5752 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5753 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5754 | mDispatcher->notifyKey(keyArgs); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5755 |  | 
|  | 5756 | // Window should receive key down event. | 
|  | 5757 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 5758 | } | 
|  | 5759 |  | 
|  | 5760 | void expectKeyRepeatOnce(int32_t repeatCount) { | 
|  | 5761 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); | 
|  | 5762 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 5763 | ASSERT_NE(nullptr, repeatEvent); | 
|  | 5764 |  | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5765 | ASSERT_EQ(InputEventType::KEY, repeatEvent->getType()); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5766 |  | 
|  | 5767 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); | 
|  | 5768 | uint32_t eventAction = repeatKeyEvent->getAction(); | 
|  | 5769 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); | 
|  | 5770 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); | 
|  | 5771 | } | 
|  | 5772 |  | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5773 | void sendAndConsumeKeyUp(int32_t deviceId) { | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5774 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5775 | keyArgs.deviceId = deviceId; | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5776 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5777 | mDispatcher->notifyKey(keyArgs); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5778 |  | 
|  | 5779 | // Window should receive key down event. | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5780 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5781 | /*expectedFlags=*/0); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5782 | } | 
|  | 5783 | }; | 
|  | 5784 |  | 
|  | 5785 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5786 | sendAndConsumeKeyDown(/*deviceId=*/1); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5787 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 5788 | expectKeyRepeatOnce(repeatCount); | 
|  | 5789 | } | 
|  | 5790 | } | 
|  | 5791 |  | 
|  | 5792 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5793 | sendAndConsumeKeyDown(/*deviceId=*/1); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5794 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 5795 | expectKeyRepeatOnce(repeatCount); | 
|  | 5796 | } | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5797 | sendAndConsumeKeyDown(/*deviceId=*/2); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5798 | /* repeatCount will start from 1 for deviceId 2 */ | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5799 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 5800 | expectKeyRepeatOnce(repeatCount); | 
|  | 5801 | } | 
|  | 5802 | } | 
|  | 5803 |  | 
|  | 5804 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5805 | sendAndConsumeKeyDown(/*deviceId=*/1); | 
|  | 5806 | expectKeyRepeatOnce(/*repeatCount=*/1); | 
|  | 5807 | sendAndConsumeKeyUp(/*deviceId=*/1); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5808 | mWindow->assertNoEvents(); | 
|  | 5809 | } | 
|  | 5810 |  | 
|  | 5811 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5812 | sendAndConsumeKeyDown(/*deviceId=*/1); | 
|  | 5813 | expectKeyRepeatOnce(/*repeatCount=*/1); | 
|  | 5814 | sendAndConsumeKeyDown(/*deviceId=*/2); | 
|  | 5815 | expectKeyRepeatOnce(/*repeatCount=*/1); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5816 | // Stale key up from device 1. | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5817 | sendAndConsumeKeyUp(/*deviceId=*/1); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5818 | // Device 2 is still down, keep repeating | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5819 | expectKeyRepeatOnce(/*repeatCount=*/2); | 
|  | 5820 | expectKeyRepeatOnce(/*repeatCount=*/3); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5821 | // Device 2 key up | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5822 | sendAndConsumeKeyUp(/*deviceId=*/2); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5823 | mWindow->assertNoEvents(); | 
|  | 5824 | } | 
|  | 5825 |  | 
|  | 5826 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5827 | sendAndConsumeKeyDown(/*deviceId=*/1); | 
|  | 5828 | expectKeyRepeatOnce(/*repeatCount=*/1); | 
|  | 5829 | sendAndConsumeKeyDown(/*deviceId=*/2); | 
|  | 5830 | expectKeyRepeatOnce(/*repeatCount=*/1); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5831 | // 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] | 5832 | sendAndConsumeKeyUp(/*deviceId=*/2); | 
| Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5833 | // Device 1 still holds key down, but the repeating was already stopped | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5834 | mWindow->assertNoEvents(); | 
|  | 5835 | } | 
|  | 5836 |  | 
| liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5837 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { | 
|  | 5838 | sendAndConsumeKeyDown(DEVICE_ID); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5839 | expectKeyRepeatOnce(/*repeatCount=*/1); | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5840 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); | 
| liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5841 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, | 
|  | 5842 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); | 
|  | 5843 | mWindow->assertNoEvents(); | 
|  | 5844 | } | 
|  | 5845 |  | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5846 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { | 
| Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5847 | GTEST_SKIP() << "Flaky test (b/270393106)"; | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5848 | sendAndConsumeKeyDown(/*deviceId=*/1); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5849 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 5850 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 5851 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 5852 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, | 
|  | 5853 | IdGenerator::getSource(repeatEvent->getId())); | 
|  | 5854 | } | 
|  | 5855 | } | 
|  | 5856 |  | 
|  | 5857 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { | 
| Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5858 | GTEST_SKIP() << "Flaky test (b/270393106)"; | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5859 | sendAndConsumeKeyDown(/*deviceId=*/1); | 
| Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5860 |  | 
|  | 5861 | std::unordered_set<int32_t> idSet; | 
|  | 5862 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { | 
|  | 5863 | InputEvent* repeatEvent = mWindow->consume(); | 
|  | 5864 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; | 
|  | 5865 | int32_t id = repeatEvent->getId(); | 
|  | 5866 | EXPECT_EQ(idSet.end(), idSet.find(id)); | 
|  | 5867 | idSet.insert(id); | 
|  | 5868 | } | 
|  | 5869 | } | 
|  | 5870 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5871 | /* Test InputDispatcher for MultiDisplay */ | 
|  | 5872 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { | 
|  | 5873 | public: | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5874 | virtual void SetUp() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5875 | InputDispatcherTest::SetUp(); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5876 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5877 | application1 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5878 | windowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5879 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5880 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5881 | // Set focus window for primary display, but focused display would be second one. | 
|  | 5882 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5883 | windowInPrimary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5884 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5885 | setFocusedWindow(windowInPrimary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5886 | windowInPrimary->consumeFocusEvent(true); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5887 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5888 | application2 = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5889 | windowInSecondary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5890 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5891 | // Set focus to second display window. | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5892 | // Set focus display to second one. | 
|  | 5893 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); | 
|  | 5894 | // Set focus window for second display. | 
|  | 5895 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5896 | windowInSecondary->setFocusable(true); | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5897 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5898 | setFocusedWindow(windowInSecondary); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5899 | windowInSecondary->consumeFocusEvent(true); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5900 | } | 
|  | 5901 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5902 | virtual void TearDown() override { | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5903 | InputDispatcherTest::TearDown(); | 
|  | 5904 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5905 | application1.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5906 | windowInPrimary.clear(); | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5907 | application2.reset(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5908 | windowInSecondary.clear(); | 
|  | 5909 | } | 
|  | 5910 |  | 
|  | 5911 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5912 | std::shared_ptr<FakeApplicationHandle> application1; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5913 | sp<FakeWindowHandle> windowInPrimary; | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5914 | std::shared_ptr<FakeApplicationHandle> application2; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5915 | sp<FakeWindowHandle> windowInSecondary; | 
|  | 5916 | }; | 
|  | 5917 |  | 
|  | 5918 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { | 
|  | 5919 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5920 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5921 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 5922 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5923 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5924 | windowInSecondary->assertNoEvents(); | 
|  | 5925 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5926 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5927 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5928 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 5929 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5930 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5931 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5932 | } | 
|  | 5933 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5934 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5935 | // Test inject a key down with display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5936 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5937 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5938 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5939 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5940 | windowInSecondary->assertNoEvents(); | 
|  | 5941 |  | 
|  | 5942 | // Test inject a key down without display id specified. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5944 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5945 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5946 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5947 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5948 | // Remove all windows in secondary display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5949 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5950 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5951 | // Old focus should receive a cancel event. | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5952 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5953 | AKEY_EVENT_FLAG_CANCELED); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5954 |  | 
|  | 5955 | // Test inject a key down, should timeout because of no target window. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5956 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5957 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5958 | windowInPrimary->assertNoEvents(); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5959 | windowInSecondary->consumeFocusEvent(false); | 
| Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5960 | windowInSecondary->assertNoEvents(); | 
|  | 5961 | } | 
|  | 5962 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5963 | // Test per-display input monitors for motion event. | 
|  | 5964 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5965 | FakeMonitorReceiver monitorInPrimary = | 
|  | 5966 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 5967 | FakeMonitorReceiver monitorInSecondary = | 
|  | 5968 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5969 |  | 
|  | 5970 | // Test touch down on primary display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5971 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5972 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 5973 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5974 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5975 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5976 | windowInSecondary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5977 | monitorInSecondary.assertNoEvents(); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5978 |  | 
|  | 5979 | // Test touch down on second display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5981 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 5982 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5983 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5984 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5985 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5986 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5987 |  | 
| Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 5988 | // Lift up the touch from the second display | 
|  | 5989 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5990 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 5991 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 5992 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); | 
|  | 5993 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); | 
|  | 5994 |  | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5995 | // Test inject a non-pointer motion event. | 
|  | 5996 | // If specific a display, it will dispatch to the focused window of particular display, | 
|  | 5997 | // or it will dispatch to the focused window of focused display. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5998 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 5999 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) | 
|  | 6000 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6001 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6002 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6003 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6004 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6005 | } | 
|  | 6006 |  | 
|  | 6007 | // Test per-display input monitors for key event. | 
|  | 6008 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6009 | // Input monitor per display. | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6010 | FakeMonitorReceiver monitorInPrimary = | 
|  | 6011 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 6012 | FakeMonitorReceiver monitorInSecondary = | 
|  | 6013 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6014 |  | 
|  | 6015 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 6017 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6018 | windowInPrimary->assertNoEvents(); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6019 | monitorInPrimary.assertNoEvents(); | 
| Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 6020 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); | 
| chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6021 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); | 
| Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 6022 | } | 
|  | 6023 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6024 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { | 
|  | 6025 | sp<FakeWindowHandle> secondWindowInPrimary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6026 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6027 | secondWindowInPrimary->setFocusable(true); | 
|  | 6028 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); | 
|  | 6029 | setFocusedWindow(secondWindowInPrimary); | 
|  | 6030 | windowInPrimary->consumeFocusEvent(false); | 
|  | 6031 | secondWindowInPrimary->consumeFocusEvent(true); | 
|  | 6032 |  | 
|  | 6033 | // Test inject a key down. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6034 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 6035 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6036 | windowInPrimary->assertNoEvents(); | 
|  | 6037 | windowInSecondary->assertNoEvents(); | 
|  | 6038 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 6039 | } | 
|  | 6040 |  | 
| Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6041 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { | 
|  | 6042 | FakeMonitorReceiver monitorInPrimary = | 
|  | 6043 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 6044 | FakeMonitorReceiver monitorInSecondary = | 
|  | 6045 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); | 
|  | 6046 |  | 
|  | 6047 | // Test touch down on primary display. | 
|  | 6048 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6049 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 6050 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6051 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6052 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6053 |  | 
|  | 6054 | // Test touch down on second display. | 
|  | 6055 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6056 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) | 
|  | 6057 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6058 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 6059 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); | 
|  | 6060 |  | 
|  | 6061 | // Trigger cancel touch. | 
|  | 6062 | mDispatcher->cancelCurrentTouch(); | 
|  | 6063 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); | 
|  | 6064 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); | 
|  | 6065 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); | 
|  | 6066 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); | 
|  | 6067 |  | 
|  | 6068 | // Test inject a move motion event, no window/monitor should receive the event. | 
|  | 6069 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 6070 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6071 | ADISPLAY_ID_DEFAULT, {110, 200})) | 
|  | 6072 | << "Inject motion event should return InputEventInjectionResult::FAILED"; | 
|  | 6073 | windowInPrimary->assertNoEvents(); | 
|  | 6074 | monitorInPrimary.assertNoEvents(); | 
|  | 6075 |  | 
|  | 6076 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 6077 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6078 | SECOND_DISPLAY_ID, {110, 200})) | 
|  | 6079 | << "Inject motion event should return InputEventInjectionResult::FAILED"; | 
|  | 6080 | windowInSecondary->assertNoEvents(); | 
|  | 6081 | monitorInSecondary.assertNoEvents(); | 
|  | 6082 | } | 
|  | 6083 |  | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6084 | class InputFilterTest : public InputDispatcherTest { | 
|  | 6085 | protected: | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6086 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, | 
|  | 6087 | const ui::Transform& transform = ui::Transform()) { | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6088 | NotifyMotionArgs motionArgs; | 
|  | 6089 |  | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6090 | motionArgs = | 
|  | 6091 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6092 | mDispatcher->notifyMotion(motionArgs); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6093 | motionArgs = | 
|  | 6094 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6095 | mDispatcher->notifyMotion(motionArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6096 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6097 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 6098 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6099 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6100 | } else { | 
|  | 6101 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 6102 | } | 
|  | 6103 | } | 
|  | 6104 |  | 
|  | 6105 | void testNotifyKey(bool expectToBeFiltered) { | 
|  | 6106 | NotifyKeyArgs keyArgs; | 
|  | 6107 |  | 
|  | 6108 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6109 | mDispatcher->notifyKey(keyArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6110 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6111 | mDispatcher->notifyKey(keyArgs); | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6112 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6113 |  | 
|  | 6114 | if (expectToBeFiltered) { | 
| Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 6115 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); | 
| Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6116 | } else { | 
|  | 6117 | mFakePolicy->assertFilterInputEventWasNotCalled(); | 
|  | 6118 | } | 
|  | 6119 | } | 
|  | 6120 | }; | 
|  | 6121 |  | 
|  | 6122 | // Test InputFilter for MotionEvent | 
|  | 6123 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { | 
|  | 6124 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. | 
|  | 6125 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 6126 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 6127 |  | 
|  | 6128 | // Enable InputFilter | 
|  | 6129 | mDispatcher->setInputFilterEnabled(true); | 
|  | 6130 | // Test touch on both primary and second display, and check if both events are filtered. | 
|  | 6131 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); | 
|  | 6132 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); | 
|  | 6133 |  | 
|  | 6134 | // Disable InputFilter | 
|  | 6135 | mDispatcher->setInputFilterEnabled(false); | 
|  | 6136 | // Test touch on both primary and second display, and check if both events aren't filtered. | 
|  | 6137 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); | 
|  | 6138 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); | 
|  | 6139 | } | 
|  | 6140 |  | 
|  | 6141 | // Test InputFilter for KeyEvent | 
|  | 6142 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { | 
|  | 6143 | // Since the InputFilter is disabled by default, check if key event aren't filtered. | 
|  | 6144 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 6145 |  | 
|  | 6146 | // Enable InputFilter | 
|  | 6147 | mDispatcher->setInputFilterEnabled(true); | 
|  | 6148 | // Send a key event, and check if it is filtered. | 
|  | 6149 | testNotifyKey(/*expectToBeFiltered*/ true); | 
|  | 6150 |  | 
|  | 6151 | // Disable InputFilter | 
|  | 6152 | mDispatcher->setInputFilterEnabled(false); | 
|  | 6153 | // Send a key event, and check if it isn't filtered. | 
|  | 6154 | testNotifyKey(/*expectToBeFiltered*/ false); | 
|  | 6155 | } | 
|  | 6156 |  | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6157 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the | 
|  | 6158 | // logical display coordinate space. | 
|  | 6159 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { | 
|  | 6160 | ui::Transform firstDisplayTransform; | 
|  | 6161 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); | 
|  | 6162 | ui::Transform secondDisplayTransform; | 
|  | 6163 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); | 
|  | 6164 |  | 
|  | 6165 | std::vector<gui::DisplayInfo> displayInfos(2); | 
|  | 6166 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; | 
|  | 6167 | displayInfos[0].transform = firstDisplayTransform; | 
|  | 6168 | displayInfos[1].displayId = SECOND_DISPLAY_ID; | 
|  | 6169 | displayInfos[1].transform = secondDisplayTransform; | 
|  | 6170 |  | 
| Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6171 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); | 
| Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6172 |  | 
|  | 6173 | // Enable InputFilter | 
|  | 6174 | mDispatcher->setInputFilterEnabled(true); | 
|  | 6175 |  | 
|  | 6176 | // Ensure the correct transforms are used for the displays. | 
|  | 6177 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); | 
|  | 6178 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); | 
|  | 6179 | } | 
|  | 6180 |  | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6181 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { | 
|  | 6182 | protected: | 
|  | 6183 | virtual void SetUp() override { | 
|  | 6184 | InputDispatcherTest::SetUp(); | 
|  | 6185 |  | 
|  | 6186 | /** | 
|  | 6187 | * We don't need to enable input filter to test the injected event policy, but we enabled it | 
|  | 6188 | * here to make the tests more realistic, since this policy only matters when inputfilter is | 
|  | 6189 | * on. | 
|  | 6190 | */ | 
|  | 6191 | mDispatcher->setInputFilterEnabled(true); | 
|  | 6192 |  | 
|  | 6193 | std::shared_ptr<InputApplicationHandle> application = | 
|  | 6194 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6195 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", | 
|  | 6196 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6197 |  | 
|  | 6198 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 6199 | mWindow->setFocusable(true); | 
|  | 6200 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 6201 | setFocusedWindow(mWindow); | 
|  | 6202 | mWindow->consumeFocusEvent(true); | 
|  | 6203 | } | 
|  | 6204 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6205 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 6206 | int32_t flags) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6207 | KeyEvent event; | 
|  | 6208 |  | 
|  | 6209 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 6210 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, | 
|  | 6211 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6212 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6213 | const int32_t additionalPolicyFlags = | 
|  | 6214 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; | 
|  | 6215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6216 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6217 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 6218 | policyFlags | additionalPolicyFlags)); | 
|  | 6219 |  | 
|  | 6220 | InputEvent* received = mWindow->consume(); | 
|  | 6221 | ASSERT_NE(nullptr, received); | 
|  | 6222 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6223 | ASSERT_EQ(received->getType(), InputEventType::KEY); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6224 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); | 
|  | 6225 | ASSERT_EQ(flags, keyEvent.getFlags()); | 
|  | 6226 | } | 
|  | 6227 |  | 
|  | 6228 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, | 
|  | 6229 | int32_t flags) { | 
|  | 6230 | MotionEvent event; | 
|  | 6231 | PointerProperties pointerProperties[1]; | 
|  | 6232 | PointerCoords pointerCoords[1]; | 
|  | 6233 | pointerProperties[0].clear(); | 
|  | 6234 | pointerProperties[0].id = 0; | 
|  | 6235 | pointerCoords[0].clear(); | 
|  | 6236 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); | 
|  | 6237 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); | 
|  | 6238 |  | 
|  | 6239 | ui::Transform identityTransform; | 
|  | 6240 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 6241 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6242 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, | 
|  | 6243 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, | 
|  | 6244 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 6245 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, | 
| Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 6246 | eventTime, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6247 | /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
|  | 6248 |  | 
|  | 6249 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; | 
|  | 6250 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6251 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6252 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, | 
|  | 6253 | policyFlags | additionalPolicyFlags)); | 
|  | 6254 |  | 
|  | 6255 | InputEvent* received = mWindow->consume(); | 
|  | 6256 | ASSERT_NE(nullptr, received); | 
|  | 6257 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6258 | ASSERT_EQ(received->getType(), InputEventType::MOTION); | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6259 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); | 
|  | 6260 | ASSERT_EQ(flags, motionEvent.getFlags()); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6261 | } | 
|  | 6262 |  | 
|  | 6263 | private: | 
|  | 6264 | sp<FakeWindowHandle> mWindow; | 
|  | 6265 | }; | 
|  | 6266 |  | 
|  | 6267 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6268 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input | 
|  | 6269 | // filter. Without it, the event will no different from a regularly injected event, and the | 
|  | 6270 | // injected device id will be overwritten. | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6271 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, | 
|  | 6272 | /*flags=*/0); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6273 | } | 
|  | 6274 |  | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6275 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6276 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6277 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6278 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
|  | 6279 | } | 
|  | 6280 |  | 
|  | 6281 | TEST_F(InputFilterInjectionPolicyTest, | 
|  | 6282 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { | 
|  | 6283 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6284 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, | 
| Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6285 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6286 | } | 
|  | 6287 |  | 
|  | 6288 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6289 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, | 
|  | 6290 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); | 
| Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6291 | } | 
|  | 6292 |  | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6293 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6294 | virtual void SetUp() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6295 | InputDispatcherTest::SetUp(); | 
|  | 6296 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6297 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 6298 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6299 | mUnfocusedWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6300 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6301 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6302 |  | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6303 | mFocusedWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6304 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6305 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6306 |  | 
|  | 6307 | // Set focused application. | 
|  | 6308 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6309 | mFocusedWindow->setFocusable(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6310 |  | 
|  | 6311 | // Expect one focus window exist in display. | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6312 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6313 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6314 | mFocusedWindow->consumeFocusEvent(true); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6315 | } | 
|  | 6316 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6317 | virtual void TearDown() override { | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6318 | InputDispatcherTest::TearDown(); | 
|  | 6319 |  | 
|  | 6320 | mUnfocusedWindow.clear(); | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6321 | mFocusedWindow.clear(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6322 | } | 
|  | 6323 |  | 
|  | 6324 | protected: | 
|  | 6325 | sp<FakeWindowHandle> mUnfocusedWindow; | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6326 | sp<FakeWindowHandle> mFocusedWindow; | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6327 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6328 | }; | 
|  | 6329 |  | 
|  | 6330 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 6331 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received | 
|  | 6332 | // the onPointerDownOutsideFocus callback. | 
|  | 6333 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6334 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6335 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6336 | {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6337 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6338 | mUnfocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6339 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6340 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6341 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); | 
|  | 6342 | } | 
|  | 6343 |  | 
|  | 6344 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action | 
|  | 6345 | // DOWN on the window that doesn't have focus. Ensure no window received the | 
|  | 6346 | // onPointerDownOutsideFocus callback. | 
|  | 6347 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6348 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6349 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6350 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6351 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6352 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6353 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6354 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6355 | } | 
|  | 6356 |  | 
|  | 6357 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't | 
|  | 6358 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. | 
|  | 6359 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6361 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6362 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6363 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6364 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6365 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6366 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6367 | } | 
|  | 6368 |  | 
|  | 6369 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action | 
|  | 6370 | // DOWN on the window that already has focus. Ensure no window received the | 
|  | 6371 | // onPointerDownOutsideFocus callback. | 
| Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6372 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6373 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6374 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6375 | FOCUSED_WINDOW_TOUCH_POINT)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6376 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6377 | mFocusedWindow->consumeMotionDown(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6378 |  | 
| Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6379 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6380 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
| chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6381 | } | 
|  | 6382 |  | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6383 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag | 
|  | 6384 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. | 
|  | 6385 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { | 
|  | 6386 | const MotionEvent event = | 
|  | 6387 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 6388 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 6389 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) | 
| Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6390 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) | 
|  | 6391 | .build(); | 
|  | 6392 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) | 
|  | 6393 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 6394 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); | 
|  | 6395 |  | 
|  | 6396 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6397 | mFakePolicy->assertOnPointerDownWasNotCalled(); | 
|  | 6398 | // Ensure that the unfocused window did not receive any FOCUS events. | 
|  | 6399 | mUnfocusedWindow->assertNoEvents(); | 
|  | 6400 | } | 
|  | 6401 |  | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6402 | // These tests ensures we can send touch events to a single client when there are multiple input | 
|  | 6403 | // windows that point to the same client token. | 
|  | 6404 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { | 
|  | 6405 | virtual void SetUp() override { | 
|  | 6406 | InputDispatcherTest::SetUp(); | 
|  | 6407 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6408 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 6409 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6410 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", | 
|  | 6411 | ADISPLAY_ID_DEFAULT); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6412 | mWindow1->setFrame(Rect(0, 0, 100, 100)); | 
|  | 6413 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6414 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", | 
|  | 6415 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6416 | mWindow2->setFrame(Rect(100, 100, 200, 200)); | 
|  | 6417 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6418 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6419 | } | 
|  | 6420 |  | 
|  | 6421 | protected: | 
|  | 6422 | sp<FakeWindowHandle> mWindow1; | 
|  | 6423 | sp<FakeWindowHandle> mWindow2; | 
|  | 6424 |  | 
|  | 6425 | // 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] | 6426 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { | 
| chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 6427 | vec2 vals = windowInfo->transform.transform(point.x, point.y); | 
|  | 6428 | return {vals.x, vals.y}; | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6429 | } | 
|  | 6430 |  | 
|  | 6431 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, | 
|  | 6432 | const std::vector<PointF>& points) { | 
| Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6433 | const std::string name = window->getName(); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6434 | InputEvent* event = window->consume(); | 
|  | 6435 |  | 
|  | 6436 | ASSERT_NE(nullptr, event) << name.c_str() | 
|  | 6437 | << ": consumer should have returned non-NULL event."; | 
|  | 6438 |  | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6439 | ASSERT_EQ(InputEventType::MOTION, event->getType()) | 
|  | 6440 | << name.c_str() << ": expected MotionEvent, got " << *event; | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6441 |  | 
|  | 6442 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); | 
| Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6443 | assertMotionAction(expectedAction, motionEvent.getAction()); | 
| Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 6444 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6445 |  | 
|  | 6446 | for (size_t i = 0; i < points.size(); i++) { | 
|  | 6447 | float expectedX = points[i].x; | 
|  | 6448 | float expectedY = points[i].y; | 
|  | 6449 |  | 
|  | 6450 | EXPECT_EQ(expectedX, motionEvent.getX(i)) | 
|  | 6451 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() | 
|  | 6452 | << ", got " << motionEvent.getX(i); | 
|  | 6453 | EXPECT_EQ(expectedY, motionEvent.getY(i)) | 
|  | 6454 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() | 
|  | 6455 | << ", got " << motionEvent.getY(i); | 
|  | 6456 | } | 
|  | 6457 | } | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6458 |  | 
| Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6459 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6460 | std::vector<PointF> expectedPoints) { | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6461 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6462 | ADISPLAY_ID_DEFAULT, touchedPoints)); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6463 |  | 
|  | 6464 | // Always consume from window1 since it's the window that has the InputReceiver | 
|  | 6465 | consumeMotionEvent(mWindow1, action, expectedPoints); | 
|  | 6466 | } | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6467 | }; | 
|  | 6468 |  | 
|  | 6469 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { | 
|  | 6470 | // Touch Window 1 | 
|  | 6471 | PointF touchedPoint = {10, 10}; | 
|  | 6472 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6473 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6474 |  | 
|  | 6475 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6476 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6477 |  | 
|  | 6478 | // Touch Window 2 | 
|  | 6479 | touchedPoint = {150, 150}; | 
|  | 6480 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6481 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6482 | } | 
|  | 6483 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6484 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { | 
|  | 6485 | // Set scale value for window2 | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6486 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 6487 |  | 
|  | 6488 | // Touch Window 1 | 
|  | 6489 | PointF touchedPoint = {10, 10}; | 
|  | 6490 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6491 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6492 | // Release touch on Window 1 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6493 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6494 |  | 
|  | 6495 | // Touch Window 2 | 
|  | 6496 | touchedPoint = {150, 150}; | 
|  | 6497 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6498 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
|  | 6499 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6500 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6501 | // Update the transform so rotation is set | 
|  | 6502 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 6503 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); | 
|  | 6504 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); | 
| chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6505 | } | 
|  | 6506 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6507 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6508 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 6509 |  | 
|  | 6510 | // Touch Window 1 | 
|  | 6511 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 6512 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6513 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6514 |  | 
|  | 6515 | // Touch Window 2 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6516 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 6517 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6518 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6519 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6520 | // Release Window 2 | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6521 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6522 | expectedPoints.pop_back(); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6523 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6524 | // Update the transform so rotation is set for Window 2 | 
|  | 6525 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 6526 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6527 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6528 | } | 
|  | 6529 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6530 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6531 | mWindow2->setWindowScale(0.5f, 0.5f); | 
|  | 6532 |  | 
|  | 6533 | // Touch Window 1 | 
|  | 6534 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 6535 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6536 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6537 |  | 
|  | 6538 | // Touch Window 2 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6539 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 6540 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6541 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6542 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6543 |  | 
|  | 6544 | // Move both windows | 
|  | 6545 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 6546 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 6547 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 6548 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6549 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6550 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6551 | // Release Window 2 | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6552 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6553 | expectedPoints.pop_back(); | 
|  | 6554 |  | 
|  | 6555 | // Touch Window 2 | 
|  | 6556 | mWindow2->setWindowTransform(0, -1, 1, 0); | 
|  | 6557 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6558 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6559 |  | 
|  | 6560 | // Move both windows | 
|  | 6561 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 6562 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 6563 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 6564 |  | 
|  | 6565 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6566 | } | 
|  | 6567 |  | 
|  | 6568 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { | 
|  | 6569 | mWindow1->setWindowScale(0.5f, 0.5f); | 
|  | 6570 |  | 
|  | 6571 | // Touch Window 1 | 
|  | 6572 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; | 
|  | 6573 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6574 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6575 |  | 
|  | 6576 | // Touch Window 2 | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6577 | touchedPoints.push_back(PointF{150, 150}); | 
|  | 6578 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6579 |  | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6580 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6581 |  | 
|  | 6582 | // Move both windows | 
|  | 6583 | touchedPoints = {{20, 20}, {175, 175}}; | 
|  | 6584 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), | 
|  | 6585 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; | 
|  | 6586 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6587 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); | 
| Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6588 | } | 
|  | 6589 |  | 
| Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 6590 | /** | 
|  | 6591 | * When one of the windows is slippery, the touch should not slip into the other window with the | 
|  | 6592 | * same input channel. | 
|  | 6593 | */ | 
|  | 6594 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { | 
|  | 6595 | mWindow1->setSlippery(true); | 
|  | 6596 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); | 
|  | 6597 |  | 
|  | 6598 | // Touch down in window 1 | 
|  | 6599 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6600 | ADISPLAY_ID_DEFAULT, {{50, 50}})); | 
|  | 6601 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); | 
|  | 6602 |  | 
|  | 6603 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. | 
|  | 6604 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN | 
|  | 6605 | // getting generated. | 
|  | 6606 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6607 | ADISPLAY_ID_DEFAULT, {{150, 150}})); | 
|  | 6608 |  | 
|  | 6609 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); | 
|  | 6610 | } | 
|  | 6611 |  | 
| Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 6612 | /** | 
|  | 6613 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and | 
|  | 6614 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window | 
|  | 6615 | * that the pointer is hovering over may have a different transform. | 
|  | 6616 | */ | 
|  | 6617 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { | 
|  | 6618 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); | 
|  | 6619 |  | 
|  | 6620 | // Start hover in window 1 | 
|  | 6621 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6622 | ADISPLAY_ID_DEFAULT, {{50, 50}})); | 
|  | 6623 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, | 
|  | 6624 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); | 
|  | 6625 |  | 
|  | 6626 | // Move hover to window 2. | 
|  | 6627 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6628 | ADISPLAY_ID_DEFAULT, {{150, 150}})); | 
|  | 6629 |  | 
|  | 6630 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); | 
|  | 6631 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, | 
|  | 6632 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); | 
|  | 6633 | } | 
|  | 6634 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6635 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { | 
|  | 6636 | virtual void SetUp() override { | 
|  | 6637 | InputDispatcherTest::SetUp(); | 
|  | 6638 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6639 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6640 | mApplication->setDispatchingTimeout(20ms); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6641 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", | 
|  | 6642 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6643 | mWindow->setFrame(Rect(0, 0, 30, 30)); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6644 | mWindow->setDispatchingTimeout(30ms); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6645 | mWindow->setFocusable(true); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6646 |  | 
|  | 6647 | // Set focused application. | 
|  | 6648 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
|  | 6649 |  | 
|  | 6650 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6651 | setFocusedWindow(mWindow); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6652 | mWindow->consumeFocusEvent(true); | 
|  | 6653 | } | 
|  | 6654 |  | 
|  | 6655 | virtual void TearDown() override { | 
|  | 6656 | InputDispatcherTest::TearDown(); | 
|  | 6657 | mWindow.clear(); | 
|  | 6658 | } | 
|  | 6659 |  | 
|  | 6660 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6661 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6662 | sp<FakeWindowHandle> mWindow; | 
|  | 6663 | static constexpr PointF WINDOW_LOCATION = {20, 20}; | 
|  | 6664 |  | 
|  | 6665 | void tapOnWindow() { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6667 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6668 | WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6670 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6671 | WINDOW_LOCATION)); | 
|  | 6672 | } | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6673 |  | 
|  | 6674 | sp<FakeWindowHandle> addSpyWindow() { | 
|  | 6675 | sp<FakeWindowHandle> spy = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6676 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6677 | spy->setTrustedOverlay(true); | 
|  | 6678 | spy->setFocusable(false); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6679 | spy->setSpy(true); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6680 | spy->setDispatchingTimeout(30ms); | 
|  | 6681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); | 
|  | 6682 | return spy; | 
|  | 6683 | } | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6684 | }; | 
|  | 6685 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6686 | // Send a tap and respond, which should not cause an ANR. | 
|  | 6687 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { | 
|  | 6688 | tapOnWindow(); | 
|  | 6689 | mWindow->consumeMotionDown(); | 
|  | 6690 | mWindow->consumeMotionUp(); | 
|  | 6691 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6692 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 6693 | } | 
|  | 6694 |  | 
|  | 6695 | // Send a regular key and respond, which should not cause an ANR. | 
|  | 6696 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6697 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6698 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 6699 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6700 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 6701 | } | 
|  | 6702 |  | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6703 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { | 
|  | 6704 | mWindow->setFocusable(false); | 
|  | 6705 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 6706 | mWindow->consumeFocusEvent(false); | 
|  | 6707 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6708 | InputEventInjectionResult result = | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6709 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
|  | 6710 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, | 
|  | 6711 | /*allowKeyRepeat=*/false); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6713 | // Key will not go to window because we have no focused window. | 
|  | 6714 | // The 'no focused window' ANR timer should start instead. | 
|  | 6715 |  | 
|  | 6716 | // Now, the focused application goes away. | 
|  | 6717 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); | 
|  | 6718 | // The key should get dropped and there should be no ANR. | 
|  | 6719 |  | 
|  | 6720 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6721 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 6722 | } | 
|  | 6723 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6724 | // 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] | 6725 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. | 
|  | 6726 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6727 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6729 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6730 | WINDOW_LOCATION)); | 
|  | 6731 |  | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6732 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN | 
|  | 6733 | ASSERT_TRUE(sequenceNum); | 
|  | 6734 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6735 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6736 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6737 | mWindow->finishEvent(*sequenceNum); | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6738 | mWindow->consumeMotionEvent( | 
|  | 6739 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6740 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6741 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6742 | } | 
|  | 6743 |  | 
|  | 6744 | // Send a key to the app and have the app not respond right away. | 
|  | 6745 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { | 
|  | 6746 | // Inject a key, and don't respond - expect that ANR is called. | 
| Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6747 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6748 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); | 
|  | 6749 | ASSERT_TRUE(sequenceNum); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6750 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6751 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6752 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6753 | } | 
|  | 6754 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6755 | // We have a focused application, but no focused window | 
|  | 6756 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6757 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6758 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 6759 | mWindow->consumeFocusEvent(false); | 
|  | 6760 |  | 
|  | 6761 | // taps on the window work as normal | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6762 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6763 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6764 | WINDOW_LOCATION)); | 
|  | 6765 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); | 
|  | 6766 | mDispatcher->waitForIdle(); | 
|  | 6767 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 6768 |  | 
|  | 6769 | // Once a focused event arrives, we get an ANR for this application | 
|  | 6770 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 6771 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6772 | const InputEventInjectionResult result = | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6773 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
|  | 6774 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6775 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6776 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6777 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6778 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6779 | } | 
|  | 6780 |  | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6781 | /** | 
|  | 6782 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, | 
|  | 6783 | * there will not be an ANR. | 
|  | 6784 | */ | 
|  | 6785 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { | 
|  | 6786 | mWindow->setFocusable(false); | 
|  | 6787 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 6788 | mWindow->consumeFocusEvent(false); | 
|  | 6789 |  | 
|  | 6790 | KeyEvent event; | 
|  | 6791 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - | 
|  | 6792 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); | 
|  | 6793 |  | 
|  | 6794 | // Define a valid key down event that is stale (too old). | 
|  | 6795 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, | 
|  | 6796 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6797 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6798 |  | 
|  | 6799 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; | 
|  | 6800 |  | 
|  | 6801 | InputEventInjectionResult result = | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6802 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, | 
| Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6803 | InputEventInjectionSync::WAIT_FOR_RESULT, | 
|  | 6804 | INJECT_EVENT_TIMEOUT, policyFlags); | 
|  | 6805 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) | 
|  | 6806 | << "Injection should fail because the event is stale"; | 
|  | 6807 |  | 
|  | 6808 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6809 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 6810 | mWindow->assertNoEvents(); | 
|  | 6811 | } | 
|  | 6812 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6813 | // We have a focused application, but no focused window | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6814 | // Make sure that we don't notify policy twice about the same ANR. | 
|  | 6815 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6816 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6817 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 6818 | mWindow->consumeFocusEvent(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6819 |  | 
|  | 6820 | // Once a focused event arrives, we get an ANR for this application | 
|  | 6821 | // We specify the injection timeout to be smaller than the application timeout, to ensure that | 
|  | 6822 | // injection times out (instead of failing). | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6823 | const InputEventInjectionResult result = | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6824 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
|  | 6825 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6826 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6827 | const std::chrono::duration appTimeout = | 
|  | 6828 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
|  | 6829 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6830 |  | 
| Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6831 | std::this_thread::sleep_for(appTimeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6832 | // ANR should not be raised again. It is up to policy to do that if it desires. | 
|  | 6833 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6834 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6835 | // If we now get a focused window, the ANR should stop, but the policy handles that via | 
|  | 6836 | // '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] | 6837 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6838 | } | 
|  | 6839 |  | 
|  | 6840 | // We have a focused application, but no focused window | 
|  | 6841 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6842 | mWindow->setFocusable(false); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6843 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 6844 | mWindow->consumeFocusEvent(false); | 
|  | 6845 |  | 
|  | 6846 | // Once a focused event arrives, we get an ANR for this application | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6847 | const InputEventInjectionResult result = | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6848 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6849 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 6850 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6851 |  | 
| Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6852 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
|  | 6853 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6854 |  | 
|  | 6855 | // Future focused events get dropped right away | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6856 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6857 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 6858 | mWindow->assertNoEvents(); | 
|  | 6859 | } | 
|  | 6860 |  | 
|  | 6861 | /** | 
|  | 6862 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the | 
|  | 6863 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. | 
|  | 6864 | * If we process 1 of the events, but ANR on the second event with the same timestamp, | 
|  | 6865 | * the ANR mechanism should still work. | 
|  | 6866 | * | 
|  | 6867 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the | 
|  | 6868 | * DOWN event, while not responding on the second one. | 
|  | 6869 | */ | 
|  | 6870 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { | 
|  | 6871 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 6872 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6873 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 6874 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 6875 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6876 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6877 |  | 
|  | 6878 | // Now send ACTION_UP, with identical timestamp | 
|  | 6879 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 6880 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, | 
|  | 6881 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 6882 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6883 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6884 |  | 
|  | 6885 | // We have now sent down and up. Let's consume first event and then ANR on the second. | 
|  | 6886 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6887 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6888 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6889 | } | 
|  | 6890 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6891 | // A spy window can receive an ANR | 
|  | 6892 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { | 
|  | 6893 | sp<FakeWindowHandle> spy = addSpyWindow(); | 
|  | 6894 |  | 
|  | 6895 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6896 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6897 | WINDOW_LOCATION)); | 
|  | 6898 | mWindow->consumeMotionDown(); | 
|  | 6899 |  | 
|  | 6900 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN | 
|  | 6901 | ASSERT_TRUE(sequenceNum); | 
|  | 6902 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6903 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6904 |  | 
|  | 6905 | spy->finishEvent(*sequenceNum); | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6906 | spy->consumeMotionEvent( | 
|  | 6907 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6908 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6909 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6910 | } | 
|  | 6911 |  | 
|  | 6912 | // 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] | 6913 | // new motion events | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6914 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { | 
|  | 6915 | sp<FakeWindowHandle> spy = addSpyWindow(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6916 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6918 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6919 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6920 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6921 |  | 
|  | 6922 | // Stuck on the ACTION_UP | 
|  | 6923 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6924 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6925 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6926 | // 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] | 6927 | tapOnWindow(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6928 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6929 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6930 |  | 
|  | 6931 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 6932 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6933 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6934 | mWindow->assertNoEvents(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6935 | spy->assertNoEvents(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6936 | } | 
|  | 6937 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6938 | // 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] | 6939 | // new motion events | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6940 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { | 
|  | 6941 | sp<FakeWindowHandle> spy = addSpyWindow(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6942 |  | 
|  | 6943 | tapOnWindow(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6944 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6945 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6946 |  | 
|  | 6947 | mWindow->consumeMotionDown(); | 
|  | 6948 | // Stuck on the ACTION_UP | 
|  | 6949 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6950 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6951 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6952 | // 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] | 6953 | tapOnWindow(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6954 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6955 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6956 |  | 
|  | 6957 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion | 
|  | 6958 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6959 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6960 | mWindow->assertNoEvents(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6961 | spy->assertNoEvents(); | 
|  | 6962 | } | 
|  | 6963 |  | 
|  | 6964 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { | 
|  | 6965 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); | 
|  | 6966 |  | 
|  | 6967 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); | 
|  | 6968 |  | 
|  | 6969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 6970 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 6971 | WINDOW_LOCATION)); | 
|  | 6972 |  | 
|  | 6973 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 6974 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); | 
|  | 6975 | ASSERT_TRUE(consumeSeq); | 
|  | 6976 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6977 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6978 |  | 
|  | 6979 | monitor.finishEvent(*consumeSeq); | 
|  | 6980 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); | 
|  | 6981 |  | 
|  | 6982 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6983 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6984 | } | 
|  | 6985 |  | 
|  | 6986 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to | 
|  | 6987 | // process events, you don't get an anr. When the window later becomes unresponsive again, you | 
|  | 6988 | // get an ANR again. | 
|  | 6989 | // 1. tap -> block on ACTION_UP -> receive ANR | 
|  | 6990 | // 2. consume all pending events (= queue becomes healthy again) | 
|  | 6991 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time | 
|  | 6992 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { | 
|  | 6993 | tapOnWindow(); | 
|  | 6994 |  | 
|  | 6995 | mWindow->consumeMotionDown(); | 
|  | 6996 | // Block on ACTION_UP | 
|  | 6997 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6998 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6999 | mWindow->consumeMotionUp(); // Now the connection should be healthy again | 
|  | 7000 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7001 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7002 | mWindow->assertNoEvents(); | 
|  | 7003 |  | 
|  | 7004 | tapOnWindow(); | 
|  | 7005 | mWindow->consumeMotionDown(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7006 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7007 | mWindow->consumeMotionUp(); | 
|  | 7008 |  | 
|  | 7009 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7010 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7011 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7012 | mWindow->assertNoEvents(); | 
|  | 7013 | } | 
|  | 7014 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7015 | // If a connection remains unresponsive for a while, make sure policy is only notified once about | 
|  | 7016 | // it. | 
|  | 7017 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7018 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7019 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7020 | WINDOW_LOCATION)); | 
|  | 7021 |  | 
|  | 7022 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7023 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7024 | std::this_thread::sleep_for(windowTimeout); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7025 | // 'notifyConnectionUnresponsive' should only be called once per connection | 
|  | 7026 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 7027 | // 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] | 7028 | mWindow->consumeMotionDown(); | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7029 | mWindow->consumeMotionEvent( | 
|  | 7030 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7031 | mWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7032 | mDispatcher->waitForIdle(); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7033 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7034 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7035 | } | 
|  | 7036 |  | 
|  | 7037 | /** | 
|  | 7038 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 7039 | * not to to the focused window until the motion is processed. | 
|  | 7040 | * | 
|  | 7041 | * Warning!!! | 
|  | 7042 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 7043 | * and the injection timeout that we specify when injecting the key. | 
|  | 7044 | * We must have the injection timeout (10ms) be smaller than | 
|  | 7045 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 7046 | * | 
|  | 7047 | * If that value changes, this test should also change. | 
|  | 7048 | */ | 
|  | 7049 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { | 
|  | 7050 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 7051 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 7052 |  | 
|  | 7053 | tapOnWindow(); | 
|  | 7054 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 7055 | ASSERT_TRUE(downSequenceNum); | 
|  | 7056 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 7057 | ASSERT_TRUE(upSequenceNum); | 
|  | 7058 | // Don't finish the events yet, and send a key | 
|  | 7059 | // Injection will "succeed" because we will eventually give up and send the key to the focused | 
|  | 7060 | // window even if motions are still being processed. But because the injection timeout is short, | 
|  | 7061 | // we will receive INJECTION_TIMED_OUT as the result. | 
|  | 7062 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7063 | InputEventInjectionResult result = | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7064 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7065 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); | 
|  | 7066 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7067 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 7068 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 7069 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 7070 | ASSERT_FALSE(keySequenceNum); | 
|  | 7071 |  | 
|  | 7072 | std::this_thread::sleep_for(500ms); | 
|  | 7073 | // if we wait long enough though, dispatcher will give up, and still send the key | 
|  | 7074 | // to the focused window, even though we have not yet finished the motion event | 
|  | 7075 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 7076 | mWindow->finishEvent(*downSequenceNum); | 
|  | 7077 | mWindow->finishEvent(*upSequenceNum); | 
|  | 7078 | } | 
|  | 7079 |  | 
|  | 7080 | /** | 
|  | 7081 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 7082 | * not go to the focused window until the motion is processed. | 
|  | 7083 | * If then a new motion comes in, then the pending key event should be going to the currently | 
|  | 7084 | * focused window right away. | 
|  | 7085 | */ | 
|  | 7086 | TEST_F(InputDispatcherSingleWindowAnr, | 
|  | 7087 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { | 
|  | 7088 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering | 
|  | 7089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 7090 |  | 
|  | 7091 | tapOnWindow(); | 
|  | 7092 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); | 
|  | 7093 | ASSERT_TRUE(downSequenceNum); | 
|  | 7094 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); | 
|  | 7095 | ASSERT_TRUE(upSequenceNum); | 
|  | 7096 | // Don't finish the events yet, and send a key | 
|  | 7097 | // Injection is async, so it will succeed | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7098 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7099 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
|  | 7100 | InputEventInjectionSync::NONE)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7101 | // At this point, key is still pending, and should not be sent to the application yet. | 
|  | 7102 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); | 
|  | 7103 | ASSERT_FALSE(keySequenceNum); | 
|  | 7104 |  | 
|  | 7105 | // Now tap down again. It should cause the pending key to go to the focused window right away. | 
|  | 7106 | tapOnWindow(); | 
|  | 7107 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd | 
|  | 7108 | // the other events yet. We can finish events in any order. | 
|  | 7109 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN | 
|  | 7110 | mWindow->finishEvent(*upSequenceNum);   // first tap's ACTION_UP | 
|  | 7111 | mWindow->consumeMotionDown(); | 
|  | 7112 | mWindow->consumeMotionUp(); | 
|  | 7113 | mWindow->assertNoEvents(); | 
|  | 7114 | } | 
|  | 7115 |  | 
| Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 7116 | /** | 
|  | 7117 | * Send an event to the app and have the app not respond right away. | 
|  | 7118 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. | 
|  | 7119 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. | 
|  | 7120 | * At some point, the window becomes responsive again. | 
|  | 7121 | * Ensure that subsequent events get dropped, and the next gesture is delivered. | 
|  | 7122 | */ | 
|  | 7123 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { | 
|  | 7124 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7125 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) | 
|  | 7126 | .build()); | 
|  | 7127 |  | 
|  | 7128 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN | 
|  | 7129 | ASSERT_TRUE(sequenceNum); | 
|  | 7130 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
|  | 7131 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); | 
|  | 7132 |  | 
|  | 7133 | mWindow->finishEvent(*sequenceNum); | 
|  | 7134 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); | 
|  | 7135 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 7136 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); | 
|  | 7137 |  | 
|  | 7138 | // Now that the window is responsive, let's continue the gesture. | 
|  | 7139 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7140 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) | 
|  | 7141 | .build()); | 
|  | 7142 |  | 
|  | 7143 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7144 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) | 
|  | 7145 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) | 
|  | 7146 | .build()); | 
|  | 7147 |  | 
|  | 7148 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7149 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) | 
|  | 7150 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) | 
|  | 7151 | .build()); | 
|  | 7152 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7153 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) | 
|  | 7154 | .build()); | 
|  | 7155 | // We already canceled this pointer, so the window shouldn't get any new events. | 
|  | 7156 | mWindow->assertNoEvents(); | 
|  | 7157 |  | 
|  | 7158 | // Start another one. | 
|  | 7159 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 7160 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) | 
|  | 7161 | .build()); | 
|  | 7162 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); | 
|  | 7163 | } | 
|  | 7164 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7165 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { | 
|  | 7166 | virtual void SetUp() override { | 
|  | 7167 | InputDispatcherTest::SetUp(); | 
|  | 7168 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7169 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7170 | mApplication->setDispatchingTimeout(10ms); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7171 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", | 
|  | 7172 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7173 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7174 | // 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] | 7175 | mUnfocusedWindow->setWatchOutsideTouch(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7176 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7177 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", | 
|  | 7178 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 7179 | mFocusedWindow->setDispatchingTimeout(30ms); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7180 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7181 |  | 
|  | 7182 | // Set focused application. | 
|  | 7183 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7184 | mFocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7185 |  | 
|  | 7186 | // Expect one focus window exist in display. | 
|  | 7187 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7188 | setFocusedWindow(mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7189 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 7190 | } | 
|  | 7191 |  | 
|  | 7192 | virtual void TearDown() override { | 
|  | 7193 | InputDispatcherTest::TearDown(); | 
|  | 7194 |  | 
|  | 7195 | mUnfocusedWindow.clear(); | 
|  | 7196 | mFocusedWindow.clear(); | 
|  | 7197 | } | 
|  | 7198 |  | 
|  | 7199 | protected: | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7200 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7201 | sp<FakeWindowHandle> mUnfocusedWindow; | 
|  | 7202 | sp<FakeWindowHandle> mFocusedWindow; | 
|  | 7203 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; | 
|  | 7204 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; | 
|  | 7205 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; | 
|  | 7206 |  | 
|  | 7207 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } | 
|  | 7208 |  | 
|  | 7209 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } | 
|  | 7210 |  | 
|  | 7211 | private: | 
|  | 7212 | void tap(const PointF& location) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7214 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7215 | location)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7217 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7218 | location)); | 
|  | 7219 | } | 
|  | 7220 | }; | 
|  | 7221 |  | 
|  | 7222 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout | 
|  | 7223 | // should be ANR'd first. | 
|  | 7224 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7226 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7227 | FOCUSED_WINDOW_LOCATION)) | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7228 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7229 | mFocusedWindow->consumeMotionDown(); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7230 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7231 | ADISPLAY_ID_DEFAULT, /*flags=*/0); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7232 | // We consumed all events, so no ANR | 
|  | 7233 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 7234 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 7235 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7237 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7238 | FOCUSED_WINDOW_LOCATION)); | 
|  | 7239 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 7240 | ASSERT_TRUE(unfocusedSequenceNum); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7241 |  | 
|  | 7242 | const std::chrono::duration timeout = | 
|  | 7243 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7244 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7245 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event | 
|  | 7246 | // sequence to make it consistent | 
|  | 7247 | mFocusedWindow->consumeMotionCancel(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7248 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7249 | mFocusedWindow->consumeMotionDown(); | 
|  | 7250 | // This cancel is generated because the connection was unresponsive | 
|  | 7251 | mFocusedWindow->consumeMotionCancel(); | 
|  | 7252 | mFocusedWindow->assertNoEvents(); | 
|  | 7253 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7254 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7255 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), | 
|  | 7256 | mFocusedWindow->getPid()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7257 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7258 | } | 
|  | 7259 |  | 
|  | 7260 | // If we have 2 windows with identical timeouts that are both unresponsive, | 
|  | 7261 | // it doesn't matter which order they should have ANR. | 
|  | 7262 | // But we should receive ANR for both. | 
|  | 7263 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { | 
|  | 7264 | // Set the timeout for unfocused window to match the focused window | 
|  | 7265 | mUnfocusedWindow->setDispatchingTimeout(10ms); | 
|  | 7266 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 7267 |  | 
|  | 7268 | tapOnFocusedWindow(); | 
|  | 7269 | // 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] | 7270 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; | 
|  | 7271 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); | 
|  | 7272 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7273 |  | 
|  | 7274 | // 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] | 7275 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 7276 | mFocusedWindow->getToken() == anrConnectionToken2); | 
|  | 7277 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || | 
|  | 7278 | mUnfocusedWindow->getToken() == anrConnectionToken2); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7279 |  | 
|  | 7280 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 7281 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7282 |  | 
|  | 7283 | mFocusedWindow->consumeMotionDown(); | 
|  | 7284 | mFocusedWindow->consumeMotionUp(); | 
|  | 7285 | mUnfocusedWindow->consumeMotionOutside(); | 
|  | 7286 |  | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7287 | sp<IBinder> responsiveToken1, responsiveToken2; | 
|  | 7288 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); | 
|  | 7289 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7290 |  | 
|  | 7291 | // Both applications should be marked as responsive, in any order | 
|  | 7292 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || | 
|  | 7293 | mFocusedWindow->getToken() == responsiveToken2); | 
|  | 7294 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || | 
|  | 7295 | mUnfocusedWindow->getToken() == responsiveToken2); | 
|  | 7296 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7297 | } | 
|  | 7298 |  | 
|  | 7299 | // If a window is already not responding, the second tap on the same window should be ignored. | 
|  | 7300 | // We should also log an error to account for the dropped event (not tested here). | 
|  | 7301 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. | 
|  | 7302 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { | 
|  | 7303 | tapOnFocusedWindow(); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7304 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7305 | ADISPLAY_ID_DEFAULT, /*flags=*/0); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7306 | // Receive the events, but don't respond | 
|  | 7307 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN | 
|  | 7308 | ASSERT_TRUE(downEventSequenceNum); | 
|  | 7309 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP | 
|  | 7310 | ASSERT_TRUE(upEventSequenceNum); | 
|  | 7311 | const std::chrono::duration timeout = | 
|  | 7312 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7313 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7314 |  | 
|  | 7315 | // Tap once again | 
|  | 7316 | // 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] | 7317 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7318 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7319 | FOCUSED_WINDOW_LOCATION)); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7320 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7321 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7322 | FOCUSED_WINDOW_LOCATION)); | 
|  | 7323 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a | 
|  | 7324 | // valid touch target | 
|  | 7325 | mUnfocusedWindow->assertNoEvents(); | 
|  | 7326 |  | 
|  | 7327 | // Consume the first tap | 
|  | 7328 | mFocusedWindow->finishEvent(*downEventSequenceNum); | 
|  | 7329 | mFocusedWindow->finishEvent(*upEventSequenceNum); | 
|  | 7330 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 7331 | // The second tap did not go to the focused window | 
|  | 7332 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7333 | // Since all events are finished, connection should be deemed healthy again | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7334 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), | 
|  | 7335 | mFocusedWindow->getPid()); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7336 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 7337 | } | 
|  | 7338 |  | 
|  | 7339 | // If you tap outside of all windows, there will not be ANR | 
|  | 7340 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7341 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7342 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7343 | LOCATION_OUTSIDE_ALL_WINDOWS)); | 
|  | 7344 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 7345 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 7346 | } | 
|  | 7347 |  | 
|  | 7348 | // Since the focused window is paused, tapping on it should not produce any events | 
|  | 7349 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { | 
|  | 7350 | mFocusedWindow->setPaused(true); | 
|  | 7351 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); | 
|  | 7352 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7353 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7354 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7355 | FOCUSED_WINDOW_LOCATION)); | 
|  | 7356 |  | 
|  | 7357 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); | 
|  | 7358 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 7359 | // Should not ANR because the window is paused, and touches shouldn't go to it | 
|  | 7360 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
|  | 7361 |  | 
|  | 7362 | mFocusedWindow->assertNoEvents(); | 
|  | 7363 | mUnfocusedWindow->assertNoEvents(); | 
|  | 7364 | } | 
|  | 7365 |  | 
|  | 7366 | /** | 
|  | 7367 | * If a window is processing a motion event, and then a key event comes in, the key event should | 
|  | 7368 | * not to to the focused window until the motion is processed. | 
|  | 7369 | * If a different window becomes focused at this time, the key should go to that window instead. | 
|  | 7370 | * | 
|  | 7371 | * Warning!!! | 
|  | 7372 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT | 
|  | 7373 | * and the injection timeout that we specify when injecting the key. | 
|  | 7374 | * We must have the injection timeout (10ms) be smaller than | 
|  | 7375 | *  KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). | 
|  | 7376 | * | 
|  | 7377 | * If that value changes, this test should also change. | 
|  | 7378 | */ | 
|  | 7379 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { | 
|  | 7380 | // Set a long ANR timeout to prevent it from triggering | 
|  | 7381 | mFocusedWindow->setDispatchingTimeout(2s); | 
|  | 7382 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 7383 |  | 
|  | 7384 | tapOnUnfocusedWindow(); | 
|  | 7385 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 7386 | ASSERT_TRUE(downSequenceNum); | 
|  | 7387 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); | 
|  | 7388 | ASSERT_TRUE(upSequenceNum); | 
|  | 7389 | // Don't finish the events yet, and send a key | 
|  | 7390 | // Injection will succeed because we will eventually give up and send the key to the focused | 
|  | 7391 | // window even if motions are still being processed. | 
|  | 7392 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7393 | InputEventInjectionResult result = | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7394 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
|  | 7395 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7396 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7397 | // Key will not be sent to the window, yet, because the window is still processing events | 
|  | 7398 | // and the key remains pending, waiting for the touch events to be processed | 
|  | 7399 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); | 
|  | 7400 | ASSERT_FALSE(keySequenceNum); | 
|  | 7401 |  | 
|  | 7402 | // 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] | 7403 | mFocusedWindow->setFocusable(false); | 
|  | 7404 | mUnfocusedWindow->setFocusable(true); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7405 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7406 | setFocusedWindow(mUnfocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7407 |  | 
|  | 7408 | // Focus events should precede the key events | 
|  | 7409 | mUnfocusedWindow->consumeFocusEvent(true); | 
|  | 7410 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 7411 |  | 
|  | 7412 | // Finish the tap events, which should unblock dispatcher | 
|  | 7413 | mUnfocusedWindow->finishEvent(*downSequenceNum); | 
|  | 7414 | mUnfocusedWindow->finishEvent(*upSequenceNum); | 
|  | 7415 |  | 
|  | 7416 | // Now that all queues are cleared and no backlog in the connections, the key event | 
|  | 7417 | // can finally go to the newly focused "mUnfocusedWindow". | 
|  | 7418 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 7419 | mFocusedWindow->assertNoEvents(); | 
|  | 7420 | mUnfocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7421 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7422 | } | 
|  | 7423 |  | 
|  | 7424 | // When the touch stream is split across 2 windows, and one of them does not respond, | 
|  | 7425 | // then ANR should be raised and the touch should be canceled for the unresponsive window. | 
|  | 7426 | // The other window should not be affected by that. | 
|  | 7427 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { | 
|  | 7428 | // Touch Window 1 | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7429 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 7430 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7431 | {FOCUSED_WINDOW_LOCATION})); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7432 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7433 | ADISPLAY_ID_DEFAULT, /*flags=*/0); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7434 |  | 
|  | 7435 | // Touch Window 2 | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7436 | mDispatcher->notifyMotion( | 
|  | 7437 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7438 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7439 |  | 
|  | 7440 | const std::chrono::duration timeout = | 
|  | 7441 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7442 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7443 |  | 
|  | 7444 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 7445 | mFocusedWindow->consumeMotionDown(); | 
|  | 7446 | // Focused window may or may not receive ACTION_MOVE | 
|  | 7447 | // But it should definitely receive ACTION_CANCEL due to the ANR | 
|  | 7448 | InputEvent* event; | 
|  | 7449 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); | 
|  | 7450 | ASSERT_TRUE(moveOrCancelSequenceNum); | 
|  | 7451 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); | 
|  | 7452 | ASSERT_NE(nullptr, event); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7453 | ASSERT_EQ(event->getType(), InputEventType::MOTION); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7454 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); | 
|  | 7455 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { | 
|  | 7456 | mFocusedWindow->consumeMotionCancel(); | 
|  | 7457 | } else { | 
|  | 7458 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); | 
|  | 7459 | } | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7460 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
| Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7461 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), | 
|  | 7462 | mFocusedWindow->getPid()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7463 |  | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7464 | mUnfocusedWindow->assertNoEvents(); | 
|  | 7465 | mFocusedWindow->assertNoEvents(); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7466 | mFakePolicy->assertNotifyAnrWasNotCalled(); | 
| Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7467 | } | 
|  | 7468 |  | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7469 | /** | 
|  | 7470 | * If we have no focused window, and a key comes in, we start the ANR timer. | 
|  | 7471 | * The focused application should add a focused window before the timer runs out to prevent ANR. | 
|  | 7472 | * | 
|  | 7473 | * If the user touches another application during this time, the key should be dropped. | 
|  | 7474 | * Next, if a new focused window comes in, without toggling the focused application, | 
|  | 7475 | * then no ANR should occur. | 
|  | 7476 | * | 
|  | 7477 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', | 
|  | 7478 | * but in some cases the policy may not update the focused application. | 
|  | 7479 | */ | 
|  | 7480 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { | 
|  | 7481 | std::shared_ptr<FakeApplicationHandle> focusedApplication = | 
|  | 7482 | std::make_shared<FakeApplicationHandle>(); | 
|  | 7483 | focusedApplication->setDispatchingTimeout(60ms); | 
|  | 7484 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); | 
|  | 7485 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. | 
|  | 7486 | mFocusedWindow->setFocusable(false); | 
|  | 7487 |  | 
|  | 7488 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 7489 | mFocusedWindow->consumeFocusEvent(false); | 
|  | 7490 |  | 
|  | 7491 | // Send a key. The ANR timer should start because there is no focused window. | 
|  | 7492 | // 'focusedApplication' will get blamed if this timer completes. | 
|  | 7493 | // 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] | 7494 | InputEventInjectionResult result = | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7495 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
|  | 7496 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, | 
|  | 7497 | /*allowKeyRepeat=*/false); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7499 |  | 
|  | 7500 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, | 
|  | 7501 | // then the injected touches won't cause the focused event to get dropped. | 
|  | 7502 | // The dispatcher only checks for whether the queue should be pruned upon queueing. | 
|  | 7503 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would | 
|  | 7504 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. | 
|  | 7505 | // For this test, it means that the key would get delivered to the window once it becomes | 
|  | 7506 | // focused. | 
|  | 7507 | std::this_thread::sleep_for(10ms); | 
|  | 7508 |  | 
|  | 7509 | // Touch unfocused window. This should force the pending key to get dropped. | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7510 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 7511 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7512 | {UNFOCUSED_WINDOW_LOCATION})); | 
| Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7513 |  | 
|  | 7514 | // We do not consume the motion right away, because that would require dispatcher to first | 
|  | 7515 | // process (== drop) the key event, and by that time, ANR will be raised. | 
|  | 7516 | // Set the focused window first. | 
|  | 7517 | mFocusedWindow->setFocusable(true); | 
|  | 7518 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); | 
|  | 7519 | setFocusedWindow(mFocusedWindow); | 
|  | 7520 | mFocusedWindow->consumeFocusEvent(true); | 
|  | 7521 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs | 
|  | 7522 | // to another application. This could be a bug / behaviour in the policy. | 
|  | 7523 |  | 
|  | 7524 | mUnfocusedWindow->consumeMotionDown(); | 
|  | 7525 |  | 
|  | 7526 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 7527 | // Should not ANR because we actually have a focused window. It was just added too slowly. | 
|  | 7528 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); | 
|  | 7529 | } | 
|  | 7530 |  | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7531 | // These tests ensure we cannot send touch events to a window that's positioned behind a window | 
|  | 7532 | // that has feature NO_INPUT_CHANNEL. | 
|  | 7533 | // Layout: | 
|  | 7534 | //   Top (closest to user) | 
|  | 7535 | //       mNoInputWindow (above all windows) | 
|  | 7536 | //       mBottomWindow | 
|  | 7537 | //   Bottom (furthest from user) | 
|  | 7538 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { | 
|  | 7539 | virtual void SetUp() override { | 
|  | 7540 | InputDispatcherTest::SetUp(); | 
|  | 7541 |  | 
|  | 7542 | mApplication = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7543 | mNoInputWindow = | 
|  | 7544 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, | 
|  | 7545 | "Window without input channel", ADISPLAY_ID_DEFAULT, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7546 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7547 | mNoInputWindow->setNoInputChannel(true); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7548 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 7549 | // It's perfectly valid for this window to not have an associated input channel | 
|  | 7550 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7551 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", | 
|  | 7552 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7553 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 7554 |  | 
|  | 7555 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 7556 | } | 
|  | 7557 |  | 
|  | 7558 | protected: | 
|  | 7559 | std::shared_ptr<FakeApplicationHandle> mApplication; | 
|  | 7560 | sp<FakeWindowHandle> mNoInputWindow; | 
|  | 7561 | sp<FakeWindowHandle> mBottomWindow; | 
|  | 7562 | }; | 
|  | 7563 |  | 
|  | 7564 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { | 
|  | 7565 | PointF touchedPoint = {10, 10}; | 
|  | 7566 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7567 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 7568 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7569 | {touchedPoint})); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7570 |  | 
|  | 7571 | mNoInputWindow->assertNoEvents(); | 
|  | 7572 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have | 
|  | 7573 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, | 
|  | 7574 | // and therefore should prevent mBottomWindow from receiving touches | 
|  | 7575 | mBottomWindow->assertNoEvents(); | 
|  | 7576 | } | 
|  | 7577 |  | 
|  | 7578 | /** | 
|  | 7579 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, | 
|  | 7580 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. | 
|  | 7581 | */ | 
|  | 7582 | TEST_F(InputDispatcherMultiWindowOcclusionTests, | 
|  | 7583 | NoInputChannelFeature_DropsTouchesWithValidChannel) { | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7584 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, | 
|  | 7585 | "Window with input channel and NO_INPUT_CHANNEL", | 
|  | 7586 | ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7587 |  | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7588 | mNoInputWindow->setNoInputChannel(true); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7589 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); | 
|  | 7590 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); | 
|  | 7591 |  | 
|  | 7592 | PointF touchedPoint = {10, 10}; | 
|  | 7593 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7594 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 7595 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7596 | {touchedPoint})); | 
| Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7597 |  | 
|  | 7598 | mNoInputWindow->assertNoEvents(); | 
|  | 7599 | mBottomWindow->assertNoEvents(); | 
|  | 7600 | } | 
|  | 7601 |  | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7602 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { | 
|  | 7603 | protected: | 
|  | 7604 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 7605 | sp<FakeWindowHandle> mWindow; | 
|  | 7606 | sp<FakeWindowHandle> mMirror; | 
|  | 7607 |  | 
|  | 7608 | virtual void SetUp() override { | 
|  | 7609 | InputDispatcherTest::SetUp(); | 
|  | 7610 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7611 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
|  | 7612 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", | 
|  | 7613 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7614 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 7615 | mWindow->setFocusable(true); | 
|  | 7616 | mMirror->setFocusable(true); | 
|  | 7617 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 7618 | } | 
|  | 7619 | }; | 
|  | 7620 |  | 
|  | 7621 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { | 
|  | 7622 | // Request focus on a mirrored window | 
|  | 7623 | setFocusedWindow(mMirror); | 
|  | 7624 |  | 
|  | 7625 | // window gets focused | 
|  | 7626 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7627 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 7628 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7629 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 7630 | } | 
|  | 7631 |  | 
|  | 7632 | // A focused & mirrored window remains focused only if the window and its mirror are both | 
|  | 7633 | // focusable. | 
|  | 7634 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { | 
|  | 7635 | setFocusedWindow(mMirror); | 
|  | 7636 |  | 
|  | 7637 | // window gets focused | 
|  | 7638 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7639 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 7640 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7641 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 7643 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7644 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 7645 |  | 
|  | 7646 | mMirror->setFocusable(false); | 
|  | 7647 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 7648 |  | 
|  | 7649 | // window loses focus since one of the windows associated with the token in not focusable | 
|  | 7650 | mWindow->consumeFocusEvent(false); | 
|  | 7651 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7652 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 7653 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7654 | mWindow->assertNoEvents(); | 
|  | 7655 | } | 
|  | 7656 |  | 
|  | 7657 | // A focused & mirrored window remains focused until the window and its mirror both become | 
|  | 7658 | // invisible. | 
|  | 7659 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { | 
|  | 7660 | setFocusedWindow(mMirror); | 
|  | 7661 |  | 
|  | 7662 | // window gets focused | 
|  | 7663 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7664 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 7665 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7666 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 7668 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7669 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 7670 |  | 
|  | 7671 | mMirror->setVisible(false); | 
|  | 7672 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 7673 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 7675 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7676 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 7678 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7679 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 7680 |  | 
|  | 7681 | mWindow->setVisible(false); | 
|  | 7682 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 7683 |  | 
|  | 7684 | // window loses focus only after all windows associated with the token become invisible. | 
|  | 7685 | mWindow->consumeFocusEvent(false); | 
|  | 7686 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7687 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 7688 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7689 | mWindow->assertNoEvents(); | 
|  | 7690 | } | 
|  | 7691 |  | 
|  | 7692 | // A focused & mirrored window remains focused until both windows are removed. | 
|  | 7693 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { | 
|  | 7694 | setFocusedWindow(mMirror); | 
|  | 7695 |  | 
|  | 7696 | // window gets focused | 
|  | 7697 | mWindow->consumeFocusEvent(true); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7698 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 7699 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7700 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 7702 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7703 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 7704 |  | 
|  | 7705 | // single window is removed but the window token remains focused | 
|  | 7706 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); | 
|  | 7707 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 7709 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7710 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7711 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 7712 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7713 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 7714 |  | 
|  | 7715 | // Both windows are removed | 
|  | 7716 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); | 
|  | 7717 | mWindow->consumeFocusEvent(false); | 
|  | 7718 |  | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7719 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) | 
|  | 7720 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7721 | mWindow->assertNoEvents(); | 
|  | 7722 | } | 
|  | 7723 |  | 
|  | 7724 | // Focus request can be pending until one window becomes visible. | 
|  | 7725 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { | 
|  | 7726 | // Request focus on an invisible mirror. | 
|  | 7727 | mWindow->setVisible(false); | 
|  | 7728 | mMirror->setVisible(false); | 
|  | 7729 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 7730 | setFocusedWindow(mMirror); | 
|  | 7731 |  | 
|  | 7732 | // Injected key goes to pending queue. | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7734 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, | 
|  | 7735 | InputEventInjectionSync::NONE)); | 
| Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7736 |  | 
|  | 7737 | mMirror->setVisible(true); | 
|  | 7738 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); | 
|  | 7739 |  | 
|  | 7740 | // window gets focused | 
|  | 7741 | mWindow->consumeFocusEvent(true); | 
|  | 7742 | // window gets the pending key event | 
|  | 7743 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 7744 | } | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7745 |  | 
|  | 7746 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { | 
|  | 7747 | protected: | 
|  | 7748 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 7749 | sp<FakeWindowHandle> mWindow; | 
|  | 7750 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 7751 |  | 
|  | 7752 | void SetUp() override { | 
|  | 7753 | InputDispatcherTest::SetUp(); | 
|  | 7754 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7755 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7756 | mWindow->setFocusable(true); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7757 | mSecondWindow = | 
|  | 7758 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7759 | mSecondWindow->setFocusable(true); | 
|  | 7760 |  | 
|  | 7761 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
|  | 7762 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); | 
|  | 7763 |  | 
|  | 7764 | setFocusedWindow(mWindow); | 
|  | 7765 | mWindow->consumeFocusEvent(true); | 
|  | 7766 | } | 
|  | 7767 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7768 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7769 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7770 | } | 
|  | 7771 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7772 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, | 
|  | 7773 | bool enabled) { | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7774 | mDispatcher->requestPointerCapture(window->getToken(), enabled); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7775 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); | 
|  | 7776 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7777 | window->consumeCaptureEvent(enabled); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7778 | return request; | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7779 | } | 
|  | 7780 | }; | 
|  | 7781 |  | 
|  | 7782 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { | 
|  | 7783 | // Ensure that capture cannot be obtained for unfocused windows. | 
|  | 7784 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
|  | 7785 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 7786 | mSecondWindow->assertNoEvents(); | 
|  | 7787 |  | 
|  | 7788 | // Ensure that capture can be enabled from the focus window. | 
|  | 7789 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 7790 |  | 
|  | 7791 | // Ensure that capture cannot be disabled from a window that does not have capture. | 
|  | 7792 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); | 
|  | 7793 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 7794 |  | 
|  | 7795 | // Ensure that capture can be disabled from the window with capture. | 
|  | 7796 | requestAndVerifyPointerCapture(mWindow, false); | 
|  | 7797 | } | 
|  | 7798 |  | 
|  | 7799 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7800 | auto request = requestAndVerifyPointerCapture(mWindow, true); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7801 |  | 
|  | 7802 | setFocusedWindow(mSecondWindow); | 
|  | 7803 |  | 
|  | 7804 | // Ensure that the capture disabled event was sent first. | 
|  | 7805 | mWindow->consumeCaptureEvent(false); | 
|  | 7806 | mWindow->consumeFocusEvent(false); | 
|  | 7807 | mSecondWindow->consumeFocusEvent(true); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7808 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7809 |  | 
|  | 7810 | // 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] | 7811 | notifyPointerCaptureChanged({}); | 
|  | 7812 | notifyPointerCaptureChanged(request); | 
|  | 7813 | notifyPointerCaptureChanged({}); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7814 | mWindow->assertNoEvents(); | 
|  | 7815 | mSecondWindow->assertNoEvents(); | 
|  | 7816 | mFakePolicy->assertSetPointerCaptureNotCalled(); | 
|  | 7817 | } | 
|  | 7818 |  | 
|  | 7819 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7820 | auto request = requestAndVerifyPointerCapture(mWindow, true); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7821 |  | 
|  | 7822 | // InputReader unexpectedly disables and enables pointer capture. | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7823 | notifyPointerCaptureChanged({}); | 
|  | 7824 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7825 |  | 
|  | 7826 | // Ensure that Pointer Capture is disabled. | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7827 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7828 | mWindow->consumeCaptureEvent(false); | 
|  | 7829 | mWindow->assertNoEvents(); | 
|  | 7830 | } | 
|  | 7831 |  | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7832 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { | 
|  | 7833 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 7834 |  | 
|  | 7835 | // The first window loses focus. | 
|  | 7836 | setFocusedWindow(mSecondWindow); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7837 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7838 | mWindow->consumeCaptureEvent(false); | 
|  | 7839 |  | 
|  | 7840 | // Request Pointer Capture from the second window before the notification from InputReader | 
|  | 7841 | // arrives. | 
|  | 7842 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7843 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7844 |  | 
|  | 7845 | // InputReader notifies Pointer Capture was disabled (because of the focus change). | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7846 | notifyPointerCaptureChanged({}); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7847 |  | 
|  | 7848 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7849 | notifyPointerCaptureChanged(request); | 
| Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7850 |  | 
|  | 7851 | mSecondWindow->consumeFocusEvent(true); | 
|  | 7852 | mSecondWindow->consumeCaptureEvent(true); | 
|  | 7853 | } | 
|  | 7854 |  | 
| Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7855 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { | 
|  | 7856 | // App repeatedly enables and disables capture. | 
|  | 7857 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 7858 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 7859 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); | 
|  | 7860 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
|  | 7861 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 7862 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 7863 |  | 
|  | 7864 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the | 
|  | 7865 | // first request is now stale, this should do nothing. | 
|  | 7866 | notifyPointerCaptureChanged(firstRequest); | 
|  | 7867 | mWindow->assertNoEvents(); | 
|  | 7868 |  | 
|  | 7869 | // InputReader notifies that the second request was enabled. | 
|  | 7870 | notifyPointerCaptureChanged(secondRequest); | 
|  | 7871 | mWindow->consumeCaptureEvent(true); | 
|  | 7872 | } | 
|  | 7873 |  | 
| Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7874 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { | 
|  | 7875 | requestAndVerifyPointerCapture(mWindow, true); | 
|  | 7876 |  | 
|  | 7877 | // App toggles pointer capture off and on. | 
|  | 7878 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); | 
|  | 7879 | mFakePolicy->assertSetPointerCaptureCalled(false); | 
|  | 7880 |  | 
|  | 7881 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); | 
|  | 7882 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); | 
|  | 7883 |  | 
|  | 7884 | // InputReader notifies that the latest "enable" request was processed, while skipping over the | 
|  | 7885 | // preceding "disable" request. | 
|  | 7886 | notifyPointerCaptureChanged(enableRequest); | 
|  | 7887 |  | 
|  | 7888 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive | 
|  | 7889 | // any notifications. | 
|  | 7890 | mWindow->assertNoEvents(); | 
|  | 7891 | } | 
|  | 7892 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7893 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { | 
|  | 7894 | protected: | 
|  | 7895 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7896 |  | 
|  | 7897 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; | 
|  | 7898 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); | 
|  | 7899 |  | 
|  | 7900 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; | 
|  | 7901 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 7902 |  | 
|  | 7903 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold | 
|  | 7904 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; | 
|  | 7905 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); | 
|  | 7906 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < | 
|  | 7907 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 7908 |  | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7909 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; | 
|  | 7910 | static constexpr gui::Uid APP_B_UID{10002}; | 
|  | 7911 | static constexpr gui::Uid APP_C_UID{10003}; | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7912 |  | 
|  | 7913 | sp<FakeWindowHandle> mTouchWindow; | 
|  | 7914 |  | 
|  | 7915 | virtual void SetUp() override { | 
|  | 7916 | InputDispatcherTest::SetUp(); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7917 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7918 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); | 
|  | 7919 | } | 
|  | 7920 |  | 
|  | 7921 | virtual void TearDown() override { | 
|  | 7922 | InputDispatcherTest::TearDown(); | 
|  | 7923 | mTouchWindow.clear(); | 
|  | 7924 | } | 
|  | 7925 |  | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7926 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7927 | float alpha = 1.0f) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7928 | sp<FakeWindowHandle> window = getWindow(uid, name); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7929 | window->setTouchable(false); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7930 | window->setTouchOcclusionMode(mode); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7931 | window->setAlpha(alpha); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7932 | return window; | 
|  | 7933 | } | 
|  | 7934 |  | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7935 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7936 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); | 
|  | 7937 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7938 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7939 | // Generate an arbitrary PID based on the UID | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7940 | window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7941 | return window; | 
|  | 7942 | } | 
|  | 7943 |  | 
|  | 7944 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7945 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 7946 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 7947 | points)); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7948 | } | 
|  | 7949 | }; | 
|  | 7950 |  | 
|  | 7951 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7952 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7953 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7954 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7955 |  | 
|  | 7956 | touch(); | 
|  | 7957 |  | 
|  | 7958 | mTouchWindow->assertNoEvents(); | 
|  | 7959 | } | 
|  | 7960 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7961 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7962 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { | 
|  | 7963 | const sp<FakeWindowHandle>& w = | 
|  | 7964 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); | 
|  | 7965 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 7966 |  | 
|  | 7967 | touch(); | 
|  | 7968 |  | 
|  | 7969 | mTouchWindow->assertNoEvents(); | 
|  | 7970 | } | 
|  | 7971 |  | 
|  | 7972 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7973 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { | 
|  | 7974 | const sp<FakeWindowHandle>& w = | 
|  | 7975 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 7976 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 7977 |  | 
|  | 7978 | touch(); | 
|  | 7979 |  | 
|  | 7980 | w->assertNoEvents(); | 
|  | 7981 | } | 
|  | 7982 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7983 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7984 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); | 
|  | 7985 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7986 |  | 
|  | 7987 | touch(); | 
|  | 7988 |  | 
|  | 7989 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 7990 | } | 
|  | 7991 |  | 
|  | 7992 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7993 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7994 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7995 | w->setFrame(Rect(0, 0, 50, 50)); | 
|  | 7996 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7997 |  | 
|  | 7998 | touch({PointF{100, 100}}); | 
|  | 7999 |  | 
|  | 8000 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8001 | } | 
|  | 8002 |  | 
|  | 8003 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8004 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8005 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8006 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8007 |  | 
|  | 8008 | touch(); | 
|  | 8009 |  | 
|  | 8010 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8011 | } | 
|  | 8012 |  | 
|  | 8013 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { | 
|  | 8014 | const sp<FakeWindowHandle>& w = | 
|  | 8015 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 8016 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8017 |  | 
|  | 8018 | touch(); | 
|  | 8019 |  | 
|  | 8020 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8021 | } | 
|  | 8022 |  | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8023 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { | 
|  | 8024 | const sp<FakeWindowHandle>& w = | 
|  | 8025 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
|  | 8026 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8027 |  | 
|  | 8028 | touch(); | 
|  | 8029 |  | 
|  | 8030 | w->assertNoEvents(); | 
|  | 8031 | } | 
|  | 8032 |  | 
|  | 8033 | /** | 
|  | 8034 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs | 
|  | 8035 | * inside) while letting them pass-through. Note that even though touch passes through the occluding | 
|  | 8036 | * window, the occluding window will still receive ACTION_OUTSIDE event. | 
|  | 8037 | */ | 
|  | 8038 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8039 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { | 
|  | 8040 | const sp<FakeWindowHandle>& w = | 
|  | 8041 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8042 | w->setWatchOutsideTouch(true); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8043 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8044 |  | 
|  | 8045 | touch(); | 
|  | 8046 |  | 
|  | 8047 | w->consumeMotionOutside(); | 
|  | 8048 | } | 
|  | 8049 |  | 
|  | 8050 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { | 
|  | 8051 | const sp<FakeWindowHandle>& w = | 
|  | 8052 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8053 | w->setWatchOutsideTouch(true); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8054 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8055 |  | 
|  | 8056 | touch(); | 
|  | 8057 |  | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8058 | w->consumeMotionOutsideWithZeroedCoords(); | 
| Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8059 | } | 
|  | 8060 |  | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8061 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8062 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8063 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8064 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8065 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8066 |  | 
|  | 8067 | touch(); | 
|  | 8068 |  | 
|  | 8069 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8070 | } | 
|  | 8071 |  | 
|  | 8072 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { | 
|  | 8073 | const sp<FakeWindowHandle>& w = | 
|  | 8074 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8075 | MAXIMUM_OBSCURING_OPACITY); | 
|  | 8076 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8077 |  | 
|  | 8078 | touch(); | 
|  | 8079 |  | 
|  | 8080 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8081 | } | 
|  | 8082 |  | 
|  | 8083 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8084 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8085 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8086 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8087 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8088 |  | 
|  | 8089 | touch(); | 
|  | 8090 |  | 
|  | 8091 | mTouchWindow->assertNoEvents(); | 
|  | 8092 | } | 
|  | 8093 |  | 
|  | 8094 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { | 
|  | 8095 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 | 
|  | 8096 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8097 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 8098 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8099 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8100 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 8101 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8102 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 8103 |  | 
|  | 8104 | touch(); | 
|  | 8105 |  | 
|  | 8106 | mTouchWindow->assertNoEvents(); | 
|  | 8107 | } | 
|  | 8108 |  | 
|  | 8109 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { | 
|  | 8110 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 | 
|  | 8111 | const sp<FakeWindowHandle>& w1 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8112 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, | 
|  | 8113 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8114 | const sp<FakeWindowHandle>& w2 = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8115 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, | 
|  | 8116 | OPACITY_FAR_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8117 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 8118 |  | 
|  | 8119 | touch(); | 
|  | 8120 |  | 
|  | 8121 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8122 | } | 
|  | 8123 |  | 
|  | 8124 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8125 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { | 
|  | 8126 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8127 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8128 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8129 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8130 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 8131 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8132 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 8133 |  | 
|  | 8134 | touch(); | 
|  | 8135 |  | 
|  | 8136 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8137 | } | 
|  | 8138 |  | 
|  | 8139 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { | 
|  | 8140 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8141 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8142 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8143 | const sp<FakeWindowHandle>& wC = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8144 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 8145 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8146 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
| Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8147 |  | 
|  | 8148 | touch(); | 
|  | 8149 |  | 
|  | 8150 | mTouchWindow->assertNoEvents(); | 
|  | 8151 | } | 
|  | 8152 |  | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8153 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8154 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { | 
|  | 8155 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8156 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 8157 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8158 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8159 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8160 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8161 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 8162 |  | 
|  | 8163 | touch(); | 
|  | 8164 |  | 
|  | 8165 | mTouchWindow->assertNoEvents(); | 
|  | 8166 | } | 
|  | 8167 |  | 
|  | 8168 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8169 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { | 
|  | 8170 | const sp<FakeWindowHandle>& wA = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8171 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 8172 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8173 | const sp<FakeWindowHandle>& wB = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8174 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8175 | OPACITY_BELOW_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8176 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); | 
|  | 8177 |  | 
|  | 8178 | touch(); | 
|  | 8179 |  | 
|  | 8180 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8181 | } | 
|  | 8182 |  | 
|  | 8183 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { | 
|  | 8184 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8185 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, | 
|  | 8186 | OPACITY_ABOVE_THRESHOLD); | 
| Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8187 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8188 |  | 
|  | 8189 | touch(); | 
|  | 8190 |  | 
|  | 8191 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8192 | } | 
|  | 8193 |  | 
|  | 8194 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { | 
|  | 8195 | const sp<FakeWindowHandle>& w = | 
|  | 8196 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 8197 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8198 |  | 
|  | 8199 | touch(); | 
|  | 8200 |  | 
|  | 8201 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8202 | } | 
|  | 8203 |  | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8204 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8205 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { | 
|  | 8206 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 8207 | const sp<FakeWindowHandle>& w = | 
|  | 8208 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); | 
|  | 8209 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8210 |  | 
|  | 8211 | touch(); | 
|  | 8212 |  | 
|  | 8213 | mTouchWindow->assertNoEvents(); | 
|  | 8214 | } | 
|  | 8215 |  | 
|  | 8216 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { | 
|  | 8217 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); | 
|  | 8218 | const sp<FakeWindowHandle>& w = | 
|  | 8219 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); | 
|  | 8220 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8221 |  | 
|  | 8222 | touch(); | 
|  | 8223 |  | 
|  | 8224 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8225 | } | 
|  | 8226 |  | 
|  | 8227 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8228 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { | 
|  | 8229 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); | 
|  | 8230 | const sp<FakeWindowHandle>& w = | 
| Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8231 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8232 | OPACITY_ABOVE_THRESHOLD); | 
|  | 8233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8234 |  | 
|  | 8235 | touch(); | 
|  | 8236 |  | 
|  | 8237 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8238 | } | 
|  | 8239 |  | 
|  | 8240 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8241 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { | 
|  | 8242 | const sp<FakeWindowHandle>& w1 = | 
|  | 8243 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 8244 | OPACITY_BELOW_THRESHOLD); | 
|  | 8245 | const sp<FakeWindowHandle>& w2 = | 
|  | 8246 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, | 
|  | 8247 | OPACITY_BELOW_THRESHOLD); | 
|  | 8248 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); | 
|  | 8249 |  | 
|  | 8250 | touch(); | 
|  | 8251 |  | 
|  | 8252 | mTouchWindow->assertNoEvents(); | 
|  | 8253 | } | 
|  | 8254 |  | 
|  | 8255 | /** | 
|  | 8256 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the | 
|  | 8257 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold | 
|  | 8258 | * (which alone would result in allowing touches) does not affect the blocking behavior. | 
|  | 8259 | */ | 
|  | 8260 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8261 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { | 
|  | 8262 | const sp<FakeWindowHandle>& wB = | 
|  | 8263 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, | 
|  | 8264 | OPACITY_BELOW_THRESHOLD); | 
|  | 8265 | const sp<FakeWindowHandle>& wC = | 
|  | 8266 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, | 
|  | 8267 | OPACITY_BELOW_THRESHOLD); | 
|  | 8268 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); | 
|  | 8269 |  | 
|  | 8270 | touch(); | 
|  | 8271 |  | 
|  | 8272 | mTouchWindow->assertNoEvents(); | 
|  | 8273 | } | 
|  | 8274 |  | 
|  | 8275 | /** | 
|  | 8276 | * This test is testing that a window from a different UID but with same application token doesn't | 
|  | 8277 | * block the touch. Apps can share the application token for close UI collaboration for example. | 
|  | 8278 | */ | 
|  | 8279 | TEST_F(InputDispatcherUntrustedTouchesTest, | 
|  | 8280 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { | 
|  | 8281 | const sp<FakeWindowHandle>& w = | 
|  | 8282 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); | 
|  | 8283 | w->setApplicationToken(mTouchWindow->getApplicationToken()); | 
| Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8284 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); | 
|  | 8285 |  | 
|  | 8286 | touch(); | 
|  | 8287 |  | 
|  | 8288 | mTouchWindow->consumeAnyMotionDown(); | 
|  | 8289 | } | 
|  | 8290 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8291 | class InputDispatcherDragTests : public InputDispatcherTest { | 
|  | 8292 | protected: | 
|  | 8293 | std::shared_ptr<FakeApplicationHandle> mApp; | 
|  | 8294 | sp<FakeWindowHandle> mWindow; | 
|  | 8295 | sp<FakeWindowHandle> mSecondWindow; | 
|  | 8296 | sp<FakeWindowHandle> mDragWindow; | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8297 | sp<FakeWindowHandle> mSpyWindow; | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8298 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. | 
|  | 8299 | static constexpr int32_t MOUSE_POINTER_ID = 1; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8300 |  | 
|  | 8301 | void SetUp() override { | 
|  | 8302 | InputDispatcherTest::SetUp(); | 
|  | 8303 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8304 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8305 | mWindow->setFrame(Rect(0, 0, 100, 100)); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8306 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8307 | mSecondWindow = | 
|  | 8308 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8309 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8310 |  | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8311 | mSpyWindow = | 
|  | 8312 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8313 | mSpyWindow->setSpy(true); | 
|  | 8314 | mSpyWindow->setTrustedOverlay(true); | 
|  | 8315 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); | 
|  | 8316 |  | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8317 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8318 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8319 | } | 
|  | 8320 |  | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8321 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { | 
|  | 8322 | switch (fromSource) { | 
|  | 8323 | case AINPUT_SOURCE_TOUCHSCREEN: | 
|  | 8324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8325 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8326 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 8327 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8328 | break; | 
|  | 8329 | case AINPUT_SOURCE_STYLUS: | 
|  | 8330 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8331 | injectMotionEvent( | 
|  | 8332 | mDispatcher, | 
|  | 8333 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 8334 | AINPUT_SOURCE_STYLUS) | 
|  | 8335 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8336 | .pointer(PointerBuilder(0, ToolType::STYLUS) | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8337 | .x(50) | 
|  | 8338 | .y(50)) | 
|  | 8339 | .build())); | 
|  | 8340 | break; | 
|  | 8341 | case AINPUT_SOURCE_MOUSE: | 
|  | 8342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8343 | injectMotionEvent( | 
|  | 8344 | mDispatcher, | 
|  | 8345 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) | 
|  | 8346 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 8347 | .pointer(PointerBuilder(MOUSE_POINTER_ID, | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8348 | ToolType::MOUSE) | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8349 | .x(50) | 
|  | 8350 | .y(50)) | 
|  | 8351 | .build())); | 
|  | 8352 | break; | 
|  | 8353 | default: | 
|  | 8354 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; | 
|  | 8355 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8356 |  | 
|  | 8357 | // Window should receive motion event. | 
|  | 8358 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8359 | // Spy window should also receive motion event | 
|  | 8360 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8361 | } | 
|  | 8362 |  | 
|  | 8363 | // Start performing drag, we will create a drag window and transfer touch to it. | 
|  | 8364 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. | 
|  | 8365 | // Returns true on success. | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8366 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8367 | if (sendDown) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8368 | injectDown(fromSource); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8369 | } | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8370 |  | 
|  | 8371 | // The drag window covers the entire display | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8372 | mDragWindow = | 
|  | 8373 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8374 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8375 | mDispatcher->setInputWindows( | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8376 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8377 |  | 
|  | 8378 | // Transfer touch focus to the drag window | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8379 | bool transferred = | 
|  | 8380 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8381 | /*isDragDrop=*/true); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8382 | if (transferred) { | 
|  | 8383 | mWindow->consumeMotionCancel(); | 
|  | 8384 | mDragWindow->consumeMotionDown(); | 
|  | 8385 | } | 
|  | 8386 | return transferred; | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8387 | } | 
|  | 8388 | }; | 
|  | 8389 |  | 
|  | 8390 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8391 | startDrag(); | 
| arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8392 |  | 
|  | 8393 | // Move on window. | 
|  | 8394 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8395 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8396 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 8397 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8398 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8399 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 8400 | mSecondWindow->assertNoEvents(); | 
|  | 8401 |  | 
|  | 8402 | // Move to another window. | 
|  | 8403 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8404 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8405 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 8406 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8407 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8408 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 8409 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 8410 |  | 
|  | 8411 | // Move back to original window. | 
|  | 8412 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8413 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8414 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 8415 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8416 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8417 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 8418 | mSecondWindow->consumeDragEvent(true, -50, 50); | 
|  | 8419 |  | 
|  | 8420 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8421 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 8422 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8423 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 8424 | mWindow->assertNoEvents(); | 
|  | 8425 | mSecondWindow->assertNoEvents(); | 
|  | 8426 | } | 
|  | 8427 |  | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8428 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8429 | startDrag(); | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8430 |  | 
|  | 8431 | // No cancel event after drag start | 
|  | 8432 | mSpyWindow->assertNoEvents(); | 
|  | 8433 |  | 
|  | 8434 | const MotionEvent secondFingerDownEvent = | 
|  | 8435 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 8436 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8437 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) | 
|  | 8438 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) | 
| Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8439 | .build(); | 
|  | 8440 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8441 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 8442 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 8443 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8444 |  | 
|  | 8445 | // Receives cancel for first pointer after next pointer down | 
|  | 8446 | mSpyWindow->consumeMotionCancel(); | 
|  | 8447 | mSpyWindow->consumeMotionDown(); | 
|  | 8448 |  | 
|  | 8449 | mSpyWindow->assertNoEvents(); | 
|  | 8450 | } | 
|  | 8451 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8452 | TEST_F(InputDispatcherDragTests, DragAndDrop) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8453 | startDrag(); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8454 |  | 
|  | 8455 | // Move on window. | 
|  | 8456 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8457 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8458 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 8459 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8460 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8461 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 8462 | mSecondWindow->assertNoEvents(); | 
|  | 8463 |  | 
|  | 8464 | // Move to another window. | 
|  | 8465 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8466 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8467 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 8468 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8469 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8470 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 8471 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 8472 |  | 
|  | 8473 | // drop to another window. | 
|  | 8474 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8475 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 8476 | {150, 50})) | 
|  | 8477 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8478 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8479 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8480 | mWindow->assertNoEvents(); | 
|  | 8481 | mSecondWindow->assertNoEvents(); | 
|  | 8482 | } | 
|  | 8483 |  | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8484 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8485 | startDrag(true, AINPUT_SOURCE_STYLUS); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8486 |  | 
|  | 8487 | // Move on window and keep button pressed. | 
|  | 8488 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8489 | injectMotionEvent(mDispatcher, | 
|  | 8490 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 8491 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8492 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8493 | .build())) | 
|  | 8494 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8495 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8496 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 8497 | mSecondWindow->assertNoEvents(); | 
|  | 8498 |  | 
|  | 8499 | // Move to another window and release button, expect to drop item. | 
|  | 8500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8501 | injectMotionEvent(mDispatcher, | 
|  | 8502 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) | 
|  | 8503 | .buttonState(0) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8504 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8505 | .build())) | 
|  | 8506 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8507 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8508 | mWindow->assertNoEvents(); | 
|  | 8509 | mSecondWindow->assertNoEvents(); | 
| Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8510 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8511 |  | 
|  | 8512 | // nothing to the window. | 
|  | 8513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8514 | injectMotionEvent(mDispatcher, | 
|  | 8515 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) | 
|  | 8516 | .buttonState(0) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8517 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) | 
| arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8518 | .build())) | 
|  | 8519 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8520 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 8521 | mWindow->assertNoEvents(); | 
|  | 8522 | mSecondWindow->assertNoEvents(); | 
|  | 8523 | } | 
|  | 8524 |  | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8525 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8526 | startDrag(); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8527 |  | 
|  | 8528 | // Set second window invisible. | 
|  | 8529 | mSecondWindow->setVisible(false); | 
|  | 8530 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); | 
|  | 8531 |  | 
|  | 8532 | // Move on window. | 
|  | 8533 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8534 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8535 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 8536 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8537 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8538 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 8539 | mSecondWindow->assertNoEvents(); | 
|  | 8540 |  | 
|  | 8541 | // Move to another window. | 
|  | 8542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8543 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8544 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 8545 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8546 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8547 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 8548 | mSecondWindow->assertNoEvents(); | 
|  | 8549 |  | 
|  | 8550 | // drop to another window. | 
|  | 8551 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8552 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 8553 | {150, 50})) | 
|  | 8554 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8555 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8556 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); | 
| Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8557 | mWindow->assertNoEvents(); | 
|  | 8558 | mSecondWindow->assertNoEvents(); | 
|  | 8559 | } | 
|  | 8560 |  | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8561 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8562 | // Ensure window could track pointerIds if it didn't support split touch. | 
|  | 8563 | mWindow->setPreventSplitting(true); | 
|  | 8564 |  | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8565 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8566 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 8567 | {50, 50})) | 
|  | 8568 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8569 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 8570 |  | 
|  | 8571 | const MotionEvent secondFingerDownEvent = | 
|  | 8572 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 8573 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 8574 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8575 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) | 
|  | 8576 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8577 | .build(); | 
|  | 8578 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8579 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 8580 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 8581 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8582 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8583 |  | 
|  | 8584 | // Should not perform drag and drop when window has multi fingers. | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8585 | ASSERT_FALSE(startDrag(false)); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8586 | } | 
|  | 8587 |  | 
|  | 8588 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { | 
|  | 8589 | // First down on second window. | 
|  | 8590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8591 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 8592 | {150, 50})) | 
|  | 8593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8594 |  | 
|  | 8595 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 8596 |  | 
|  | 8597 | // Second down on first window. | 
|  | 8598 | const MotionEvent secondFingerDownEvent = | 
|  | 8599 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 8600 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 8601 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8602 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) | 
|  | 8603 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8604 | .build(); | 
|  | 8605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8606 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 8607 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 8608 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8609 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 8610 |  | 
|  | 8611 | // Perform drag and drop from first window. | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8612 | ASSERT_TRUE(startDrag(false)); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8613 |  | 
|  | 8614 | // Move on window. | 
|  | 8615 | const MotionEvent secondFingerMoveEvent = | 
|  | 8616 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 8617 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8618 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) | 
|  | 8619 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8620 | .build(); | 
|  | 8621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8622 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, | 
|  | 8623 | InputEventInjectionSync::WAIT_FOR_RESULT)); | 
|  | 8624 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8625 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 8626 | mSecondWindow->consumeMotionMove(); | 
|  | 8627 |  | 
|  | 8628 | // Release the drag pointer should perform drop. | 
|  | 8629 | const MotionEvent secondFingerUpEvent = | 
|  | 8630 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 8631 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8632 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) | 
|  | 8633 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8634 | .build(); | 
|  | 8635 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8636 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, | 
|  | 8637 | InputEventInjectionSync::WAIT_FOR_RESULT)); | 
|  | 8638 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8639 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); | 
| Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8640 | mWindow->assertNoEvents(); | 
|  | 8641 | mSecondWindow->consumeMotionMove(); | 
|  | 8642 | } | 
|  | 8643 |  | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8644 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8645 | startDrag(); | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8646 |  | 
|  | 8647 | // Update window of second display. | 
|  | 8648 | sp<FakeWindowHandle> windowInSecondary = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8649 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8650 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
|  | 8651 |  | 
|  | 8652 | // Let second display has a touch state. | 
|  | 8653 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8654 | injectMotionEvent(mDispatcher, | 
|  | 8655 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, | 
|  | 8656 | AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 8657 | .displayId(SECOND_DISPLAY_ID) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8658 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8659 | .build())); | 
| Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8660 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8661 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8662 | // Update window again. | 
|  | 8663 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); | 
|  | 8664 |  | 
|  | 8665 | // Move on window. | 
|  | 8666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8667 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8668 | ADISPLAY_ID_DEFAULT, {50, 50})) | 
|  | 8669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8670 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8671 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 8672 | mSecondWindow->assertNoEvents(); | 
|  | 8673 |  | 
|  | 8674 | // Move to another window. | 
|  | 8675 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8676 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8677 | ADISPLAY_ID_DEFAULT, {150, 50})) | 
|  | 8678 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8679 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8680 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 8681 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 8682 |  | 
|  | 8683 | // drop to another window. | 
|  | 8684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8685 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 8686 | {150, 50})) | 
|  | 8687 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8688 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8689 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); | 
| Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8690 | mWindow->assertNoEvents(); | 
|  | 8691 | mSecondWindow->assertNoEvents(); | 
|  | 8692 | } | 
|  | 8693 |  | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8694 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { | 
|  | 8695 | startDrag(true, AINPUT_SOURCE_MOUSE); | 
|  | 8696 | // Move on window. | 
|  | 8697 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8698 | injectMotionEvent(mDispatcher, | 
|  | 8699 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) | 
|  | 8700 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 8701 | .pointer(PointerBuilder(MOUSE_POINTER_ID, | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8702 | ToolType::MOUSE) | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8703 | .x(50) | 
|  | 8704 | .y(50)) | 
|  | 8705 | .build())) | 
|  | 8706 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8707 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8708 | mWindow->consumeDragEvent(false, 50, 50); | 
|  | 8709 | mSecondWindow->assertNoEvents(); | 
|  | 8710 |  | 
|  | 8711 | // Move to another window. | 
|  | 8712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8713 | injectMotionEvent(mDispatcher, | 
|  | 8714 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) | 
|  | 8715 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) | 
|  | 8716 | .pointer(PointerBuilder(MOUSE_POINTER_ID, | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8717 | ToolType::MOUSE) | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8718 | .x(150) | 
|  | 8719 | .y(50)) | 
|  | 8720 | .build())) | 
|  | 8721 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8722 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); | 
|  | 8723 | mWindow->consumeDragEvent(true, 150, 50); | 
|  | 8724 | mSecondWindow->consumeDragEvent(false, 50, 50); | 
|  | 8725 |  | 
|  | 8726 | // drop to another window. | 
|  | 8727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8728 | injectMotionEvent(mDispatcher, | 
|  | 8729 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) | 
|  | 8730 | .buttonState(0) | 
|  | 8731 | .pointer(PointerBuilder(MOUSE_POINTER_ID, | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8732 | ToolType::MOUSE) | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8733 | .x(150) | 
|  | 8734 | .y(50)) | 
|  | 8735 | .build())) | 
|  | 8736 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8737 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
| Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8738 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); | 
| Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8739 | mWindow->assertNoEvents(); | 
|  | 8740 | mSecondWindow->assertNoEvents(); | 
|  | 8741 | } | 
|  | 8742 |  | 
| Linnan Li | a4659fc | 2023-07-14 14:36:22 +0800 | [diff] [blame^] | 8743 | /** | 
|  | 8744 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag | 
|  | 8745 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. | 
|  | 8746 | */ | 
|  | 8747 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { | 
|  | 8748 | // Down on second window | 
|  | 8749 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8750 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 8751 | {150, 50})) | 
|  | 8752 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8753 |  | 
|  | 8754 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); | 
|  | 8755 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); | 
|  | 8756 |  | 
|  | 8757 | // Down on first window | 
|  | 8758 | const MotionEvent secondFingerDownEvent = | 
|  | 8759 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 8760 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 8761 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) | 
|  | 8762 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) | 
|  | 8763 | .build(); | 
|  | 8764 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8765 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 8766 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 8767 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8768 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); | 
|  | 8769 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); | 
|  | 8770 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); | 
|  | 8771 |  | 
|  | 8772 | // Start drag on first window | 
|  | 8773 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); | 
|  | 8774 |  | 
|  | 8775 | // Trigger cancel | 
|  | 8776 | mDispatcher->cancelCurrentTouch(); | 
|  | 8777 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); | 
|  | 8778 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel()); | 
|  | 8779 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); | 
|  | 8780 |  | 
|  | 8781 | ASSERT_TRUE(mDispatcher->waitForIdle()); | 
|  | 8782 | // The D&D finished with nullptr | 
|  | 8783 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); | 
|  | 8784 |  | 
|  | 8785 | // Remove drag window | 
|  | 8786 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); | 
|  | 8787 |  | 
|  | 8788 | // Inject a simple gesture, ensure dispatcher not crashed | 
|  | 8789 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8790 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 8791 | PointF{50, 50})) | 
|  | 8792 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8793 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); | 
|  | 8794 |  | 
|  | 8795 | const MotionEvent moveEvent = | 
|  | 8796 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 8797 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 8798 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) | 
|  | 8799 | .build(); | 
|  | 8800 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8801 | injectMotionEvent(mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, | 
|  | 8802 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 8803 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8804 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); | 
|  | 8805 |  | 
|  | 8806 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 8807 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 8808 | {50, 50})) | 
|  | 8809 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 8810 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); | 
|  | 8811 | } | 
|  | 8812 |  | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8813 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; | 
|  | 8814 |  | 
|  | 8815 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { | 
|  | 8816 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8817 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 8818 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8819 | window->setDropInput(true); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8820 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 8821 | window->setFocusable(true); | 
|  | 8822 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 8823 | setFocusedWindow(window); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8824 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8825 |  | 
|  | 8826 | // With the flag set, window should not get any input | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8827 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8828 | window->assertNoEvents(); | 
|  | 8829 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8830 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 8831 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8832 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8833 | ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8834 | window->assertNoEvents(); | 
|  | 8835 |  | 
|  | 8836 | // With the flag cleared, the window should get input | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8837 | window->setDropInput(false); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8838 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 8839 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8840 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8841 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 8842 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8843 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 8844 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8845 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 8846 | window->assertNoEvents(); | 
|  | 8847 | } | 
|  | 8848 |  | 
|  | 8849 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { | 
|  | 8850 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = | 
|  | 8851 | std::make_shared<FakeApplicationHandle>(); | 
|  | 8852 | sp<FakeWindowHandle> obscuringWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8853 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", | 
|  | 8854 | ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8855 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8856 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8857 | obscuringWindow->setTouchable(false); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8858 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8859 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 8860 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8861 | window->setDropInputIfObscured(true); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8862 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8863 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 8864 | window->setFocusable(true); | 
|  | 8865 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 8866 | setFocusedWindow(window); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8867 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8868 |  | 
|  | 8869 | // With the flag set, window should not get any input | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8870 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8871 | window->assertNoEvents(); | 
|  | 8872 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8873 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 8874 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8875 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8876 | ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8877 | window->assertNoEvents(); | 
|  | 8878 |  | 
|  | 8879 | // With the flag cleared, the window should get input | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8880 | window->setDropInputIfObscured(false); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8881 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 8882 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8883 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8884 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 8885 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8886 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 8887 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8888 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); | 
|  | 8889 | window->assertNoEvents(); | 
|  | 8890 | } | 
|  | 8891 |  | 
|  | 8892 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { | 
|  | 8893 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = | 
|  | 8894 | std::make_shared<FakeApplicationHandle>(); | 
|  | 8895 | sp<FakeWindowHandle> obscuringWindow = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8896 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", | 
|  | 8897 | ADISPLAY_ID_DEFAULT); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8898 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8899 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8900 | obscuringWindow->setTouchable(false); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8901 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8902 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 8903 | "Test window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8904 | window->setDropInputIfObscured(true); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8905 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8906 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 8907 | window->setFocusable(true); | 
|  | 8908 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); | 
|  | 8909 | setFocusedWindow(window); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8910 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8911 |  | 
|  | 8912 | // With the flag set, window should not get any input | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8913 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8914 | window->assertNoEvents(); | 
|  | 8915 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8916 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 8917 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8918 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 8919 | ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8920 | window->assertNoEvents(); | 
|  | 8921 |  | 
|  | 8922 | // When the window is no longer obscured because it went on top, it should get input | 
|  | 8923 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); | 
|  | 8924 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8925 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8926 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); | 
|  | 8927 |  | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8928 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, | 
|  | 8929 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); | 
| Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8930 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 8931 | window->assertNoEvents(); | 
|  | 8932 | } | 
|  | 8933 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8934 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { | 
|  | 8935 | protected: | 
|  | 8936 | std::shared_ptr<FakeApplicationHandle> mApp; | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8937 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8938 | sp<FakeWindowHandle> mWindow; | 
|  | 8939 | sp<FakeWindowHandle> mSecondWindow; | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8940 | sp<FakeWindowHandle> mThirdWindow; | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8941 |  | 
|  | 8942 | void SetUp() override { | 
|  | 8943 | InputDispatcherTest::SetUp(); | 
|  | 8944 |  | 
|  | 8945 | mApp = std::make_shared<FakeApplicationHandle>(); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8946 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8947 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8948 | mWindow->setFocusable(true); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8949 | setFocusedWindow(mWindow); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8950 | mSecondWindow = | 
|  | 8951 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8952 | mSecondWindow->setFocusable(true); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8953 | mThirdWindow = | 
|  | 8954 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, | 
|  | 8955 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); | 
|  | 8956 | mThirdWindow->setFocusable(true); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8957 |  | 
|  | 8958 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8959 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, | 
|  | 8960 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); | 
|  | 8961 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8962 | mWindow->consumeFocusEvent(true); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8963 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8964 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8965 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8966 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8967 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); | 
|  | 8968 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8969 | mThirdWindow->assertNoEvents(); | 
|  | 8970 | } | 
|  | 8971 |  | 
|  | 8972 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. | 
|  | 8973 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8974 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8975 | SECOND_DISPLAY_ID)) { | 
|  | 8976 | mWindow->assertNoEvents(); | 
|  | 8977 | mSecondWindow->assertNoEvents(); | 
|  | 8978 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8979 | } | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8980 | } | 
|  | 8981 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8982 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8983 | bool hasPermission) { | 
| Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8984 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, | 
|  | 8985 | ADISPLAY_ID_DEFAULT)); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8986 | mWindow->consumeTouchModeEvent(inTouchMode); | 
|  | 8987 | mSecondWindow->consumeTouchModeEvent(inTouchMode); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8988 | mThirdWindow->assertNoEvents(); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8989 | } | 
|  | 8990 | }; | 
|  | 8991 |  | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8992 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8993 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8994 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, | 
|  | 8995 | windowInfo.ownerPid, windowInfo.ownerUid, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8996 | /* hasPermission=*/false); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8997 | } | 
|  | 8998 |  | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8999 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { | 
|  | 9000 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9001 | gui::Pid ownerPid = windowInfo.ownerPid; | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9002 | gui::Uid ownerUid = windowInfo.ownerUid; | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9003 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9004 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9005 | ownerUid, /*hasPermission=*/false, | 
| Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 9006 | ADISPLAY_ID_DEFAULT)); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9007 | mWindow->assertNoEvents(); | 
|  | 9008 | mSecondWindow->assertNoEvents(); | 
|  | 9009 | } | 
|  | 9010 |  | 
|  | 9011 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { | 
|  | 9012 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9013 | gui::Pid ownerPid = windowInfo.ownerPid; | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9014 | gui::Uid ownerUid = windowInfo.ownerUid; | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9015 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9016 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9017 | ownerUid, /*hasPermission=*/true); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9018 | } | 
|  | 9019 |  | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9020 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { | 
| Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 9021 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
| Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 9022 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, | 
|  | 9023 | windowInfo.ownerPid, windowInfo.ownerUid, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9024 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); | 
| Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 9025 | mWindow->assertNoEvents(); | 
|  | 9026 | mSecondWindow->assertNoEvents(); | 
|  | 9027 | } | 
|  | 9028 |  | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9029 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { | 
|  | 9030 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); | 
|  | 9031 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, | 
|  | 9032 | windowInfo.ownerPid, windowInfo.ownerUid, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9033 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); | 
| Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 9034 | mWindow->assertNoEvents(); | 
|  | 9035 | mSecondWindow->assertNoEvents(); | 
|  | 9036 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); | 
|  | 9037 | } | 
|  | 9038 |  | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 9039 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { | 
|  | 9040 | // Interact with the window first. | 
|  | 9041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) | 
|  | 9042 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9043 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); | 
|  | 9044 |  | 
|  | 9045 | // Then remove focus. | 
|  | 9046 | mWindow->setFocusable(false); | 
|  | 9047 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); | 
|  | 9048 |  | 
|  | 9049 | // Assert that caller can switch touch mode by owning one of the last interacted window. | 
|  | 9050 | const WindowInfo& windowInfo = *mWindow->getInfo(); | 
|  | 9051 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, | 
|  | 9052 | windowInfo.ownerPid, windowInfo.ownerUid, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9053 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); | 
| Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 9054 | } | 
|  | 9055 |  | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9056 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { | 
|  | 9057 | public: | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9058 | sp<FakeWindowHandle> createSpy() { | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9059 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 9060 | std::make_shared<FakeApplicationHandle>(); | 
|  | 9061 | std::string name = "Fake Spy "; | 
|  | 9062 | name += std::to_string(mSpyCount++); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9063 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, | 
|  | 9064 | name.c_str(), ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9065 | spy->setSpy(true); | 
| Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9066 | spy->setTrustedOverlay(true); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9067 | return spy; | 
|  | 9068 | } | 
|  | 9069 |  | 
|  | 9070 | sp<FakeWindowHandle> createForeground() { | 
|  | 9071 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 9072 | std::make_shared<FakeApplicationHandle>(); | 
|  | 9073 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9074 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", | 
|  | 9075 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9076 | window->setFocusable(true); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9077 | return window; | 
|  | 9078 | } | 
|  | 9079 |  | 
|  | 9080 | private: | 
|  | 9081 | int mSpyCount{0}; | 
|  | 9082 | }; | 
|  | 9083 |  | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9084 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9085 | /** | 
| Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9086 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. | 
|  | 9087 | */ | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9088 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { | 
|  | 9089 | ScopedSilentDeath _silentDeath; | 
|  | 9090 |  | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9091 | auto spy = createSpy(); | 
| Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9092 | spy->setTrustedOverlay(false); | 
|  | 9093 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), | 
|  | 9094 | ".* not a trusted overlay"); | 
|  | 9095 | } | 
|  | 9096 |  | 
|  | 9097 | /** | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9098 | * Input injection into a display with a spy window but no foreground windows should succeed. | 
|  | 9099 | */ | 
|  | 9100 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9101 | auto spy = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9102 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); | 
|  | 9103 |  | 
|  | 9104 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9105 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 9106 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9107 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 9108 | } | 
|  | 9109 |  | 
|  | 9110 | /** | 
|  | 9111 | * Verify the order in which different input windows receive events. The touched foreground window | 
|  | 9112 | * (if there is one) should always receive the event first. When there are multiple spy windows, the | 
|  | 9113 | * spy windows will receive the event according to their Z-order, where the top-most spy window will | 
|  | 9114 | * receive events before ones belows it. | 
|  | 9115 | * | 
|  | 9116 | * Here, we set up a scenario with four windows in the following Z order from the top: | 
|  | 9117 | *    spy1, spy2, window, spy3. | 
|  | 9118 | * We then inject an event and verify that the foreground "window" receives it first, followed by | 
|  | 9119 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground | 
|  | 9120 | * window. | 
|  | 9121 | */ | 
|  | 9122 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { | 
|  | 9123 | auto window = createForeground(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9124 | auto spy1 = createSpy(); | 
|  | 9125 | auto spy2 = createSpy(); | 
|  | 9126 | auto spy3 = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9127 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); | 
|  | 9128 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; | 
|  | 9129 | const size_t numChannels = channels.size(); | 
|  | 9130 |  | 
| Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 9131 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9132 | if (!epollFd.ok()) { | 
|  | 9133 | FAIL() << "Failed to create epoll fd"; | 
|  | 9134 | } | 
|  | 9135 |  | 
|  | 9136 | for (size_t i = 0; i < numChannels; i++) { | 
|  | 9137 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; | 
|  | 9138 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { | 
|  | 9139 | FAIL() << "Failed to add fd to epoll"; | 
|  | 9140 | } | 
|  | 9141 | } | 
|  | 9142 |  | 
|  | 9143 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9144 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 9145 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9146 |  | 
|  | 9147 | std::vector<size_t> eventOrder; | 
|  | 9148 | std::vector<struct epoll_event> events(numChannels); | 
|  | 9149 | for (;;) { | 
|  | 9150 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), | 
|  | 9151 | (100ms).count()); | 
|  | 9152 | if (nFds < 0) { | 
|  | 9153 | FAIL() << "Failed to call epoll_wait"; | 
|  | 9154 | } | 
|  | 9155 | if (nFds == 0) { | 
|  | 9156 | break; // epoll_wait timed out | 
|  | 9157 | } | 
|  | 9158 | for (int i = 0; i < nFds; i++) { | 
| Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 9159 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); | 
| Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 9160 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9161 | channels[i]->consumeMotionDown(); | 
|  | 9162 | } | 
|  | 9163 | } | 
|  | 9164 |  | 
|  | 9165 | // Verify the order in which the events were received. | 
|  | 9166 | EXPECT_EQ(3u, eventOrder.size()); | 
|  | 9167 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window | 
|  | 9168 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 | 
|  | 9169 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 | 
|  | 9170 | } | 
|  | 9171 |  | 
|  | 9172 | /** | 
|  | 9173 | * A spy window using the NOT_TOUCHABLE flag does not receive events. | 
|  | 9174 | */ | 
|  | 9175 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { | 
|  | 9176 | auto window = createForeground(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9177 | auto spy = createSpy(); | 
|  | 9178 | spy->setTouchable(false); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9179 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9180 |  | 
|  | 9181 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9182 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 9183 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9184 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 9185 | spy->assertNoEvents(); | 
|  | 9186 | } | 
|  | 9187 |  | 
|  | 9188 | /** | 
|  | 9189 | * A spy window will only receive gestures that originate within its touchable region. Gestures that | 
|  | 9190 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched | 
|  | 9191 | * to the window. | 
|  | 9192 | */ | 
|  | 9193 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { | 
|  | 9194 | auto window = createForeground(); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9195 | auto spy = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9196 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); | 
|  | 9197 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9198 |  | 
|  | 9199 | // Inject an event outside the spy window's touchable region. | 
|  | 9200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9201 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 9202 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9203 | window->consumeMotionDown(); | 
|  | 9204 | spy->assertNoEvents(); | 
|  | 9205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9206 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 9207 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9208 | window->consumeMotionUp(); | 
|  | 9209 | spy->assertNoEvents(); | 
|  | 9210 |  | 
|  | 9211 | // Inject an event inside the spy window's touchable region. | 
|  | 9212 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9213 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9214 | {5, 10})) | 
|  | 9215 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9216 | window->consumeMotionDown(); | 
|  | 9217 | spy->consumeMotionDown(); | 
|  | 9218 | } | 
|  | 9219 |  | 
|  | 9220 | /** | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9221 | * 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] | 9222 | * 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] | 9223 | */ | 
|  | 9224 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { | 
|  | 9225 | auto window = createForeground(); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9226 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9227 | auto spy = createSpy(); | 
|  | 9228 | spy->setWatchOutsideTouch(true); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9229 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9230 | spy->setFrame(Rect{0, 0, 20, 20}); | 
|  | 9231 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9232 |  | 
|  | 9233 | // Inject an event outside the spy window's frame and touchable region. | 
|  | 9234 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9235 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9236 | {100, 200})) | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9237 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9238 | window->consumeMotionDown(); | 
| Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9239 | spy->consumeMotionOutsideWithZeroedCoords(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9240 | } | 
|  | 9241 |  | 
|  | 9242 | /** | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9243 | * Even when a spy window spans over multiple foreground windows, the spy should receive all | 
|  | 9244 | * pointers that are down within its bounds. | 
|  | 9245 | */ | 
|  | 9246 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { | 
|  | 9247 | auto windowLeft = createForeground(); | 
|  | 9248 | windowLeft->setFrame({0, 0, 100, 200}); | 
|  | 9249 | auto windowRight = createForeground(); | 
|  | 9250 | windowRight->setFrame({100, 0, 200, 200}); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9251 | auto spy = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9252 | spy->setFrame({0, 0, 200, 200}); | 
|  | 9253 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); | 
|  | 9254 |  | 
|  | 9255 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9256 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9257 | {50, 50})) | 
|  | 9258 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9259 | windowLeft->consumeMotionDown(); | 
|  | 9260 | spy->consumeMotionDown(); | 
|  | 9261 |  | 
|  | 9262 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9263 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9264 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9265 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) | 
|  | 9266 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9267 | .build(); | 
|  | 9268 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9269 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9270 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 9271 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9272 | windowRight->consumeMotionDown(); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9273 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9274 | } | 
|  | 9275 |  | 
|  | 9276 | /** | 
|  | 9277 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the | 
|  | 9278 | * the spy should receive the second pointer with ACTION_DOWN. | 
|  | 9279 | */ | 
|  | 9280 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { | 
|  | 9281 | auto window = createForeground(); | 
|  | 9282 | window->setFrame({0, 0, 200, 200}); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9283 | auto spyRight = createSpy(); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9284 | spyRight->setFrame({100, 0, 200, 200}); | 
|  | 9285 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); | 
|  | 9286 |  | 
|  | 9287 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9288 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9289 | {50, 50})) | 
|  | 9290 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9291 | window->consumeMotionDown(); | 
|  | 9292 | spyRight->assertNoEvents(); | 
|  | 9293 |  | 
|  | 9294 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9295 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9296 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9297 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) | 
|  | 9298 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9299 | .build(); | 
|  | 9300 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9301 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9302 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 9303 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9304 | window->consumeMotionPointerDown(/*pointerIndex=*/1); | 
| Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9305 | spyRight->consumeMotionDown(); | 
|  | 9306 | } | 
|  | 9307 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9308 | /** | 
|  | 9309 | * The spy window should not be able to affect whether or not touches are split. Only the foreground | 
|  | 9310 | * windows should be allowed to control split touch. | 
|  | 9311 | */ | 
|  | 9312 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9313 | // 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] | 9314 | // because a foreground window has not disabled splitting. | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9315 | auto spy = createSpy(); | 
| Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9316 | spy->setPreventSplitting(true); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9317 |  | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9318 | auto window = createForeground(); | 
|  | 9319 | window->setFrame(Rect(0, 0, 100, 100)); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9320 |  | 
|  | 9321 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9322 |  | 
|  | 9323 | // First finger down, no window touched. | 
|  | 9324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9325 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9326 | {100, 200})) | 
|  | 9327 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9328 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 9329 | window->assertNoEvents(); | 
|  | 9330 |  | 
|  | 9331 | // Second finger down on window, the window should receive touch down. | 
|  | 9332 | const MotionEvent secondFingerDownEvent = | 
| Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9333 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9334 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 9335 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9336 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) | 
|  | 9337 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9338 | .build(); | 
|  | 9339 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9340 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9341 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 9342 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9343 |  | 
|  | 9344 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9345 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9346 | } | 
|  | 9347 |  | 
|  | 9348 | /** | 
|  | 9349 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows | 
|  | 9350 | * do not receive key events. | 
|  | 9351 | */ | 
|  | 9352 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9353 | auto spy = createSpy(); | 
| Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9354 | spy->setFocusable(false); | 
|  | 9355 |  | 
|  | 9356 | auto window = createForeground(); | 
|  | 9357 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9358 | setFocusedWindow(window); | 
|  | 9359 | window->consumeFocusEvent(true); | 
|  | 9360 |  | 
|  | 9361 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) | 
|  | 9362 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9363 | window->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 9364 |  | 
|  | 9365 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) | 
|  | 9366 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9367 | window->consumeKeyUp(ADISPLAY_ID_NONE); | 
|  | 9368 |  | 
|  | 9369 | spy->assertNoEvents(); | 
|  | 9370 | } | 
|  | 9371 |  | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9372 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; | 
|  | 9373 |  | 
|  | 9374 | /** | 
|  | 9375 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that | 
|  | 9376 | * are currently sent to any other windows - including other spy windows - will also be cancelled. | 
|  | 9377 | */ | 
|  | 9378 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { | 
|  | 9379 | auto window = createForeground(); | 
|  | 9380 | auto spy1 = createSpy(); | 
|  | 9381 | auto spy2 = createSpy(); | 
|  | 9382 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); | 
|  | 9383 |  | 
|  | 9384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9385 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 9386 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9387 | window->consumeMotionDown(); | 
|  | 9388 | spy1->consumeMotionDown(); | 
|  | 9389 | spy2->consumeMotionDown(); | 
|  | 9390 |  | 
|  | 9391 | // Pilfer pointers from the second spy window. | 
|  | 9392 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); | 
|  | 9393 | spy2->assertNoEvents(); | 
|  | 9394 | spy1->consumeMotionCancel(); | 
|  | 9395 | window->consumeMotionCancel(); | 
|  | 9396 |  | 
|  | 9397 | // The rest of the gesture should only be sent to the second spy window. | 
|  | 9398 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9399 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 9400 | ADISPLAY_ID_DEFAULT)) | 
|  | 9401 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9402 | spy2->consumeMotionMove(); | 
|  | 9403 | spy1->assertNoEvents(); | 
|  | 9404 | window->assertNoEvents(); | 
|  | 9405 | } | 
|  | 9406 |  | 
|  | 9407 | /** | 
|  | 9408 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed | 
|  | 9409 | * in the middle of the gesture. | 
|  | 9410 | */ | 
|  | 9411 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { | 
|  | 9412 | auto window = createForeground(); | 
|  | 9413 | auto spy = createSpy(); | 
|  | 9414 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9415 |  | 
|  | 9416 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9417 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 9418 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9419 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 9420 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); | 
|  | 9421 |  | 
|  | 9422 | window->releaseChannel(); | 
|  | 9423 |  | 
|  | 9424 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 9425 |  | 
|  | 9426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9427 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) | 
|  | 9428 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9429 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); | 
|  | 9430 | } | 
|  | 9431 |  | 
|  | 9432 | /** | 
|  | 9433 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to | 
|  | 9434 | * the spy, but not to any other windows. | 
|  | 9435 | */ | 
|  | 9436 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { | 
|  | 9437 | auto spy = createSpy(); | 
|  | 9438 | auto window = createForeground(); | 
|  | 9439 |  | 
|  | 9440 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9441 |  | 
|  | 9442 | // First finger down on the window and the spy. | 
|  | 9443 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9444 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9445 | {100, 200})) | 
|  | 9446 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9447 | spy->consumeMotionDown(); | 
|  | 9448 | window->consumeMotionDown(); | 
|  | 9449 |  | 
|  | 9450 | // Spy window pilfers the pointers. | 
|  | 9451 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 9452 | window->consumeMotionCancel(); | 
|  | 9453 |  | 
|  | 9454 | // Second finger down on the window and spy, but the window should not receive the pointer down. | 
|  | 9455 | const MotionEvent secondFingerDownEvent = | 
|  | 9456 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 9457 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 9458 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9459 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) | 
|  | 9460 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9461 | .build(); | 
|  | 9462 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9463 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9464 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 9465 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9466 |  | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9467 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9468 |  | 
|  | 9469 | // Third finger goes down outside all windows, so injection should fail. | 
|  | 9470 | const MotionEvent thirdFingerDownEvent = | 
|  | 9471 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 9472 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 9473 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9474 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) | 
|  | 9475 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) | 
|  | 9476 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9477 | .build(); | 
|  | 9478 | ASSERT_EQ(InputEventInjectionResult::FAILED, | 
|  | 9479 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9480 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
| Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9481 | << "Inject motion event should return InputEventInjectionResult::FAILED"; | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9482 |  | 
|  | 9483 | spy->assertNoEvents(); | 
|  | 9484 | window->assertNoEvents(); | 
|  | 9485 | } | 
|  | 9486 |  | 
|  | 9487 | /** | 
|  | 9488 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled | 
|  | 9489 | */ | 
|  | 9490 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { | 
|  | 9491 | auto spy = createSpy(); | 
|  | 9492 | spy->setFrame(Rect(0, 0, 100, 100)); | 
|  | 9493 | auto window = createForeground(); | 
|  | 9494 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 9495 |  | 
|  | 9496 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9497 |  | 
|  | 9498 | // First finger down on the window only | 
|  | 9499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9500 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9501 | {150, 150})) | 
|  | 9502 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9503 | window->consumeMotionDown(); | 
|  | 9504 |  | 
|  | 9505 | // Second finger down on the spy and window | 
|  | 9506 | const MotionEvent secondFingerDownEvent = | 
|  | 9507 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 9508 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 9509 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9510 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) | 
|  | 9511 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9512 | .build(); | 
|  | 9513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9514 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9515 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 9516 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9517 | spy->consumeMotionDown(); | 
|  | 9518 | window->consumeMotionPointerDown(1); | 
|  | 9519 |  | 
|  | 9520 | // Third finger down on the spy and window | 
|  | 9521 | const MotionEvent thirdFingerDownEvent = | 
|  | 9522 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 9523 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 9524 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9525 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) | 
|  | 9526 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) | 
|  | 9527 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9528 | .build(); | 
|  | 9529 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9530 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9531 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 9532 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9533 | spy->consumeMotionPointerDown(1); | 
|  | 9534 | window->consumeMotionPointerDown(2); | 
|  | 9535 |  | 
|  | 9536 | // Spy window pilfers the pointers. | 
|  | 9537 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 9538 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 9539 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); | 
|  | 9540 |  | 
|  | 9541 | spy->assertNoEvents(); | 
|  | 9542 | window->assertNoEvents(); | 
|  | 9543 | } | 
|  | 9544 |  | 
|  | 9545 | /** | 
|  | 9546 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to | 
|  | 9547 | * other windows should be canceled. If this results in the cancellation of all pointers for some | 
|  | 9548 | * window, then that window should receive ACTION_CANCEL. | 
|  | 9549 | */ | 
|  | 9550 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { | 
|  | 9551 | auto spy = createSpy(); | 
|  | 9552 | spy->setFrame(Rect(0, 0, 100, 100)); | 
|  | 9553 | auto window = createForeground(); | 
|  | 9554 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 9555 |  | 
|  | 9556 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9557 |  | 
|  | 9558 | // First finger down on both spy and window | 
|  | 9559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9560 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9561 | {10, 10})) | 
|  | 9562 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9563 | window->consumeMotionDown(); | 
|  | 9564 | spy->consumeMotionDown(); | 
|  | 9565 |  | 
|  | 9566 | // Second finger down on the spy and window | 
|  | 9567 | const MotionEvent secondFingerDownEvent = | 
|  | 9568 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 9569 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 9570 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9571 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) | 
|  | 9572 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9573 | .build(); | 
|  | 9574 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9575 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9576 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 9577 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9578 | spy->consumeMotionPointerDown(1); | 
|  | 9579 | window->consumeMotionPointerDown(1); | 
|  | 9580 |  | 
|  | 9581 | // Spy window pilfers the pointers. | 
|  | 9582 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 9583 | window->consumeMotionCancel(); | 
|  | 9584 |  | 
|  | 9585 | spy->assertNoEvents(); | 
|  | 9586 | window->assertNoEvents(); | 
|  | 9587 | } | 
|  | 9588 |  | 
|  | 9589 | /** | 
|  | 9590 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still | 
|  | 9591 | * be sent to other windows | 
|  | 9592 | */ | 
|  | 9593 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { | 
|  | 9594 | auto spy = createSpy(); | 
|  | 9595 | spy->setFrame(Rect(0, 0, 100, 100)); | 
|  | 9596 | auto window = createForeground(); | 
|  | 9597 | window->setFrame(Rect(0, 0, 200, 200)); | 
|  | 9598 |  | 
|  | 9599 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9600 |  | 
|  | 9601 | // First finger down on both window and spy | 
|  | 9602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9603 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
|  | 9604 | {10, 10})) | 
|  | 9605 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9606 | window->consumeMotionDown(); | 
|  | 9607 | spy->consumeMotionDown(); | 
|  | 9608 |  | 
|  | 9609 | // Spy window pilfers the pointers. | 
|  | 9610 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); | 
|  | 9611 | window->consumeMotionCancel(); | 
|  | 9612 |  | 
|  | 9613 | // Second finger down on the window only | 
|  | 9614 | const MotionEvent secondFingerDownEvent = | 
|  | 9615 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) | 
|  | 9616 | .displayId(ADISPLAY_ID_DEFAULT) | 
|  | 9617 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9618 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) | 
|  | 9619 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) | 
| Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9620 | .build(); | 
|  | 9621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9622 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, | 
|  | 9623 | InputEventInjectionSync::WAIT_FOR_RESULT)) | 
|  | 9624 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; | 
|  | 9625 | window->consumeMotionDown(); | 
|  | 9626 | window->assertNoEvents(); | 
|  | 9627 |  | 
|  | 9628 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line | 
|  | 9629 | spy->consumeMotionMove(); | 
|  | 9630 | spy->assertNoEvents(); | 
|  | 9631 | } | 
|  | 9632 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9633 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { | 
|  | 9634 | public: | 
|  | 9635 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { | 
|  | 9636 | std::shared_ptr<FakeApplicationHandle> overlayApplication = | 
|  | 9637 | std::make_shared<FakeApplicationHandle>(); | 
|  | 9638 | sp<FakeWindowHandle> overlay = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9639 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, | 
|  | 9640 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9641 | overlay->setFocusable(false); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9642 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); | 
| Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9643 | overlay->setTouchable(false); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9644 | overlay->setInterceptsStylus(true); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9645 | overlay->setTrustedOverlay(true); | 
|  | 9646 |  | 
|  | 9647 | std::shared_ptr<FakeApplicationHandle> application = | 
|  | 9648 | std::make_shared<FakeApplicationHandle>(); | 
|  | 9649 | sp<FakeWindowHandle> window = | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9650 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", | 
|  | 9651 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9652 | window->setFocusable(true); | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9653 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9654 |  | 
|  | 9655 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); | 
|  | 9656 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 9657 | setFocusedWindow(window); | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9658 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9659 | return {std::move(overlay), std::move(window)}; | 
|  | 9660 | } | 
|  | 9661 |  | 
|  | 9662 | void sendFingerEvent(int32_t action) { | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9663 | mDispatcher->notifyMotion( | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9664 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9665 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9666 | } | 
|  | 9667 |  | 
|  | 9668 | void sendStylusEvent(int32_t action) { | 
|  | 9669 | NotifyMotionArgs motionArgs = | 
|  | 9670 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, | 
|  | 9671 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); | 
| Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9672 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; | 
| Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9673 | mDispatcher->notifyMotion(motionArgs); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9674 | } | 
|  | 9675 | }; | 
|  | 9676 |  | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9677 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; | 
|  | 9678 |  | 
|  | 9679 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { | 
| Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 9680 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; | 
| Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9681 | ScopedSilentDeath _silentDeath; | 
|  | 9682 |  | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9683 | auto [overlay, window] = setupStylusOverlayScenario(); | 
|  | 9684 | overlay->setTrustedOverlay(false); | 
|  | 9685 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. | 
|  | 9686 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), | 
|  | 9687 | ".* not a trusted overlay"); | 
|  | 9688 | } | 
|  | 9689 |  | 
|  | 9690 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { | 
|  | 9691 | auto [overlay, window] = setupStylusOverlayScenario(); | 
|  | 9692 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 9693 |  | 
|  | 9694 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 9695 | overlay->consumeMotionDown(); | 
|  | 9696 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 9697 | overlay->consumeMotionUp(); | 
|  | 9698 |  | 
|  | 9699 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 9700 | window->consumeMotionDown(); | 
|  | 9701 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 9702 | window->consumeMotionUp(); | 
|  | 9703 |  | 
|  | 9704 | overlay->assertNoEvents(); | 
|  | 9705 | window->assertNoEvents(); | 
|  | 9706 | } | 
|  | 9707 |  | 
|  | 9708 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { | 
|  | 9709 | auto [overlay, window] = setupStylusOverlayScenario(); | 
| Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9710 | overlay->setSpy(true); | 
| Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9711 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 9712 |  | 
|  | 9713 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 9714 | overlay->consumeMotionDown(); | 
|  | 9715 | window->consumeMotionDown(); | 
|  | 9716 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 9717 | overlay->consumeMotionUp(); | 
|  | 9718 | window->consumeMotionUp(); | 
|  | 9719 |  | 
|  | 9720 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 9721 | window->consumeMotionDown(); | 
|  | 9722 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 9723 | window->consumeMotionUp(); | 
|  | 9724 |  | 
|  | 9725 | overlay->assertNoEvents(); | 
|  | 9726 | window->assertNoEvents(); | 
|  | 9727 | } | 
|  | 9728 |  | 
| Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9729 | /** | 
|  | 9730 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. | 
|  | 9731 | * The scenario is as follows: | 
|  | 9732 | *   - The stylus interceptor overlay is configured as a spy window. | 
|  | 9733 | *   - The stylus interceptor spy receives the start of a new stylus gesture. | 
|  | 9734 | *   - It pilfers pointers and then configures itself to no longer be a spy. | 
|  | 9735 | *   - The stylus interceptor continues to receive the rest of the gesture. | 
|  | 9736 | */ | 
|  | 9737 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { | 
|  | 9738 | auto [overlay, window] = setupStylusOverlayScenario(); | 
|  | 9739 | overlay->setSpy(true); | 
|  | 9740 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 9741 |  | 
|  | 9742 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); | 
|  | 9743 | overlay->consumeMotionDown(); | 
|  | 9744 | window->consumeMotionDown(); | 
|  | 9745 |  | 
|  | 9746 | // The interceptor pilfers the pointers. | 
|  | 9747 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); | 
|  | 9748 | window->consumeMotionCancel(); | 
|  | 9749 |  | 
|  | 9750 | // The interceptor configures itself so that it is no longer a spy. | 
|  | 9751 | overlay->setSpy(false); | 
|  | 9752 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); | 
|  | 9753 |  | 
|  | 9754 | // It continues to receive the rest of the stylus gesture. | 
|  | 9755 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); | 
|  | 9756 | overlay->consumeMotionMove(); | 
|  | 9757 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); | 
|  | 9758 | overlay->consumeMotionUp(); | 
|  | 9759 |  | 
|  | 9760 | window->assertNoEvents(); | 
|  | 9761 | } | 
|  | 9762 |  | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9763 | struct User { | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9764 | gui::Pid mPid; | 
| Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9765 | gui::Uid mUid; | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9766 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; | 
|  | 9767 | std::unique_ptr<InputDispatcher>& mDispatcher; | 
|  | 9768 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9769 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9770 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} | 
|  | 9771 |  | 
|  | 9772 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { | 
|  | 9773 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 9774 | ADISPLAY_ID_DEFAULT, {100, 200}, | 
|  | 9775 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 9776 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, | 
|  | 9777 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, | 
|  | 9778 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); | 
|  | 9779 | } | 
|  | 9780 |  | 
|  | 9781 | InputEventInjectionResult injectTargetedKey(int32_t action) const { | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9782 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9783 | InputEventInjectionSync::WAIT_FOR_RESULT, | 
| Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9784 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9785 | mPolicyFlags); | 
|  | 9786 | } | 
|  | 9787 |  | 
|  | 9788 | sp<FakeWindowHandle> createWindow() const { | 
|  | 9789 | std::shared_ptr<FakeApplicationHandle> overlayApplication = | 
|  | 9790 | std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9791 | sp<FakeWindowHandle> window = | 
|  | 9792 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", | 
|  | 9793 | ADISPLAY_ID_DEFAULT); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9794 | window->setOwnerInfo(mPid, mUid); | 
|  | 9795 | return window; | 
|  | 9796 | } | 
|  | 9797 | }; | 
|  | 9798 |  | 
|  | 9799 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; | 
|  | 9800 |  | 
|  | 9801 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9802 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9803 | auto window = owner.createWindow(); | 
|  | 9804 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 9805 |  | 
|  | 9806 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9807 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 9808 | window->consumeMotionDown(); | 
|  | 9809 |  | 
|  | 9810 | setFocusedWindow(window); | 
|  | 9811 | window->consumeFocusEvent(true); | 
|  | 9812 |  | 
|  | 9813 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9814 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); | 
|  | 9815 | window->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 9816 | } | 
|  | 9817 |  | 
|  | 9818 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9819 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9820 | auto window = owner.createWindow(); | 
|  | 9821 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
|  | 9822 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9823 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9824 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, | 
|  | 9825 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 9826 |  | 
|  | 9827 | setFocusedWindow(window); | 
|  | 9828 | window->consumeFocusEvent(true); | 
|  | 9829 |  | 
|  | 9830 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, | 
|  | 9831 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); | 
|  | 9832 | window->assertNoEvents(); | 
|  | 9833 | } | 
|  | 9834 |  | 
|  | 9835 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9836 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9837 | auto window = owner.createWindow(); | 
|  | 9838 | auto spy = owner.createWindow(); | 
|  | 9839 | spy->setSpy(true); | 
|  | 9840 | spy->setTrustedOverlay(true); | 
|  | 9841 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); | 
|  | 9842 |  | 
|  | 9843 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9844 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 9845 | spy->consumeMotionDown(); | 
|  | 9846 | window->consumeMotionDown(); | 
|  | 9847 | } | 
|  | 9848 |  | 
|  | 9849 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9850 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9851 | auto window = owner.createWindow(); | 
|  | 9852 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9853 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9854 | auto randosSpy = rando.createWindow(); | 
|  | 9855 | randosSpy->setSpy(true); | 
|  | 9856 | randosSpy->setTrustedOverlay(true); | 
|  | 9857 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); | 
|  | 9858 |  | 
|  | 9859 | // The event is targeted at owner's window, so injection should succeed, but the spy should | 
|  | 9860 | // not receive the event. | 
|  | 9861 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9862 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 9863 | randosSpy->assertNoEvents(); | 
|  | 9864 | window->consumeMotionDown(); | 
|  | 9865 | } | 
|  | 9866 |  | 
|  | 9867 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9868 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9869 | auto window = owner.createWindow(); | 
|  | 9870 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9871 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9872 | auto randosSpy = rando.createWindow(); | 
|  | 9873 | randosSpy->setSpy(true); | 
|  | 9874 | randosSpy->setTrustedOverlay(true); | 
|  | 9875 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); | 
|  | 9876 |  | 
|  | 9877 | // A user that has injection permission can inject into any window. | 
|  | 9878 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9879 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, | 
|  | 9880 | ADISPLAY_ID_DEFAULT)); | 
|  | 9881 | randosSpy->consumeMotionDown(); | 
|  | 9882 | window->consumeMotionDown(); | 
|  | 9883 |  | 
|  | 9884 | setFocusedWindow(randosSpy); | 
|  | 9885 | randosSpy->consumeFocusEvent(true); | 
|  | 9886 |  | 
|  | 9887 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); | 
|  | 9888 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); | 
|  | 9889 | window->assertNoEvents(); | 
|  | 9890 | } | 
|  | 9891 |  | 
| Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9892 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9893 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9894 | auto window = owner.createWindow(); | 
|  | 9895 |  | 
| Prabir Pradhan | e59c6dc | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9896 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9897 | auto randosWindow = rando.createWindow(); | 
|  | 9898 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); | 
|  | 9899 | randosWindow->setWatchOutsideTouch(true); | 
|  | 9900 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); | 
|  | 9901 |  | 
| Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9902 | // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids. | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9903 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, | 
|  | 9904 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); | 
|  | 9905 | window->consumeMotionDown(); | 
| Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9906 | randosWindow->assertNoEvents(); | 
| Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9907 | } | 
|  | 9908 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9909 | } // namespace android::inputdispatcher |