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 | aeebeb4 | 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 | aeebeb4 | 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 | aeebeb4 | 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 { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 209 | struct AnrResult { |
| 210 | sp<IBinder> token{}; |
| 211 | gui::Pid pid{gui::Pid::INVALID}; |
| 212 | }; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 213 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 215 | FakeInputDispatcherPolicy() = default; |
| 216 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 217 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 218 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 219 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 220 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 221 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 222 | |
| 223 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 224 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 225 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 226 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 229 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 230 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 231 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 232 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 233 | |
| 234 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 235 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 236 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 237 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 238 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 239 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 240 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 241 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 244 | void assertFilterInputEventWasNotCalled() { |
| 245 | std::scoped_lock lock(mLock); |
| 246 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 247 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 248 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 249 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 250 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 251 | ASSERT_TRUE(mConfigurationChangedTime) |
| 252 | << "Timed out waiting for configuration changed call"; |
| 253 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 254 | mConfigurationChangedTime = std::nullopt; |
| 255 | } |
| 256 | |
| 257 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 258 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 259 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 260 | // 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] | 261 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 262 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 263 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 264 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 265 | mLastNotifySwitch = std::nullopt; |
| 266 | } |
| 267 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 268 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 269 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 270 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 271 | mOnPointerDownToken.clear(); |
| 272 | } |
| 273 | |
| 274 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 275 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 276 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 277 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 280 | // This function must be called soon after the expected ANR timer starts, |
| 281 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 282 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 283 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 284 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 285 | std::unique_lock lock(mLock); |
| 286 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 287 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 288 | ASSERT_NO_FATAL_FAILURE( |
| 289 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 290 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 293 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 294 | const sp<WindowInfoHandle>& window) { |
| 295 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 296 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 297 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 300 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 301 | const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 302 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 303 | std::unique_lock lock(mLock); |
| 304 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 305 | AnrResult result; |
| 306 | ASSERT_NO_FATAL_FAILURE(result = |
| 307 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 308 | ASSERT_EQ(expectedToken, result.token); |
| 309 | ASSERT_EQ(expectedPid, result.pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 312 | /** 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] | 313 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 314 | std::unique_lock lock(mLock); |
| 315 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 316 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 317 | const auto& [token, _] = result; |
| 318 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 321 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 322 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 323 | std::unique_lock lock(mLock); |
| 324 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 325 | AnrResult result; |
| 326 | ASSERT_NO_FATAL_FAILURE( |
| 327 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 328 | ASSERT_EQ(expectedToken, result.token); |
| 329 | ASSERT_EQ(expectedPid, result.pid); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** 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] | 333 | sp<IBinder> getResponsiveWindowToken() { |
| 334 | std::unique_lock lock(mLock); |
| 335 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 336 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 337 | const auto& [token, _] = result; |
| 338 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void assertNotifyAnrWasNotCalled() { |
| 342 | std::scoped_lock lock(mLock); |
| 343 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 344 | ASSERT_TRUE(mAnrWindows.empty()); |
| 345 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 346 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 347 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 350 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 351 | std::unique_lock lock(mLock); |
| 352 | base::ScopedLockAssertion assumeLocked(mLock); |
| 353 | |
| 354 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 355 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 356 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 357 | enabled; |
| 358 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 359 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 360 | << ") to be called."; |
| 361 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 362 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 363 | auto request = *mPointerCaptureRequest; |
| 364 | mPointerCaptureRequest.reset(); |
| 365 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void assertSetPointerCaptureNotCalled() { |
| 369 | std::unique_lock lock(mLock); |
| 370 | base::ScopedLockAssertion assumeLocked(mLock); |
| 371 | |
| 372 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 373 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 374 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 375 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 376 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 377 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 380 | void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, |
| 381 | const sp<IBinder>& targetToken) { |
| 382 | dispatcher.waitForIdle(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 383 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 384 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 385 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 386 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 387 | } |
| 388 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 389 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 390 | std::unique_lock lock(mLock); |
| 391 | base::ScopedLockAssertion assumeLocked(mLock); |
| 392 | std::optional<sp<IBinder>> receivedToken = |
| 393 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 394 | mNotifyInputChannelBroken); |
| 395 | ASSERT_TRUE(receivedToken.has_value()); |
| 396 | ASSERT_EQ(token, *receivedToken); |
| 397 | } |
| 398 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 399 | /** |
| 400 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 401 | */ |
| 402 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 403 | mInterceptKeyTimeout = timeout; |
| 404 | } |
| 405 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 406 | void assertUserActivityPoked() { |
| 407 | std::scoped_lock lock(mLock); |
| 408 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; |
| 409 | } |
| 410 | |
| 411 | void assertUserActivityNotPoked() { |
| 412 | std::scoped_lock lock(mLock); |
| 413 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; |
| 414 | } |
| 415 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 416 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 417 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 418 | } |
| 419 | |
| 420 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 421 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 422 | } |
| 423 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 424 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 425 | std::mutex mLock; |
| 426 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 427 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 428 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 429 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 430 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 431 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 432 | |
| 433 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 434 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 435 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 436 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 437 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 438 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 439 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 440 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 441 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 442 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 443 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 444 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 445 | bool mPokedUserActivity GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 446 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 447 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 448 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 449 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 450 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 451 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 452 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 453 | // first entry from the container and erase it. |
| 454 | template <class T> |
| 455 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 456 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 457 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 458 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 459 | // before checking if ANR was called. |
| 460 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 461 | // to provide it some time to act. 100ms seems reasonable. |
| 462 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 463 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 464 | std::optional<T> token = |
| 465 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 466 | if (!token.has_value()) { |
| 467 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 468 | return {}; |
| 469 | } |
| 470 | |
| 471 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 472 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 473 | // the dispatcher started counting before this function was called |
| 474 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 475 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 476 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 477 | << "ms, but waited " |
| 478 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 479 | << "ms instead"; |
| 480 | } |
| 481 | return *token; |
| 482 | } |
| 483 | |
| 484 | template <class T> |
| 485 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 486 | std::queue<T>& storage, |
| 487 | std::unique_lock<std::mutex>& lock, |
| 488 | std::condition_variable& condition) |
| 489 | REQUIRES(mLock) { |
| 490 | condition.wait_for(lock, timeout, |
| 491 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 492 | if (storage.empty()) { |
| 493 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 494 | return std::nullopt; |
| 495 | } |
| 496 | T item = storage.front(); |
| 497 | storage.pop(); |
| 498 | return std::make_optional(item); |
| 499 | } |
| 500 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 501 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 502 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 503 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 506 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 507 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 508 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 509 | ASSERT_TRUE(pid.has_value()); |
| 510 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 511 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 512 | } |
| 513 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 514 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 515 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 516 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 517 | ASSERT_TRUE(pid.has_value()); |
| 518 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 519 | mNotifyAnr.notify_all(); |
| 520 | } |
| 521 | |
| 522 | void notifyNoFocusedWindowAnr( |
| 523 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 524 | std::scoped_lock lock(mLock); |
| 525 | mAnrApplications.push(applicationHandle); |
| 526 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 529 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 530 | std::scoped_lock lock(mLock); |
| 531 | mBrokenInputChannels.push(connectionToken); |
| 532 | mNotifyInputChannelBroken.notify_all(); |
| 533 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 534 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 535 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 536 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 537 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 538 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 539 | const std::vector<float>& values) override {} |
| 540 | |
| 541 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 542 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 543 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 544 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 545 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 546 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 547 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 548 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 549 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 550 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 551 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 552 | break; |
| 553 | } |
| 554 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 555 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 556 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 557 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 558 | break; |
| 559 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 560 | default: { |
| 561 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 562 | break; |
| 563 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 564 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 568 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 569 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 570 | // Clear intercept state when we handled the event. |
| 571 | mInterceptKeyTimeout = 0ms; |
| 572 | } |
| 573 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 575 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 576 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 577 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 578 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 579 | // Clear intercept state so we could dispatch the event in next wake. |
| 580 | mInterceptKeyTimeout = 0ms; |
| 581 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 584 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&, |
| 585 | uint32_t) override { |
| 586 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 589 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 590 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 591 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 592 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 593 | * essentially a passthrough for notifySwitch. |
| 594 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 595 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 598 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { |
| 599 | std::scoped_lock lock(mLock); |
| 600 | mPokedUserActivity = true; |
| 601 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 603 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 604 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 605 | mOnPointerDownToken = newToken; |
| 606 | } |
| 607 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 608 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 609 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 610 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 611 | mPointerCaptureChangedCondition.notify_all(); |
| 612 | } |
| 613 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 614 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 615 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 616 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 617 | mDropTargetWindowToken = token; |
| 618 | } |
| 619 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 620 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 621 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 622 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 623 | } |
| 624 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 625 | void assertFilterInputEventWasCalledInternal( |
| 626 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 627 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 628 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 629 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 630 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 631 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 632 | }; |
| 633 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | // --- InputDispatcherTest --- |
| 635 | |
| 636 | class InputDispatcherTest : public testing::Test { |
| 637 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 638 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 639 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 640 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 641 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 642 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
| 643 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 644 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 645 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 646 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 649 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 650 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 651 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 652 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 653 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 654 | |
| 655 | /** |
| 656 | * Used for debugging when writing the test |
| 657 | */ |
| 658 | void dumpDispatcherState() { |
| 659 | std::string dump; |
| 660 | mDispatcher->dump(dump); |
| 661 | std::stringstream ss(dump); |
| 662 | std::string to; |
| 663 | |
| 664 | while (std::getline(ss, to, '\n')) { |
| 665 | ALOGE("%s", to.c_str()); |
| 666 | } |
| 667 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 668 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 669 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 670 | FocusRequest request; |
| 671 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 672 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 673 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 674 | request.displayId = window->getInfo()->displayId; |
| 675 | mDispatcher->setFocusedWindow(request); |
| 676 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 677 | }; |
| 678 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 680 | KeyEvent event; |
| 681 | |
| 682 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 683 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 684 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 685 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 686 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 687 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 688 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 689 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 690 | << "Should reject key events with undefined action."; |
| 691 | |
| 692 | // 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] | 693 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 694 | 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] | 695 | ARBITRARY_TIME, 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 ACTION_MULTIPLE."; |
| 700 | } |
| 701 | |
| 702 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 703 | MotionEvent event; |
| 704 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 705 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 706 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 707 | pointerProperties[i].clear(); |
| 708 | pointerProperties[i].id = i; |
| 709 | pointerCoords[i].clear(); |
| 710 | } |
| 711 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 712 | // Some constants commonly used below |
| 713 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 714 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 715 | constexpr int32_t metaState = AMETA_NONE; |
| 716 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 717 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 718 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 719 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 720 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 721 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 722 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 723 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 724 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 725 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 726 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 727 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 728 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 729 | << "Should reject motion events with undefined action."; |
| 730 | |
| 731 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 732 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 733 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 734 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 735 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 736 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 737 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 738 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 739 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 740 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 741 | << "Should reject motion events with pointer down index too large."; |
| 742 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 743 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 744 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 745 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 746 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 747 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 748 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 749 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 750 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 751 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 752 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 753 | << "Should reject motion events with pointer down index too small."; |
| 754 | |
| 755 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 756 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 757 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 758 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 759 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 760 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 761 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 762 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 763 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 764 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | << "Should reject motion events with pointer up index too large."; |
| 766 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 767 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 768 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 769 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 770 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 771 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 772 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 773 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 774 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 775 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 776 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 777 | << "Should reject motion events with pointer up index too small."; |
| 778 | |
| 779 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 780 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 781 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 782 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 783 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 784 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 785 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 786 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 787 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 788 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | << "Should reject motion events with 0 pointers."; |
| 790 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 791 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 792 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 793 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 794 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 795 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 796 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 797 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 798 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 799 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 800 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 801 | |
| 802 | // Rejects motion events with invalid pointer ids. |
| 803 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 804 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 805 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 806 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 807 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 808 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 809 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 810 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 811 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 812 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | << "Should reject motion events with pointer ids less than 0."; |
| 814 | |
| 815 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 816 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 817 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 818 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 819 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 820 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 821 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 822 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 823 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 824 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 825 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 826 | |
| 827 | // Rejects motion events with duplicate pointer ids. |
| 828 | pointerProperties[0].id = 1; |
| 829 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 830 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 831 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 832 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 833 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 834 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 835 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 836 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 837 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 838 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 839 | << "Should reject motion events with duplicate pointer ids."; |
| 840 | } |
| 841 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 842 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 843 | |
| 844 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 845 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 846 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 847 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 848 | |
| 849 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 850 | } |
| 851 | |
| 852 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 853 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 854 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 855 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 856 | |
| 857 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 858 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 859 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 860 | } |
| 861 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 862 | namespace { |
| 863 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 864 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 865 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 866 | // Default input dispatching timeout if there is no focused application or paused window |
| 867 | // from which to determine an appropriate dispatching timeout. |
| 868 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 869 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 870 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 871 | |
| 872 | class FakeApplicationHandle : public InputApplicationHandle { |
| 873 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 874 | FakeApplicationHandle() { |
| 875 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 876 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 877 | mInfo.dispatchingTimeoutMillis = |
| 878 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 879 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 880 | virtual ~FakeApplicationHandle() {} |
| 881 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 882 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 883 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 884 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 885 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 886 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 887 | }; |
| 888 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 889 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 890 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 891 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 892 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 893 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 894 | } |
| 895 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 896 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 897 | InputEvent* event; |
| 898 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 899 | if (!consumeSeq) { |
| 900 | return nullptr; |
| 901 | } |
| 902 | finishEvent(*consumeSeq); |
| 903 | return event; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Receive an event without acknowledging it. |
| 908 | * Return the sequence number that could later be used to send finished signal. |
| 909 | */ |
| 910 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 911 | uint32_t consumeSeq; |
| 912 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 913 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 914 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 915 | status_t status = WOULD_BLOCK; |
| 916 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 917 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 918 | &event); |
| 919 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 920 | if (elapsed > 100ms) { |
| 921 | break; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | if (status == WOULD_BLOCK) { |
| 926 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 927 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | if (status != OK) { |
| 931 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 932 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 933 | } |
| 934 | if (event == nullptr) { |
| 935 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
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 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 938 | if (outEvent != nullptr) { |
| 939 | *outEvent = event; |
| 940 | } |
| 941 | return consumeSeq; |
| 942 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 943 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 944 | /** |
| 945 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 946 | */ |
| 947 | void finishEvent(uint32_t consumeSeq) { |
| 948 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 949 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 952 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 953 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 954 | ASSERT_EQ(OK, status); |
| 955 | } |
| 956 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 957 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 958 | std::optional<int32_t> expectedDisplayId, |
| 959 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 960 | InputEvent* event = consume(); |
| 961 | |
| 962 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 963 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 964 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 965 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 966 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 967 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 968 | if (expectedDisplayId.has_value()) { |
| 969 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 970 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 971 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 972 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 973 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 974 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 975 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 976 | if (expectedFlags.has_value()) { |
| 977 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 978 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 979 | break; |
| 980 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 981 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 982 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 983 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 984 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 985 | if (expectedFlags.has_value()) { |
| 986 | EXPECT_EQ(expectedFlags.value(), motionEvent.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::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 991 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 992 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 993 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 994 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 995 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 996 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 997 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 998 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 999 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1000 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 1001 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1002 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1005 | MotionEvent* consumeMotion() { |
| 1006 | InputEvent* event = consume(); |
| 1007 | |
| 1008 | if (event == nullptr) { |
| 1009 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 1010 | return nullptr; |
| 1011 | } |
| 1012 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1013 | if (event->getType() != InputEventType::MOTION) { |
| 1014 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1015 | return nullptr; |
| 1016 | } |
| 1017 | return static_cast<MotionEvent*>(event); |
| 1018 | } |
| 1019 | |
| 1020 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1021 | MotionEvent* motionEvent = consumeMotion(); |
| 1022 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1023 | ASSERT_THAT(*motionEvent, matcher); |
| 1024 | } |
| 1025 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1026 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 1027 | InputEvent* event = consume(); |
| 1028 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1029 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1030 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1031 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1032 | |
| 1033 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1034 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1035 | |
| 1036 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1037 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1040 | void consumeCaptureEvent(bool hasCapture) { |
| 1041 | const InputEvent* event = consume(); |
| 1042 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1043 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1044 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1045 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1046 | |
| 1047 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1048 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1049 | |
| 1050 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1051 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1052 | } |
| 1053 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1054 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1055 | const InputEvent* event = consume(); |
| 1056 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1057 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1058 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1059 | |
| 1060 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1061 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1062 | |
| 1063 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1064 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1065 | EXPECT_EQ(x, dragEvent.getX()); |
| 1066 | EXPECT_EQ(y, dragEvent.getY()); |
| 1067 | } |
| 1068 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1069 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1070 | const InputEvent* event = consume(); |
| 1071 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1072 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1073 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1074 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1075 | |
| 1076 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1077 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1078 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1079 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1080 | } |
| 1081 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1082 | void assertNoEvents() { |
| 1083 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1084 | if (event == nullptr) { |
| 1085 | return; |
| 1086 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1087 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1088 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1089 | ADD_FAILURE() << "Received key event " |
| 1090 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1091 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1092 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1093 | ADD_FAILURE() << "Received motion event " |
| 1094 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1095 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1096 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1097 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1098 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1099 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1100 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1101 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1102 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1103 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1104 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1105 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1106 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1107 | } |
| 1108 | FAIL() << mName.c_str() |
| 1109 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1113 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1114 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1115 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1116 | protected: |
| 1117 | std::unique_ptr<InputConsumer> mConsumer; |
| 1118 | PreallocatedInputEventFactory mEventFactory; |
| 1119 | |
| 1120 | std::string mName; |
| 1121 | }; |
| 1122 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1123 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1124 | public: |
| 1125 | static const int32_t WIDTH = 600; |
| 1126 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1127 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1128 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1129 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1130 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1131 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1132 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1133 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1134 | dispatcher->createInputChannel(name); |
| 1135 | token = (*channel)->getConnectionToken(); |
| 1136 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | inputApplicationHandle->updateInfo(); |
| 1140 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1141 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1142 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1143 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1144 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1145 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1146 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1147 | mInfo.frameLeft = 0; |
| 1148 | mInfo.frameTop = 0; |
| 1149 | mInfo.frameRight = WIDTH; |
| 1150 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1151 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1152 | mInfo.globalScaleFactor = 1.0; |
| 1153 | mInfo.touchableRegion.clear(); |
| 1154 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1155 | mInfo.ownerPid = WINDOW_PID; |
| 1156 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1157 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1158 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1161 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1162 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1163 | handle->mInfo = mInfo; |
| 1164 | handle->mInfo.displayId = displayId; |
| 1165 | handle->mInfo.id = sId++; |
| 1166 | handle->mInputReceiver = mInputReceiver; |
| 1167 | return handle; |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * This is different from clone, because clone will make a "mirror" window - a window with the |
| 1172 | * same token, but a different ID. The original window and the clone window are allowed to be |
| 1173 | * sent to the dispatcher at the same time - they can coexist inside the dispatcher. |
| 1174 | * This function will create a different object of WindowInfoHandle, but with the same |
| 1175 | * properties as the original object - including the ID. |
| 1176 | * You can use either the old or the new object to consume the events. |
| 1177 | * IMPORTANT: The duplicated object is supposed to replace the original object, and not appear |
| 1178 | * at the same time inside dispatcher. |
| 1179 | */ |
| 1180 | sp<FakeWindowHandle> duplicate() { |
| 1181 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mName); |
| 1182 | handle->mInfo = mInfo; |
| 1183 | handle->mInputReceiver = mInputReceiver; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1184 | return handle; |
| 1185 | } |
| 1186 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1187 | void setTouchable(bool touchable) { |
| 1188 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1189 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1190 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1191 | void setFocusable(bool focusable) { |
| 1192 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1193 | } |
| 1194 | |
| 1195 | void setVisible(bool visible) { |
| 1196 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1197 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1198 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1199 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1200 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1203 | void setPaused(bool paused) { |
| 1204 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1205 | } |
| 1206 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1207 | void setPreventSplitting(bool preventSplitting) { |
| 1208 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | void setSlippery(bool slippery) { |
| 1212 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1213 | } |
| 1214 | |
| 1215 | void setWatchOutsideTouch(bool watchOutside) { |
| 1216 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1217 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1218 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1219 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1220 | |
| 1221 | void setInterceptsStylus(bool interceptsStylus) { |
| 1222 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1223 | } |
| 1224 | |
| 1225 | void setDropInput(bool dropInput) { |
| 1226 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1227 | } |
| 1228 | |
| 1229 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1230 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1231 | } |
| 1232 | |
| 1233 | void setNoInputChannel(bool noInputChannel) { |
| 1234 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1235 | } |
| 1236 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1237 | void setDisableUserActivity(bool disableUserActivity) { |
| 1238 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1239 | } |
| 1240 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1241 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1242 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1243 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1244 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1245 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1246 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1247 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1248 | mInfo.frameLeft = frame.left; |
| 1249 | mInfo.frameTop = frame.top; |
| 1250 | mInfo.frameRight = frame.right; |
| 1251 | mInfo.frameBottom = frame.bottom; |
| 1252 | mInfo.touchableRegion.clear(); |
| 1253 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1254 | |
| 1255 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1256 | ui::Transform translate; |
| 1257 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1258 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1261 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1262 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1263 | void setIsWallpaper(bool isWallpaper) { |
| 1264 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1265 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1266 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1267 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1268 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1269 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1270 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1271 | void setTrustedOverlay(bool trustedOverlay) { |
| 1272 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1273 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1274 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1275 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1276 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1277 | } |
| 1278 | |
| 1279 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1280 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1281 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1282 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1283 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1284 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1287 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1288 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1291 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1292 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1293 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1294 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1298 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1299 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1300 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1304 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1305 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1306 | } |
| 1307 | |
| 1308 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1309 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1310 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1311 | expectedFlags); |
| 1312 | } |
| 1313 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1314 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1315 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1316 | int32_t expectedFlags = 0) { |
| 1317 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1318 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1319 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1323 | int32_t expectedFlags = 0) { |
| 1324 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1325 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1326 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1330 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1331 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1332 | expectedFlags); |
| 1333 | } |
| 1334 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1335 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1336 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1337 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1338 | expectedFlags); |
| 1339 | } |
| 1340 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1341 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1342 | int32_t expectedFlags = 0) { |
| 1343 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1344 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1345 | ASSERT_EQ(InputEventType::MOTION, event->getType()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1346 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1347 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1348 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1349 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1350 | } |
| 1351 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1352 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1353 | ASSERT_NE(mInputReceiver, nullptr) |
| 1354 | << "Cannot consume events from a window with no receiver"; |
| 1355 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1356 | } |
| 1357 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1358 | void consumeCaptureEvent(bool hasCapture) { |
| 1359 | ASSERT_NE(mInputReceiver, nullptr) |
| 1360 | << "Cannot consume events from a window with no receiver"; |
| 1361 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1362 | } |
| 1363 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1364 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1365 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1366 | 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] | 1367 | ASSERT_THAT(*motionEvent, matcher); |
| 1368 | } |
| 1369 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1370 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1371 | std::optional<int32_t> expectedDisplayId, |
| 1372 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1373 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1374 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1375 | expectedFlags); |
| 1376 | } |
| 1377 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1378 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1379 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1380 | } |
| 1381 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1382 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1383 | ASSERT_NE(mInputReceiver, nullptr) |
| 1384 | << "Cannot consume events from a window with no receiver"; |
| 1385 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1386 | } |
| 1387 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1388 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1389 | if (mInputReceiver == nullptr) { |
| 1390 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1391 | return std::nullopt; |
| 1392 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1393 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | void finishEvent(uint32_t sequenceNum) { |
| 1397 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1398 | mInputReceiver->finishEvent(sequenceNum); |
| 1399 | } |
| 1400 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1401 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1402 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1403 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1404 | } |
| 1405 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1406 | InputEvent* consume() { |
| 1407 | if (mInputReceiver == nullptr) { |
| 1408 | return nullptr; |
| 1409 | } |
| 1410 | return mInputReceiver->consume(); |
| 1411 | } |
| 1412 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1413 | MotionEvent* consumeMotion() { |
| 1414 | InputEvent* event = consume(); |
| 1415 | if (event == nullptr) { |
| 1416 | ADD_FAILURE() << "Consume failed : no event"; |
| 1417 | return nullptr; |
| 1418 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1419 | if (event->getType() != InputEventType::MOTION) { |
| 1420 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1421 | return nullptr; |
| 1422 | } |
| 1423 | return static_cast<MotionEvent*>(event); |
| 1424 | } |
| 1425 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1426 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1427 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1428 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1429 | return; // Can't receive events if the window does not have input channel |
| 1430 | } |
| 1431 | ASSERT_NE(nullptr, mInputReceiver) |
| 1432 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1433 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1434 | } |
| 1435 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1436 | sp<IBinder> getToken() { return mInfo.token; } |
| 1437 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1438 | const std::string& getName() { return mName; } |
| 1439 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1440 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1441 | mInfo.ownerPid = ownerPid; |
| 1442 | mInfo.ownerUid = ownerUid; |
| 1443 | } |
| 1444 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1445 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1446 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1447 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1448 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1449 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1450 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1451 | private: |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1452 | FakeWindowHandle(std::string name) : mName(name){}; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1453 | const std::string mName; |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1454 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1455 | 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] | 1456 | friend class sp<FakeWindowHandle>; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1457 | }; |
| 1458 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1459 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1460 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1461 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1462 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1463 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1464 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1465 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1466 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1467 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1468 | KeyEvent event; |
| 1469 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1470 | |
| 1471 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1472 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1473 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1474 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1475 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1476 | if (!allowKeyRepeat) { |
| 1477 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1478 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1479 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1480 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1483 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1484 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1485 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1488 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1489 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1490 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1491 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1492 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1493 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1494 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1495 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1496 | } |
| 1497 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1498 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1499 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1500 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1503 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1504 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1505 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1506 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1507 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1508 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1509 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -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, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1514 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1515 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1516 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1517 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1518 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1519 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1520 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1521 | MotionEventBuilder motionBuilder = |
| 1522 | MotionEventBuilder(action, source) |
| 1523 | .displayId(displayId) |
| 1524 | .eventTime(eventTime) |
| 1525 | .rawXCursorPosition(cursorPosition.x) |
| 1526 | .rawYCursorPosition(cursorPosition.y) |
| 1527 | .pointer( |
| 1528 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1529 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1530 | motionBuilder.downTime(eventTime); |
| 1531 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1532 | |
| 1533 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1534 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1535 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1536 | } |
| 1537 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1538 | static InputEventInjectionResult injectMotionDown( |
| 1539 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1540 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1541 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1544 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1545 | int32_t source, int32_t displayId, |
| 1546 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1547 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1550 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1551 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1552 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1553 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1554 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*flags=*/0, AKEYCODE_A, KEY_A, |
| 1555 | AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1556 | |
| 1557 | return args; |
| 1558 | } |
| 1559 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1560 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1561 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1562 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1563 | // Define a valid key event. |
| 1564 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1565 | displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, AMETA_META_ON, |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1566 | currentTime); |
| 1567 | |
| 1568 | return args; |
| 1569 | } |
| 1570 | |
| 1571 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1572 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1573 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1574 | // Define a valid key event. |
| 1575 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1576 | displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, KEY_ASSISTANT, |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1577 | AMETA_NONE, currentTime); |
| 1578 | |
| 1579 | return args; |
| 1580 | } |
| 1581 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1582 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1583 | int32_t displayId, |
| 1584 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1585 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1586 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1587 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1588 | } |
| 1589 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1590 | PointerProperties pointerProperties[pointerCount]; |
| 1591 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1592 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1593 | for (size_t i = 0; i < pointerCount; i++) { |
| 1594 | pointerProperties[i].clear(); |
| 1595 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1596 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1597 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1598 | pointerCoords[i].clear(); |
| 1599 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1600 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1601 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1602 | |
| 1603 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1604 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1605 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1606 | POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, /*flags=*/0, |
| 1607 | AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1608 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1609 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1610 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1611 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1612 | |
| 1613 | return args; |
| 1614 | } |
| 1615 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1616 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1617 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1618 | } |
| 1619 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1620 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1621 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1622 | } |
| 1623 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1624 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1625 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1626 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1627 | } |
| 1628 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1629 | } // namespace |
| 1630 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1631 | /** |
| 1632 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1633 | * broken channel. |
| 1634 | */ |
| 1635 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1636 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1637 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1638 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1639 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1640 | |
| 1641 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1642 | |
| 1643 | // Window closes its channel, but the window remains. |
| 1644 | window->destroyReceiver(); |
| 1645 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1646 | } |
| 1647 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1648 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1649 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1650 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1651 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1652 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1653 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1654 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1655 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1656 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1657 | |
| 1658 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1659 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1660 | } |
| 1661 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1662 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1663 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1664 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1665 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1666 | |
| 1667 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1668 | // Inject a MotionEvent to an unknown display. |
| 1669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1670 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1671 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1672 | |
| 1673 | // Window should receive motion event. |
| 1674 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1675 | } |
| 1676 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1677 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1678 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1679 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1680 | * called twice. |
| 1681 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1682 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1683 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1684 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1685 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1686 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1687 | |
| 1688 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1689 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1690 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1691 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1692 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1693 | |
| 1694 | // Window should receive motion event. |
| 1695 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1696 | } |
| 1697 | |
| 1698 | /** |
| 1699 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1700 | */ |
| 1701 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1702 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1703 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1704 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1705 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1706 | |
| 1707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1708 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1709 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1710 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1711 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1712 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1713 | |
| 1714 | // Window should receive motion event. |
| 1715 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1716 | } |
| 1717 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1718 | // The foreground window should receive the first touch down event. |
| 1719 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1720 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1721 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1722 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1723 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1724 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1725 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1726 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1728 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1729 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1730 | |
| 1731 | // 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] | 1732 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1733 | windowSecond->assertNoEvents(); |
| 1734 | } |
| 1735 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1736 | /** |
| 1737 | * Two windows: A top window, and a wallpaper behind the window. |
| 1738 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1739 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1740 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1741 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1742 | */ |
| 1743 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1744 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1745 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1746 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1747 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1748 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1749 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1750 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1751 | |
| 1752 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1753 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1754 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1755 | {100, 200})) |
| 1756 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1757 | |
| 1758 | // Both foreground window and its wallpaper should receive the touch down |
| 1759 | foregroundWindow->consumeMotionDown(); |
| 1760 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1761 | |
| 1762 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1763 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1764 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1765 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1766 | |
| 1767 | foregroundWindow->consumeMotionMove(); |
| 1768 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1769 | |
| 1770 | // Now the foreground window goes away, but the wallpaper stays |
| 1771 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1772 | foregroundWindow->consumeMotionCancel(); |
| 1773 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1774 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1775 | } |
| 1776 | |
| 1777 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1778 | * Two fingers down on the window, and lift off the first finger. |
| 1779 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1780 | * contains a single pointer. |
| 1781 | */ |
| 1782 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1783 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1784 | sp<FakeWindowHandle> window = |
| 1785 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1786 | |
| 1787 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1788 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1789 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1790 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1791 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1792 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1793 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1794 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1795 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1796 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1797 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1798 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1799 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1800 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1801 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1802 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1803 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1804 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1805 | |
| 1806 | // Remove the window. The gesture should be canceled |
| 1807 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 1808 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1809 | window->consumeMotionEvent( |
| 1810 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1811 | } |
| 1812 | |
| 1813 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1814 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1815 | * with the following differences: |
| 1816 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1817 | * clean up the connection. |
| 1818 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1819 | * Ensure that there's no crash in the dispatcher. |
| 1820 | */ |
| 1821 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1822 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1823 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1824 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1825 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1826 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1827 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1828 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1829 | |
| 1830 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1832 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1833 | {100, 200})) |
| 1834 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1835 | |
| 1836 | // Both foreground window and its wallpaper should receive the touch down |
| 1837 | foregroundWindow->consumeMotionDown(); |
| 1838 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1839 | |
| 1840 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1841 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1842 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1843 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1844 | |
| 1845 | foregroundWindow->consumeMotionMove(); |
| 1846 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1847 | |
| 1848 | // Wallpaper closes its channel, but the window remains. |
| 1849 | wallpaperWindow->destroyReceiver(); |
| 1850 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1851 | |
| 1852 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1853 | // is no longer valid. |
| 1854 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1855 | foregroundWindow->consumeMotionCancel(); |
| 1856 | } |
| 1857 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1858 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1859 | public ::testing::WithParamInterface<bool> {}; |
| 1860 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1861 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1862 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1863 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1864 | * The top window gets a multitouch gesture. |
| 1865 | * Ensure that wallpaper gets the same gesture. |
| 1866 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1867 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1868 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1869 | sp<FakeWindowHandle> foregroundWindow = |
| 1870 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1871 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1872 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1873 | |
| 1874 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1875 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1876 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1877 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1878 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1879 | |
| 1880 | // Touch down on top window |
| 1881 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1882 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1883 | {100, 100})) |
| 1884 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1885 | |
| 1886 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1887 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1888 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1889 | |
| 1890 | // Second finger down on the top window |
| 1891 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1892 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1893 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1894 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1895 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1896 | .build(); |
| 1897 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1898 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1899 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1900 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1901 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1902 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1903 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1904 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1905 | |
| 1906 | const MotionEvent secondFingerUpEvent = |
| 1907 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1908 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1909 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1910 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1911 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1912 | .build(); |
| 1913 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1914 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1915 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1916 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1917 | foregroundWindow->consumeMotionPointerUp(0); |
| 1918 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1919 | |
| 1920 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1921 | injectMotionEvent(mDispatcher, |
| 1922 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1923 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1924 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1925 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1926 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1927 | .x(100) |
| 1928 | .y(100)) |
| 1929 | .build(), |
| 1930 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1931 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1932 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1933 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * Two windows: a window on the left and window on the right. |
| 1938 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1939 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1940 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1941 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1942 | * ACTION_POINTER_DOWN(1). |
| 1943 | */ |
| 1944 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1945 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1946 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1947 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1948 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1949 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1950 | |
| 1951 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1952 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1953 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1954 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1955 | |
| 1956 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1957 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1958 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1959 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1960 | |
| 1961 | mDispatcher->setInputWindows( |
| 1962 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1963 | |
| 1964 | // Touch down on left window |
| 1965 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1966 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1967 | {100, 100})) |
| 1968 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1969 | |
| 1970 | // Both foreground window and its wallpaper should receive the touch down |
| 1971 | leftWindow->consumeMotionDown(); |
| 1972 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1973 | |
| 1974 | // Second finger down on the right window |
| 1975 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1976 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1977 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1978 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1979 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1980 | .build(); |
| 1981 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1982 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1983 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1984 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1985 | |
| 1986 | leftWindow->consumeMotionMove(); |
| 1987 | // Since the touch is split, right window gets ACTION_DOWN |
| 1988 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1989 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1990 | expectedWallpaperFlags); |
| 1991 | |
| 1992 | // Now, leftWindow, which received the first finger, disappears. |
| 1993 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1994 | leftWindow->consumeMotionCancel(); |
| 1995 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1996 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1997 | |
| 1998 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1999 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2000 | const MotionEvent secondFingerMoveEvent = |
| 2001 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2002 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2003 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2004 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2005 | .build(); |
| 2006 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2007 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2008 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2009 | rightWindow->consumeMotionMove(); |
| 2010 | |
| 2011 | leftWindow->assertNoEvents(); |
| 2012 | rightWindow->assertNoEvents(); |
| 2013 | wallpaperWindow->assertNoEvents(); |
| 2014 | } |
| 2015 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2016 | /** |
| 2017 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2018 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2019 | * The right window should receive ACTION_DOWN. |
| 2020 | */ |
| 2021 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2022 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2023 | sp<FakeWindowHandle> leftWindow = |
| 2024 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2025 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2026 | leftWindow->setDupTouchToWallpaper(true); |
| 2027 | leftWindow->setSlippery(true); |
| 2028 | |
| 2029 | sp<FakeWindowHandle> rightWindow = |
| 2030 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2031 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2032 | |
| 2033 | sp<FakeWindowHandle> wallpaperWindow = |
| 2034 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2035 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2036 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2037 | mDispatcher->setInputWindows( |
| 2038 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2039 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2040 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2042 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2043 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2044 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2045 | |
| 2046 | // Both foreground window and its wallpaper should receive the touch down |
| 2047 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2048 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2049 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2050 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2051 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2052 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2053 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2054 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2055 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2056 | leftWindow->consumeMotionCancel(); |
| 2057 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2058 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2061 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2062 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2063 | * interactive, it might stop sending this flag. |
| 2064 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2065 | * to have a consistent input stream. |
| 2066 | * |
| 2067 | * Test procedure: |
| 2068 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2069 | * DOWN (new gesture). |
| 2070 | * |
| 2071 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2072 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2073 | * |
| 2074 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2075 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2076 | */ |
| 2077 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2078 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2079 | sp<FakeWindowHandle> spyWindow = |
| 2080 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2081 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2082 | spyWindow->setTrustedOverlay(true); |
| 2083 | spyWindow->setSpy(true); |
| 2084 | |
| 2085 | sp<FakeWindowHandle> window = |
| 2086 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2087 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2088 | |
| 2089 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2090 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2091 | |
| 2092 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2093 | mDispatcher->notifyMotion( |
| 2094 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2095 | .deviceId(touchDeviceId) |
| 2096 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2097 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2098 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2099 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2100 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2101 | .deviceId(touchDeviceId) |
| 2102 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2103 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2104 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2105 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2106 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2107 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2108 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2109 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2110 | |
| 2111 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2112 | mDispatcher->notifyMotion( |
| 2113 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2114 | .deviceId(touchDeviceId) |
| 2115 | .policyFlags(0) |
| 2116 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2117 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2118 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2119 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2120 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2121 | |
| 2122 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2123 | // 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] | 2124 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2125 | |
| 2126 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2127 | mDispatcher->notifyMotion( |
| 2128 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2129 | .deviceId(touchDeviceId) |
| 2130 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2131 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2132 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2133 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2134 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2135 | |
| 2136 | // No more events |
| 2137 | spyWindow->assertNoEvents(); |
| 2138 | window->assertNoEvents(); |
| 2139 | } |
| 2140 | |
| 2141 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2142 | * Two windows: a window on the left and a window on the right. |
| 2143 | * Mouse is hovered from the right window into the left window. |
| 2144 | * Next, we tap on the left window, where the cursor was last seen. |
| 2145 | * The second tap is done onto the right window. |
| 2146 | * The mouse and tap are from two different devices. |
| 2147 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2148 | * explicitly helps during debugging. |
| 2149 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2150 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2151 | */ |
| 2152 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2153 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2154 | sp<FakeWindowHandle> leftWindow = |
| 2155 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2156 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2157 | |
| 2158 | sp<FakeWindowHandle> rightWindow = |
| 2159 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2160 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2161 | |
| 2162 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2163 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2164 | // stale. |
| 2165 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2166 | const int32_t mouseDeviceId = 6; |
| 2167 | const int32_t touchDeviceId = 4; |
| 2168 | // Move the cursor from right |
| 2169 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2170 | injectMotionEvent(mDispatcher, |
| 2171 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2172 | AINPUT_SOURCE_MOUSE) |
| 2173 | .deviceId(mouseDeviceId) |
| 2174 | .downTime(baseTime + 10) |
| 2175 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2176 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2177 | .x(300) |
| 2178 | .y(100)) |
| 2179 | .build())); |
| 2180 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2181 | |
| 2182 | // .. to the left window |
| 2183 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2184 | injectMotionEvent(mDispatcher, |
| 2185 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2186 | AINPUT_SOURCE_MOUSE) |
| 2187 | .deviceId(mouseDeviceId) |
| 2188 | .downTime(baseTime + 10) |
| 2189 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2190 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2191 | .x(110) |
| 2192 | .y(100)) |
| 2193 | .build())); |
| 2194 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2195 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2196 | // Now tap the left window |
| 2197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2198 | injectMotionEvent(mDispatcher, |
| 2199 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2200 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2201 | .deviceId(touchDeviceId) |
| 2202 | .downTime(baseTime + 40) |
| 2203 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2204 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2205 | .x(100) |
| 2206 | .y(100)) |
| 2207 | .build())); |
| 2208 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2209 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2210 | |
| 2211 | // release tap |
| 2212 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2213 | injectMotionEvent(mDispatcher, |
| 2214 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2215 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2216 | .deviceId(touchDeviceId) |
| 2217 | .downTime(baseTime + 40) |
| 2218 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2219 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2220 | .x(100) |
| 2221 | .y(100)) |
| 2222 | .build())); |
| 2223 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2224 | |
| 2225 | // Tap the window on the right |
| 2226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2227 | injectMotionEvent(mDispatcher, |
| 2228 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2229 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2230 | .deviceId(touchDeviceId) |
| 2231 | .downTime(baseTime + 60) |
| 2232 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2233 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2234 | .x(300) |
| 2235 | .y(100)) |
| 2236 | .build())); |
| 2237 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2238 | |
| 2239 | // release tap |
| 2240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2241 | injectMotionEvent(mDispatcher, |
| 2242 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2243 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2244 | .deviceId(touchDeviceId) |
| 2245 | .downTime(baseTime + 60) |
| 2246 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2247 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2248 | .x(300) |
| 2249 | .y(100)) |
| 2250 | .build())); |
| 2251 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2252 | |
| 2253 | // No more events |
| 2254 | leftWindow->assertNoEvents(); |
| 2255 | rightWindow->assertNoEvents(); |
| 2256 | } |
| 2257 | |
| 2258 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2259 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2260 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2261 | * While the top window is present, the hovering is stopped. |
| 2262 | * Later, hovering gets resumed again. |
| 2263 | * Ensure that new hover gesture is handled correctly. |
| 2264 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2265 | * to the window that's currently being hovered over. |
| 2266 | */ |
| 2267 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2268 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2269 | sp<FakeWindowHandle> window = |
| 2270 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2271 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2272 | |
| 2273 | // Only a single window is present at first |
| 2274 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2275 | |
| 2276 | // Start hovering in the window |
| 2277 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2278 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2279 | .build()); |
| 2280 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2281 | |
| 2282 | // Now, an obscuring window appears! |
| 2283 | sp<FakeWindowHandle> obscuringWindow = |
| 2284 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2285 | ADISPLAY_ID_DEFAULT, |
| 2286 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2287 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2288 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2289 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2290 | obscuringWindow->setNoInputChannel(true); |
| 2291 | obscuringWindow->setFocusable(false); |
| 2292 | obscuringWindow->setAlpha(1.0); |
| 2293 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2294 | |
| 2295 | // While this new obscuring window is present, the hovering is stopped |
| 2296 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2297 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2298 | .build()); |
| 2299 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2300 | |
| 2301 | // Now the obscuring window goes away. |
| 2302 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2303 | |
| 2304 | // And a new hover gesture starts. |
| 2305 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2306 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2307 | .build()); |
| 2308 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2309 | } |
| 2310 | |
| 2311 | /** |
| 2312 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2313 | * the obscuring window. |
| 2314 | */ |
| 2315 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2316 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2317 | sp<FakeWindowHandle> window = |
| 2318 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2319 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2320 | |
| 2321 | // Only a single window is present at first |
| 2322 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2323 | |
| 2324 | // Start hovering in the window |
| 2325 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2326 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2327 | .build()); |
| 2328 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2329 | |
| 2330 | // Now, an obscuring window appears! |
| 2331 | sp<FakeWindowHandle> obscuringWindow = |
| 2332 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2333 | ADISPLAY_ID_DEFAULT, |
| 2334 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2335 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2336 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2337 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2338 | obscuringWindow->setNoInputChannel(true); |
| 2339 | obscuringWindow->setFocusable(false); |
| 2340 | obscuringWindow->setAlpha(1.0); |
| 2341 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2342 | |
| 2343 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2344 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2345 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2346 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2347 | .build()); |
| 2348 | obscuringWindow->assertNoEvents(); |
| 2349 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2350 | |
| 2351 | // Now the obscuring window goes away. |
| 2352 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2353 | |
| 2354 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2355 | // so it should generate a HOVER_ENTER |
| 2356 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2357 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2358 | .build()); |
| 2359 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2360 | |
| 2361 | // Now the MOVE should be getting dispatched normally |
| 2362 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2363 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2364 | .build()); |
| 2365 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2366 | } |
| 2367 | |
| 2368 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2369 | * Two windows: a window on the left and a window on the right. |
| 2370 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2371 | * down. Then, on the left window, also place second touch pointer down. |
| 2372 | * This test tries to reproduce a crash. |
| 2373 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2374 | */ |
| 2375 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { |
| 2376 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2377 | sp<FakeWindowHandle> leftWindow = |
| 2378 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2379 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2380 | |
| 2381 | sp<FakeWindowHandle> rightWindow = |
| 2382 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2383 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2384 | |
| 2385 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2386 | |
| 2387 | const int32_t touchDeviceId = 4; |
| 2388 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2389 | |
| 2390 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2391 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2392 | .deviceId(mouseDeviceId) |
| 2393 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2394 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2395 | leftWindow->consumeMotionEvent( |
| 2396 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2397 | |
| 2398 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2399 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2400 | .deviceId(mouseDeviceId) |
| 2401 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2402 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2403 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2404 | |
| 2405 | leftWindow->consumeMotionEvent( |
| 2406 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2407 | leftWindow->consumeMotionEvent( |
| 2408 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2409 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2410 | mDispatcher->notifyMotion( |
| 2411 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2412 | .deviceId(mouseDeviceId) |
| 2413 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2414 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2415 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2416 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2417 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2418 | |
| 2419 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2420 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2421 | .deviceId(touchDeviceId) |
| 2422 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2423 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2424 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2425 | |
| 2426 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2427 | |
| 2428 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2429 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2430 | .deviceId(touchDeviceId) |
| 2431 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2432 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2433 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2434 | leftWindow->consumeMotionEvent( |
| 2435 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2436 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2437 | // current implementation. |
| 2438 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2439 | rightWindow->consumeMotionEvent( |
| 2440 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2441 | |
| 2442 | leftWindow->assertNoEvents(); |
| 2443 | rightWindow->assertNoEvents(); |
| 2444 | } |
| 2445 | |
| 2446 | /** |
| 2447 | * On a single window, use two different devices: mouse and touch. |
| 2448 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2449 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2450 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2451 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2452 | * represent a new gesture. |
| 2453 | */ |
| 2454 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { |
| 2455 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2456 | sp<FakeWindowHandle> window = |
| 2457 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2458 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2459 | |
| 2460 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2461 | |
| 2462 | const int32_t touchDeviceId = 4; |
| 2463 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2464 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2465 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2466 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2467 | .deviceId(touchDeviceId) |
| 2468 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2469 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2470 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2471 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2472 | .deviceId(touchDeviceId) |
| 2473 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2474 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2475 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2476 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2477 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2478 | .deviceId(touchDeviceId) |
| 2479 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2480 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2481 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2482 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2483 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2484 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2485 | |
| 2486 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2487 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2488 | .deviceId(mouseDeviceId) |
| 2489 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2490 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2491 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2492 | |
| 2493 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2494 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2495 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2496 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2497 | mDispatcher->notifyMotion( |
| 2498 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2499 | .deviceId(mouseDeviceId) |
| 2500 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2501 | .actionButton(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 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2505 | |
| 2506 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2507 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2508 | .deviceId(touchDeviceId) |
| 2509 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2510 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2511 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2512 | // The pointer_down event should be ignored |
| 2513 | window->assertNoEvents(); |
| 2514 | } |
| 2515 | |
| 2516 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2517 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 2518 | * the injected event. |
| 2519 | */ |
| 2520 | TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) { |
| 2521 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2522 | sp<FakeWindowHandle> window = |
| 2523 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2524 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2525 | |
| 2526 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2527 | |
| 2528 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2529 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 2530 | // completion. |
| 2531 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2532 | injectMotionEvent(mDispatcher, |
| 2533 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2534 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2535 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2536 | .x(50) |
| 2537 | .y(50)) |
| 2538 | .build())); |
| 2539 | window->consumeMotionEvent( |
| 2540 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2541 | |
| 2542 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 2543 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2544 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2545 | .deviceId(touchDeviceId) |
| 2546 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2547 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2548 | |
| 2549 | window->consumeMotionEvent( |
| 2550 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2551 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2552 | } |
| 2553 | |
| 2554 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2555 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2556 | * |
| 2557 | * Two windows: a window on the left and a window on the right. |
| 2558 | * Mouse is hovered over the left window. |
| 2559 | * Next, we tap on the left window, where the cursor was last seen. |
| 2560 | * |
| 2561 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2562 | * the left window. |
| 2563 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2564 | * window (first), and then another on the left window (second). |
| 2565 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2566 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2567 | */ |
| 2568 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2569 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2570 | sp<FakeWindowHandle> leftWindow = |
| 2571 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2572 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2573 | |
| 2574 | sp<FakeWindowHandle> rightWindow = |
| 2575 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2576 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2577 | |
| 2578 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2579 | |
| 2580 | const int32_t mouseDeviceId = 6; |
| 2581 | const int32_t touchDeviceId = 4; |
| 2582 | // Hover over the left window. Keep the cursor there. |
| 2583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2584 | injectMotionEvent(mDispatcher, |
| 2585 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2586 | AINPUT_SOURCE_MOUSE) |
| 2587 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2588 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2589 | .x(50) |
| 2590 | .y(50)) |
| 2591 | .build())); |
| 2592 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2593 | |
| 2594 | // Tap on left window |
| 2595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2596 | injectMotionEvent(mDispatcher, |
| 2597 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2598 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2599 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2600 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2601 | .x(100) |
| 2602 | .y(100)) |
| 2603 | .build())); |
| 2604 | |
| 2605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2606 | injectMotionEvent(mDispatcher, |
| 2607 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2608 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2609 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2610 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2611 | .x(100) |
| 2612 | .y(100)) |
| 2613 | .build())); |
| 2614 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2615 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2616 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2617 | |
| 2618 | // First finger down on right window |
| 2619 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2620 | injectMotionEvent(mDispatcher, |
| 2621 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2622 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2623 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2624 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2625 | .x(300) |
| 2626 | .y(100)) |
| 2627 | .build())); |
| 2628 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2629 | |
| 2630 | // Second finger down on the left window |
| 2631 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2632 | injectMotionEvent(mDispatcher, |
| 2633 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2634 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2635 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2636 | .x(300) |
| 2637 | .y(100)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2638 | .pointer(PointerBuilder(1, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2639 | .x(100) |
| 2640 | .y(100)) |
| 2641 | .build())); |
| 2642 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2643 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2644 | |
| 2645 | // No more events |
| 2646 | leftWindow->assertNoEvents(); |
| 2647 | rightWindow->assertNoEvents(); |
| 2648 | } |
| 2649 | |
| 2650 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2651 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2652 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2653 | * touch is gone, stylus hovering should start working again. |
| 2654 | */ |
| 2655 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 2656 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2657 | sp<FakeWindowHandle> window = |
| 2658 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2659 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2660 | |
| 2661 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2662 | |
| 2663 | const int32_t stylusDeviceId = 5; |
| 2664 | const int32_t touchDeviceId = 4; |
| 2665 | // Start hovering with stylus |
| 2666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2667 | injectMotionEvent(mDispatcher, |
| 2668 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2669 | AINPUT_SOURCE_STYLUS) |
| 2670 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2671 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2672 | .x(50) |
| 2673 | .y(50)) |
| 2674 | .build())); |
| 2675 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2676 | |
| 2677 | // Finger down on the window |
| 2678 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2679 | injectMotionEvent(mDispatcher, |
| 2680 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2681 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2682 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2683 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2684 | .x(100) |
| 2685 | .y(100)) |
| 2686 | .build())); |
| 2687 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2688 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2689 | |
| 2690 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2691 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2692 | injectMotionEvent(mDispatcher, |
| 2693 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2694 | AINPUT_SOURCE_STYLUS) |
| 2695 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame^] | 2696 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2697 | .build())); |
| 2698 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2699 | // is already down. |
| 2700 | |
| 2701 | // Lift up the finger |
| 2702 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2703 | injectMotionEvent(mDispatcher, |
| 2704 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2705 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2706 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2707 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2708 | .x(100) |
| 2709 | .y(100)) |
| 2710 | .build())); |
| 2711 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2712 | |
| 2713 | // Now that the touch is gone, stylus hovering should start working again |
| 2714 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2715 | injectMotionEvent(mDispatcher, |
| 2716 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2717 | AINPUT_SOURCE_STYLUS) |
| 2718 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame^] | 2719 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2720 | .build())); |
| 2721 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2722 | // No more events |
| 2723 | window->assertNoEvents(); |
| 2724 | } |
| 2725 | |
| 2726 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2727 | * A spy window above a window with no input channel. |
| 2728 | * Start hovering with a stylus device, and then tap with it. |
| 2729 | * Ensure spy window receives the entire sequence. |
| 2730 | */ |
| 2731 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 2732 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2733 | sp<FakeWindowHandle> spyWindow = |
| 2734 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2735 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2736 | spyWindow->setTrustedOverlay(true); |
| 2737 | spyWindow->setSpy(true); |
| 2738 | sp<FakeWindowHandle> window = |
| 2739 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2740 | window->setNoInputChannel(true); |
| 2741 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2742 | |
| 2743 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2744 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2745 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2746 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2747 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2748 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2749 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2750 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2751 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2752 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2753 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2754 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2755 | |
| 2756 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2757 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2758 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2759 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2760 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2761 | |
| 2762 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2763 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, 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_UP)); |
| 2767 | |
| 2768 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2769 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2770 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2771 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2772 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2773 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2774 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, 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_HOVER_EXIT)); |
| 2778 | |
| 2779 | // No more events |
| 2780 | spyWindow->assertNoEvents(); |
| 2781 | window->assertNoEvents(); |
| 2782 | } |
| 2783 | |
| 2784 | /** |
| 2785 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 2786 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 2787 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 2788 | * While the mouse is down, new move events from the touch device should be ignored. |
| 2789 | */ |
| 2790 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 2791 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2792 | sp<FakeWindowHandle> spyWindow = |
| 2793 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2794 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2795 | spyWindow->setTrustedOverlay(true); |
| 2796 | spyWindow->setSpy(true); |
| 2797 | sp<FakeWindowHandle> window = |
| 2798 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2799 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2800 | |
| 2801 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2802 | |
| 2803 | const int32_t mouseDeviceId = 7; |
| 2804 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2805 | |
| 2806 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2807 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2808 | .deviceId(mouseDeviceId) |
| 2809 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2810 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2811 | spyWindow->consumeMotionEvent( |
| 2812 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2813 | window->consumeMotionEvent( |
| 2814 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2815 | |
| 2816 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2817 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2818 | .deviceId(touchDeviceId) |
| 2819 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 2820 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2821 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2822 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2823 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2824 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2825 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2826 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2827 | .deviceId(touchDeviceId) |
| 2828 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 2829 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2830 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2831 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2832 | |
| 2833 | // Pilfer the stream |
| 2834 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2835 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2836 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2837 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2838 | .deviceId(touchDeviceId) |
| 2839 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 2840 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2841 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2842 | |
| 2843 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2844 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2845 | .deviceId(mouseDeviceId) |
| 2846 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2847 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2848 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2849 | |
| 2850 | spyWindow->consumeMotionEvent( |
| 2851 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2852 | spyWindow->consumeMotionEvent( |
| 2853 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2854 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2855 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2856 | mDispatcher->notifyMotion( |
| 2857 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2858 | .deviceId(mouseDeviceId) |
| 2859 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2860 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2861 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2862 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2863 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2864 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2865 | |
| 2866 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2867 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 2868 | .deviceId(mouseDeviceId) |
| 2869 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2870 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 2871 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2872 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2873 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2874 | |
| 2875 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2876 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2877 | .deviceId(touchDeviceId) |
| 2878 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 2879 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2880 | |
| 2881 | // No more events |
| 2882 | spyWindow->assertNoEvents(); |
| 2883 | window->assertNoEvents(); |
| 2884 | } |
| 2885 | |
| 2886 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2887 | * On the display, have a single window, and also an area where there's no window. |
| 2888 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2889 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2890 | */ |
| 2891 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2892 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2893 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2894 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2895 | |
| 2896 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2897 | |
| 2898 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2899 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2900 | |
| 2901 | mDispatcher->waitForIdle(); |
| 2902 | window->assertNoEvents(); |
| 2903 | |
| 2904 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2905 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2906 | mDispatcher->waitForIdle(); |
| 2907 | window->consumeMotionDown(); |
| 2908 | } |
| 2909 | |
| 2910 | /** |
| 2911 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2912 | * non-touchable window. |
| 2913 | */ |
| 2914 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2915 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2916 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2917 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2918 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2919 | window1->setTouchable(false); |
| 2920 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2921 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2922 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2923 | |
| 2924 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2925 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2926 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2927 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2928 | |
| 2929 | mDispatcher->waitForIdle(); |
| 2930 | window1->assertNoEvents(); |
| 2931 | window2->assertNoEvents(); |
| 2932 | |
| 2933 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2934 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2935 | mDispatcher->waitForIdle(); |
| 2936 | window2->consumeMotionDown(); |
| 2937 | } |
| 2938 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2939 | /** |
| 2940 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2941 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2942 | */ |
| 2943 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2944 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2945 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2947 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2948 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2949 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2950 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2951 | |
| 2952 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2953 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2954 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2955 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2956 | |
| 2957 | mDispatcher->waitForIdle(); |
| 2958 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2959 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2960 | window2->assertNoEvents(); |
| 2961 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2962 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2963 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2964 | |
| 2965 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2966 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2967 | mDispatcher->waitForIdle(); |
| 2968 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2969 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2970 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2971 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2972 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2973 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2974 | |
| 2975 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2976 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2977 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2978 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2979 | |
| 2980 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2981 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2982 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2983 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2984 | |
| 2985 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2986 | window1->consumeMotionMove(); |
| 2987 | window1->assertNoEvents(); |
| 2988 | |
| 2989 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2990 | mDispatcher->notifyMotion( |
| 2991 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2992 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2993 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2994 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2995 | mDispatcher->notifyMotion( |
| 2996 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2997 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2998 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3001 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3002 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3003 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3004 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3005 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3006 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3007 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3008 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3009 | |
| 3010 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3011 | |
| 3012 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 3013 | |
| 3014 | // 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] | 3015 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3016 | injectMotionEvent(mDispatcher, |
| 3017 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3018 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3019 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3020 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3021 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3022 | |
| 3023 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3025 | injectMotionEvent(mDispatcher, |
| 3026 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3027 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3028 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3029 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3030 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3031 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3032 | |
| 3033 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3034 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3035 | injectMotionEvent(mDispatcher, |
| 3036 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3037 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3038 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3039 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3040 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3041 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3042 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3043 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3044 | injectMotionEvent(mDispatcher, |
| 3045 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3046 | AINPUT_SOURCE_MOUSE) |
| 3047 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3048 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3049 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3050 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3051 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3052 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3053 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3054 | injectMotionEvent(mDispatcher, |
| 3055 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3056 | AINPUT_SOURCE_MOUSE) |
| 3057 | .buttonState(0) |
| 3058 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3059 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3060 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3061 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3062 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3063 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3064 | injectMotionEvent(mDispatcher, |
| 3065 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3066 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3067 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3068 | .build())); |
| 3069 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3070 | |
| 3071 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3073 | injectMotionEvent(mDispatcher, |
| 3074 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3075 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3076 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).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 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3079 | |
| 3080 | // No more events |
| 3081 | windowLeft->assertNoEvents(); |
| 3082 | windowRight->assertNoEvents(); |
| 3083 | } |
| 3084 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3085 | /** |
| 3086 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3087 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3088 | * currently active gesture should be canceled, and the new one should proceed. |
| 3089 | */ |
| 3090 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3091 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3092 | sp<FakeWindowHandle> window = |
| 3093 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3094 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3095 | |
| 3096 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3097 | |
| 3098 | const int32_t touchDeviceId = 4; |
| 3099 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3100 | |
| 3101 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3102 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3103 | .deviceId(touchDeviceId) |
| 3104 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3105 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3106 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3107 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3108 | .deviceId(touchDeviceId) |
| 3109 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3110 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3111 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3112 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3113 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3114 | |
| 3115 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3116 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3117 | .deviceId(mouseDeviceId) |
| 3118 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3119 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3120 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3121 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3122 | WithPointerCount(2u))); |
| 3123 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3124 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3125 | mDispatcher->notifyMotion( |
| 3126 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3127 | .deviceId(mouseDeviceId) |
| 3128 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3129 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3130 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3131 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3132 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3133 | |
| 3134 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3135 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3136 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3137 | .deviceId(touchDeviceId) |
| 3138 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3139 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3140 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3141 | window->assertNoEvents(); |
| 3142 | } |
| 3143 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3144 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3145 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3146 | |
| 3147 | sp<FakeWindowHandle> spyWindow = |
| 3148 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3149 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3150 | spyWindow->setTrustedOverlay(true); |
| 3151 | spyWindow->setSpy(true); |
| 3152 | sp<FakeWindowHandle> window = |
| 3153 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3154 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3155 | |
| 3156 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3157 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3158 | |
| 3159 | // Send mouse cursor to the window |
| 3160 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3161 | injectMotionEvent(mDispatcher, |
| 3162 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3163 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3164 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3165 | .x(100) |
| 3166 | .y(100)) |
| 3167 | .build())); |
| 3168 | |
| 3169 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3170 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3171 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3172 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3173 | |
| 3174 | window->assertNoEvents(); |
| 3175 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3178 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3179 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3180 | |
| 3181 | sp<FakeWindowHandle> spyWindow = |
| 3182 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3183 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3184 | spyWindow->setTrustedOverlay(true); |
| 3185 | spyWindow->setSpy(true); |
| 3186 | sp<FakeWindowHandle> window = |
| 3187 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3188 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3189 | |
| 3190 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3191 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3192 | |
| 3193 | // Send mouse cursor to the window |
| 3194 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3195 | injectMotionEvent(mDispatcher, |
| 3196 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3197 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3198 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3199 | .x(100) |
| 3200 | .y(100)) |
| 3201 | .build())); |
| 3202 | |
| 3203 | // Move mouse cursor |
| 3204 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3205 | injectMotionEvent(mDispatcher, |
| 3206 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3207 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3208 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3209 | .x(110) |
| 3210 | .y(110)) |
| 3211 | .build())); |
| 3212 | |
| 3213 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3214 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3215 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3216 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3217 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3218 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3219 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3220 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3221 | // Touch down on the window |
| 3222 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3223 | injectMotionEvent(mDispatcher, |
| 3224 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3225 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3226 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3227 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3228 | .x(200) |
| 3229 | .y(200)) |
| 3230 | .build())); |
| 3231 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3232 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3233 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3234 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3235 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3236 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3237 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3238 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3239 | |
| 3240 | // pilfer the motion, retaining the gesture on the spy window. |
| 3241 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3242 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3243 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3244 | |
| 3245 | // Touch UP on the window |
| 3246 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3247 | injectMotionEvent(mDispatcher, |
| 3248 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3249 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3250 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3251 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3252 | .x(200) |
| 3253 | .y(200)) |
| 3254 | .build())); |
| 3255 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3256 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3257 | |
| 3258 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3259 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3260 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3261 | |
| 3262 | // One more tap - DOWN |
| 3263 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3264 | injectMotionEvent(mDispatcher, |
| 3265 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 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(250) |
| 3270 | .y(250)) |
| 3271 | .build())); |
| 3272 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3273 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3274 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3275 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3276 | |
| 3277 | // Touch UP on the window |
| 3278 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3279 | injectMotionEvent(mDispatcher, |
| 3280 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3281 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3282 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3283 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3284 | .x(250) |
| 3285 | .y(250)) |
| 3286 | .build())); |
| 3287 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3288 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3289 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3290 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3291 | |
| 3292 | window->assertNoEvents(); |
| 3293 | spyWindow->assertNoEvents(); |
| 3294 | } |
| 3295 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3296 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3297 | // directly in this test. |
| 3298 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3299 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3300 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3301 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3302 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3303 | |
| 3304 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3305 | |
| 3306 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3307 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3308 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3309 | injectMotionEvent(mDispatcher, |
| 3310 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3311 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3312 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3313 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3314 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3315 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3316 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3317 | injectMotionEvent(mDispatcher, |
| 3318 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3319 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3320 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3321 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3322 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3323 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 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_BUTTON_PRESS, |
| 3328 | AINPUT_SOURCE_MOUSE) |
| 3329 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3330 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3331 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3332 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3333 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3334 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3335 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3336 | injectMotionEvent(mDispatcher, |
| 3337 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3338 | AINPUT_SOURCE_MOUSE) |
| 3339 | .buttonState(0) |
| 3340 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3341 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3342 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3343 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3344 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3346 | injectMotionEvent(mDispatcher, |
| 3347 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3348 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3349 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3350 | .build())); |
| 3351 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3352 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3353 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 3354 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 3355 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3356 | injectMotionEvent(mDispatcher, |
| 3357 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3358 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3359 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3360 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3361 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3364 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3365 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3366 | * is generated. |
| 3367 | */ |
| 3368 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3369 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3370 | sp<FakeWindowHandle> window = |
| 3371 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3372 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3373 | |
| 3374 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3375 | |
| 3376 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3377 | |
| 3378 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3379 | injectMotionEvent(mDispatcher, |
| 3380 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3381 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3382 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3383 | .x(300) |
| 3384 | .y(400)) |
| 3385 | .build())); |
| 3386 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3387 | |
| 3388 | // Remove the window, but keep the channel. |
| 3389 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3390 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3391 | } |
| 3392 | |
| 3393 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3394 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3395 | */ |
| 3396 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3397 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3398 | sp<FakeWindowHandle> window = |
| 3399 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3400 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3401 | |
| 3402 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3403 | |
| 3404 | const int32_t mouseDeviceId = 7; |
| 3405 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3406 | |
| 3407 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3408 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3409 | .deviceId(mouseDeviceId) |
| 3410 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 3411 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3412 | window->consumeMotionEvent( |
| 3413 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3414 | |
| 3415 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3416 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3417 | .deviceId(touchDeviceId) |
| 3418 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3419 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3420 | |
| 3421 | window->consumeMotionEvent( |
| 3422 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3423 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3424 | } |
| 3425 | |
| 3426 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3427 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3428 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3429 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3430 | */ |
| 3431 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3432 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3433 | sp<FakeWindowHandle> window = |
| 3434 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3435 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3436 | |
| 3437 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3438 | |
| 3439 | // Inject a hover_move from mouse. |
| 3440 | NotifyMotionArgs motionArgs = |
| 3441 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 3442 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3443 | motionArgs.xCursorPosition = 50; |
| 3444 | motionArgs.yCursorPosition = 50; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3445 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3446 | ASSERT_NO_FATAL_FAILURE( |
| 3447 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3448 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3449 | |
| 3450 | // Tap on the window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3451 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3452 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3453 | {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3454 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3455 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3456 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3457 | |
| 3458 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3459 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3460 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3461 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3462 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3463 | ADISPLAY_ID_DEFAULT, {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3464 | ASSERT_NO_FATAL_FAILURE( |
| 3465 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3466 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3467 | } |
| 3468 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3469 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3470 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3471 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3472 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3473 | ADISPLAY_ID_DEFAULT); |
| 3474 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3475 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3476 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3477 | SECOND_DISPLAY_ID); |
| 3478 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3479 | |
| 3480 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3481 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3482 | |
| 3483 | // Set cursor position in window in default display and check that hover enter and move |
| 3484 | // events are generated. |
| 3485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3486 | injectMotionEvent(mDispatcher, |
| 3487 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3488 | AINPUT_SOURCE_MOUSE) |
| 3489 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3490 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3491 | .x(300) |
| 3492 | .y(600)) |
| 3493 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3494 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3495 | |
| 3496 | // Remove all windows in secondary display and check that no event happens on window in |
| 3497 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3498 | mDispatcher->setInputWindows( |
| 3499 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3500 | windowDefaultDisplay->assertNoEvents(); |
| 3501 | |
| 3502 | // Move cursor position in window in default display and check that only hover move |
| 3503 | // event is generated and not hover enter event. |
| 3504 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3505 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3506 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3507 | injectMotionEvent(mDispatcher, |
| 3508 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3509 | AINPUT_SOURCE_MOUSE) |
| 3510 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3511 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3512 | .x(400) |
| 3513 | .y(700)) |
| 3514 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3515 | windowDefaultDisplay->consumeMotionEvent( |
| 3516 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3517 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3518 | windowDefaultDisplay->assertNoEvents(); |
| 3519 | } |
| 3520 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3521 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3522 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3523 | |
| 3524 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3525 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3526 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3527 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3528 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3529 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3530 | |
| 3531 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3532 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3533 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3534 | |
| 3535 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3536 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3538 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3539 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3540 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3541 | windowRight->assertNoEvents(); |
| 3542 | } |
| 3543 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3544 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3545 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3546 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3547 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3548 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3549 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3550 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3551 | setFocusedWindow(window); |
| 3552 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3553 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3554 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3555 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3556 | |
| 3557 | // Window should receive key down event. |
| 3558 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3559 | |
| 3560 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3561 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3562 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3563 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3564 | AKEY_EVENT_FLAG_CANCELED); |
| 3565 | } |
| 3566 | |
| 3567 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3568 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3569 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3570 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3571 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3572 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3573 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3574 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3575 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3576 | |
| 3577 | // Window should receive motion down event. |
| 3578 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3579 | |
| 3580 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 3581 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3582 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3583 | window->consumeMotionEvent( |
| 3584 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3585 | } |
| 3586 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 3587 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 3588 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3589 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3590 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3591 | |
| 3592 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3593 | |
| 3594 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3595 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 3596 | .build()); |
| 3597 | |
| 3598 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3599 | |
| 3600 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 3601 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 3602 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3603 | |
| 3604 | // After the device has been reset, a new hovering stream can be sent to the window |
| 3605 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3606 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 3607 | .build()); |
| 3608 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3609 | } |
| 3610 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3611 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 3612 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3613 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3614 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3615 | window->setFocusable(true); |
| 3616 | |
| 3617 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3618 | setFocusedWindow(window); |
| 3619 | |
| 3620 | window->consumeFocusEvent(true); |
| 3621 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3622 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3623 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3624 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3625 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3626 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3627 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3628 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3629 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3630 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3631 | } |
| 3632 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 3633 | /** |
| 3634 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 3635 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3636 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3637 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3638 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3639 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3640 | window->setFocusable(true); |
| 3641 | |
| 3642 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3643 | setFocusedWindow(window); |
| 3644 | |
| 3645 | window->consumeFocusEvent(true); |
| 3646 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3647 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3648 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 3649 | |
| 3650 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 3651 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 3652 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 3653 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 3654 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3655 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3656 | } |
| 3657 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3658 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3659 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3660 | * WATCH_OUTSIDE_TOUCH. |
| 3661 | * Both windows are owned by the same UID. |
| 3662 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3663 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3664 | */ |
| 3665 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3666 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3667 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3668 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3669 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3670 | |
| 3671 | sp<FakeWindowHandle> outsideWindow = |
| 3672 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3673 | ADISPLAY_ID_DEFAULT); |
| 3674 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3675 | outsideWindow->setWatchOutsideTouch(true); |
| 3676 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3677 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3678 | |
| 3679 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3680 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3681 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3682 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3683 | window->consumeMotionDown(); |
| 3684 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3685 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3686 | outsideWindow->consumeMotionEvent( |
| 3687 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3688 | } |
| 3689 | |
| 3690 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3691 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3692 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3693 | * ACTION_OUTSIDE event is sent per gesture. |
| 3694 | */ |
| 3695 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3696 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 3697 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3698 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3699 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3700 | window->setWatchOutsideTouch(true); |
| 3701 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3702 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3703 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3704 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3705 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3706 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3707 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3708 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3709 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3710 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3711 | |
| 3712 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3713 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3714 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3715 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3716 | window->assertNoEvents(); |
| 3717 | secondWindow->assertNoEvents(); |
| 3718 | |
| 3719 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3720 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3721 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3722 | ADISPLAY_ID_DEFAULT, |
| 3723 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3724 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3725 | window->consumeMotionEvent( |
| 3726 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3727 | secondWindow->consumeMotionDown(); |
| 3728 | thirdWindow->assertNoEvents(); |
| 3729 | |
| 3730 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3731 | // 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] | 3732 | mDispatcher->notifyMotion( |
| 3733 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3734 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3735 | window->assertNoEvents(); |
| 3736 | secondWindow->consumeMotionMove(); |
| 3737 | thirdWindow->consumeMotionDown(); |
| 3738 | } |
| 3739 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3740 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3741 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3742 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3743 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3744 | window->setFocusable(true); |
| 3745 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3746 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3747 | setFocusedWindow(window); |
| 3748 | |
| 3749 | window->consumeFocusEvent(true); |
| 3750 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3751 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3752 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3753 | mDispatcher->notifyKey(keyDown); |
| 3754 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3755 | |
| 3756 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3757 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3758 | |
| 3759 | // 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] | 3760 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3761 | |
| 3762 | window->consumeFocusEvent(false); |
| 3763 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3764 | mDispatcher->notifyKey(keyDown); |
| 3765 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3766 | window->assertNoEvents(); |
| 3767 | } |
| 3768 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3769 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3770 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3771 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3772 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3773 | // Ensure window is non-split and have some transform. |
| 3774 | window->setPreventSplitting(true); |
| 3775 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3776 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3777 | |
| 3778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3779 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3780 | {50, 50})) |
| 3781 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3782 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3783 | |
| 3784 | const MotionEvent secondFingerDownEvent = |
| 3785 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3786 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3787 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3788 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 3789 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3790 | .build(); |
| 3791 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3792 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3793 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3794 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3795 | |
| 3796 | const MotionEvent* event = window->consumeMotion(); |
| 3797 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3798 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3799 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3800 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3801 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3802 | } |
| 3803 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame^] | 3804 | /** |
| 3805 | * Two windows: a splittable and a non-splittable. |
| 3806 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 3807 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 3808 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 3809 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 3810 | * "incomplete" gestures. |
| 3811 | */ |
| 3812 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 3813 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3814 | sp<FakeWindowHandle> leftWindow = |
| 3815 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 3816 | ADISPLAY_ID_DEFAULT); |
| 3817 | leftWindow->setPreventSplitting(false); |
| 3818 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3819 | sp<FakeWindowHandle> rightWindow = |
| 3820 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 3821 | ADISPLAY_ID_DEFAULT); |
| 3822 | rightWindow->setPreventSplitting(true); |
| 3823 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3824 | mDispatcher->onWindowInfosChanged( |
| 3825 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3826 | |
| 3827 | // Touch down on left, splittable window |
| 3828 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3829 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3830 | .build()); |
| 3831 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3832 | |
| 3833 | mDispatcher->notifyMotion( |
| 3834 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3835 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 3836 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 3837 | .build()); |
| 3838 | leftWindow->assertNoEvents(); |
| 3839 | rightWindow->assertNoEvents(); |
| 3840 | } |
| 3841 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 3842 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 3843 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3844 | sp<FakeWindowHandle> window = |
| 3845 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3846 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3847 | sp<FakeWindowHandle> trustedOverlay = |
| 3848 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 3849 | ADISPLAY_ID_DEFAULT); |
| 3850 | trustedOverlay->setSpy(true); |
| 3851 | trustedOverlay->setTrustedOverlay(true); |
| 3852 | |
| 3853 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}}); |
| 3854 | |
| 3855 | // Start a three-finger touchpad swipe |
| 3856 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3857 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3858 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3859 | .build()); |
| 3860 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 3861 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3862 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3863 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3864 | .build()); |
| 3865 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 3866 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3867 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3868 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 3869 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3870 | .build()); |
| 3871 | |
| 3872 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3873 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3874 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 3875 | |
| 3876 | // Move the swipe a bit |
| 3877 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3878 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3879 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3880 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3881 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3882 | .build()); |
| 3883 | |
| 3884 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3885 | |
| 3886 | // End the swipe |
| 3887 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 3888 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3889 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3890 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3891 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3892 | .build()); |
| 3893 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 3894 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3895 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3896 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3897 | .build()); |
| 3898 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3899 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3900 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3901 | .build()); |
| 3902 | |
| 3903 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 3904 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 3905 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3906 | |
| 3907 | window->assertNoEvents(); |
| 3908 | } |
| 3909 | |
| 3910 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 3911 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3912 | sp<FakeWindowHandle> window = |
| 3913 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3914 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3915 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3916 | |
| 3917 | // Start a three-finger touchpad swipe |
| 3918 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3919 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3920 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3921 | .build()); |
| 3922 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 3923 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3924 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3925 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3926 | .build()); |
| 3927 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 3928 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3929 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3930 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 3931 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3932 | .build()); |
| 3933 | |
| 3934 | // Move the swipe a bit |
| 3935 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3936 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3937 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3938 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3939 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3940 | .build()); |
| 3941 | |
| 3942 | // End the swipe |
| 3943 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 3944 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3945 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3946 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3947 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3948 | .build()); |
| 3949 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 3950 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3951 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3952 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3953 | .build()); |
| 3954 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3955 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3956 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3957 | .build()); |
| 3958 | |
| 3959 | window->assertNoEvents(); |
| 3960 | } |
| 3961 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3962 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 3963 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 3964 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 3965 | * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window. |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 3966 | */ |
| 3967 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 3968 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3969 | sp<FakeWindowHandle> window = |
| 3970 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3971 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3972 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3973 | |
| 3974 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3975 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3976 | .downTime(baseTime + 10) |
| 3977 | .eventTime(baseTime + 10) |
| 3978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3979 | .build()); |
| 3980 | |
| 3981 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3982 | |
| 3983 | // We need a new window object for the same window, because dispatcher will store objects by |
| 3984 | // reference. That means that the testing code and the dispatcher will refer to the same shared |
| 3985 | // object. Calling window->setTransform here would affect dispatcher's comparison |
| 3986 | // of the old window to the new window, since both the old window and the new window would be |
| 3987 | // updated to the same value. |
| 3988 | sp<FakeWindowHandle> windowDup = window->duplicate(); |
| 3989 | |
| 3990 | // Change the transform so that the orientation is now different from original. |
| 3991 | windowDup->setWindowTransform(0, -1, 1, 0); |
| 3992 | |
| 3993 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDup}}}); |
| 3994 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 3995 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3996 | .downTime(baseTime + 10) |
| 3997 | .eventTime(baseTime + 30) |
| 3998 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3999 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4000 | .build()); |
| 4001 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4002 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4003 | |
| 4004 | // Finish the gesture and start a new one. Ensure all events are sent to the window. |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4005 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4006 | .downTime(baseTime + 10) |
| 4007 | .eventTime(baseTime + 40) |
| 4008 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4009 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4010 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4011 | |
| 4012 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4013 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4014 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4015 | .downTime(baseTime + 10) |
| 4016 | .eventTime(baseTime + 50) |
| 4017 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4018 | .build()); |
| 4019 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4020 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4021 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4022 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4023 | .downTime(baseTime + 60) |
| 4024 | .eventTime(baseTime + 60) |
| 4025 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4026 | .build()); |
| 4027 | |
| 4028 | windowDup->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4029 | } |
| 4030 | |
| 4031 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4032 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4033 | * |
| 4034 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4035 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4036 | * space. |
| 4037 | */ |
| 4038 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4039 | public: |
| 4040 | void SetUp() override { |
| 4041 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4042 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4046 | gui::DisplayInfo info; |
| 4047 | info.displayId = displayId; |
| 4048 | info.transform = transform; |
| 4049 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4050 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4054 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4055 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4056 | } |
| 4057 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4058 | void removeAllWindowsAndDisplays() { |
| 4059 | mDisplayInfos.clear(); |
| 4060 | mWindowInfos.clear(); |
| 4061 | } |
| 4062 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4063 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4064 | // on the display. |
| 4065 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4066 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4067 | // respectively. |
| 4068 | ui::Transform displayTransform; |
| 4069 | displayTransform.set(2, 0, 0, 4); |
| 4070 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4071 | |
| 4072 | std::shared_ptr<FakeApplicationHandle> application = |
| 4073 | std::make_shared<FakeApplicationHandle>(); |
| 4074 | |
| 4075 | // Add two windows to the display. Their frames are represented in the display space. |
| 4076 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4077 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4078 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4079 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 4080 | addWindow(firstWindow); |
| 4081 | |
| 4082 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4083 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4084 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4085 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 4086 | addWindow(secondWindow); |
| 4087 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 4088 | } |
| 4089 | |
| 4090 | private: |
| 4091 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 4092 | std::vector<gui::WindowInfo> mWindowInfos; |
| 4093 | }; |
| 4094 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4095 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4096 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4097 | // 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] | 4098 | // selected so that if the hit test was performed with the point and the bounds being in |
| 4099 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4100 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4101 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4102 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4103 | |
| 4104 | firstWindow->consumeMotionDown(); |
| 4105 | secondWindow->assertNoEvents(); |
| 4106 | } |
| 4107 | |
| 4108 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 4109 | // the event should be treated as being in the logical display space. |
| 4110 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 4111 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4112 | // Send down to the first window. The point is represented in the logical display space. The |
| 4113 | // point is selected so that if the hit test was done in logical display space, then it would |
| 4114 | // end up in the incorrect window. |
| 4115 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4116 | PointF{75 * 2, 55 * 4}); |
| 4117 | |
| 4118 | firstWindow->consumeMotionDown(); |
| 4119 | secondWindow->assertNoEvents(); |
| 4120 | } |
| 4121 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4122 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 4123 | // event should be treated as being in the logical display space. |
| 4124 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 4125 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4126 | |
| 4127 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4128 | ui::Transform injectedEventTransform; |
| 4129 | injectedEventTransform.set(matrix); |
| 4130 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 4131 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 4132 | |
| 4133 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4134 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4135 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4136 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4137 | .x(untransformedPoint.x) |
| 4138 | .y(untransformedPoint.y)) |
| 4139 | .build(); |
| 4140 | event.transform(matrix); |
| 4141 | |
| 4142 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 4143 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4144 | |
| 4145 | firstWindow->consumeMotionDown(); |
| 4146 | secondWindow->assertNoEvents(); |
| 4147 | } |
| 4148 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4149 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4150 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4151 | |
| 4152 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4153 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4154 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4155 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4156 | |
| 4157 | firstWindow->assertNoEvents(); |
| 4158 | const MotionEvent* event = secondWindow->consumeMotion(); |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 4159 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4160 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 4161 | |
| 4162 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 4163 | EXPECT_EQ(300, event->getRawX(0)); |
| 4164 | EXPECT_EQ(880, event->getRawY(0)); |
| 4165 | |
| 4166 | // Ensure that the x and y values are in the window's coordinate space. |
| 4167 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4168 | // the logical display space. This will be the origin of the window space. |
| 4169 | EXPECT_EQ(100, event->getX(0)); |
| 4170 | EXPECT_EQ(80, event->getY(0)); |
| 4171 | } |
| 4172 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4173 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4174 | class InputDispatcherDisplayOrientationFixture |
| 4175 | : public InputDispatcherDisplayProjectionTest, |
| 4176 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 4177 | |
| 4178 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 4179 | // in different locations on the display, specifically points close to the four corners of a |
| 4180 | // window. |
| 4181 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 4182 | constexpr static int32_t displayWidth = 400; |
| 4183 | constexpr static int32_t displayHeight = 800; |
| 4184 | |
| 4185 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4186 | |
| 4187 | const auto rotation = GetParam(); |
| 4188 | |
| 4189 | // Set up the display with the specified rotation. |
| 4190 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4191 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4192 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4193 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4194 | logicalDisplayWidth, logicalDisplayHeight); |
| 4195 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4196 | |
| 4197 | // Create a window with its bounds determined in the logical display. |
| 4198 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 4199 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4200 | sp<FakeWindowHandle> window = |
| 4201 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4202 | window->setFrame(frameInDisplay, displayTransform); |
| 4203 | addWindow(window); |
| 4204 | |
| 4205 | // The following points in logical display space should be inside the window. |
| 4206 | static const std::array<vec2, 4> insidePoints{ |
| 4207 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4208 | for (const auto pointInsideWindow : insidePoints) { |
| 4209 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4210 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4211 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4212 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4213 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4214 | window->consumeMotionDown(); |
| 4215 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4216 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4217 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4218 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4219 | window->consumeMotionUp(); |
| 4220 | } |
| 4221 | |
| 4222 | // The following points in logical display space should be outside the window. |
| 4223 | static const std::array<vec2, 5> outsidePoints{ |
| 4224 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4225 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4226 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4227 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4228 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4229 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4230 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4231 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4232 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4233 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4234 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4235 | } |
| 4236 | window->assertNoEvents(); |
| 4237 | } |
| 4238 | |
| 4239 | // Run the precision tests for all rotations. |
| 4240 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4241 | InputDispatcherDisplayOrientationFixture, |
| 4242 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4243 | ui::ROTATION_270), |
| 4244 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4245 | return ftl::enum_string(testParamInfo.param); |
| 4246 | }); |
| 4247 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4248 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4249 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4250 | |
| 4251 | class TransferTouchFixture : public InputDispatcherTest, |
| 4252 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4253 | |
| 4254 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4255 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4256 | |
| 4257 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4258 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4259 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4260 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4261 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4262 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4263 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4264 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4265 | sp<FakeWindowHandle> wallpaper = |
| 4266 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 4267 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4268 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4269 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4270 | |
| 4271 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4272 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4273 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4274 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4275 | // Only the first window should get the down event |
| 4276 | firstWindow->consumeMotionDown(); |
| 4277 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4278 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4279 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4280 | // Transfer touch to the second window |
| 4281 | TransferFunction f = GetParam(); |
| 4282 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4283 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4284 | // The first window gets cancel and the second gets down |
| 4285 | firstWindow->consumeMotionCancel(); |
| 4286 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4287 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4288 | |
| 4289 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4290 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4291 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4292 | // The first window gets no events and the second gets up |
| 4293 | firstWindow->assertNoEvents(); |
| 4294 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4295 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4296 | } |
| 4297 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4298 | /** |
| 4299 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 4300 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 4301 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 4302 | * natural to the user. |
| 4303 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 4304 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 4305 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 4306 | * the first window to the second. |
| 4307 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 4308 | * the other API, as well. |
| 4309 | */ |
| 4310 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 4311 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4312 | |
| 4313 | // Create a couple of windows + a spy window |
| 4314 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4315 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4316 | spyWindow->setTrustedOverlay(true); |
| 4317 | spyWindow->setSpy(true); |
| 4318 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4319 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4320 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4321 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4322 | |
| 4323 | // Add the windows to the dispatcher |
| 4324 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 4325 | |
| 4326 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4327 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4328 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4329 | // Only the first window and spy should get the down event |
| 4330 | spyWindow->consumeMotionDown(); |
| 4331 | firstWindow->consumeMotionDown(); |
| 4332 | |
| 4333 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 4334 | // if f === 'transferTouch'. |
| 4335 | TransferFunction f = GetParam(); |
| 4336 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4337 | ASSERT_TRUE(success); |
| 4338 | // The first window gets cancel and the second gets down |
| 4339 | firstWindow->consumeMotionCancel(); |
| 4340 | secondWindow->consumeMotionDown(); |
| 4341 | |
| 4342 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4343 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4344 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4345 | // The first window gets no events and the second+spy get up |
| 4346 | firstWindow->assertNoEvents(); |
| 4347 | spyWindow->consumeMotionUp(); |
| 4348 | secondWindow->consumeMotionUp(); |
| 4349 | } |
| 4350 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4351 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4352 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4353 | |
| 4354 | PointF touchPoint = {10, 10}; |
| 4355 | |
| 4356 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4357 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4358 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4359 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4360 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4361 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4362 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4363 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4364 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4365 | |
| 4366 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4367 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4368 | |
| 4369 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4370 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4371 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4372 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4373 | // Only the first window should get the down event |
| 4374 | firstWindow->consumeMotionDown(); |
| 4375 | secondWindow->assertNoEvents(); |
| 4376 | |
| 4377 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4378 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4379 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4380 | // Only the first window should get the pointer down event |
| 4381 | firstWindow->consumeMotionPointerDown(1); |
| 4382 | secondWindow->assertNoEvents(); |
| 4383 | |
| 4384 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4385 | TransferFunction f = GetParam(); |
| 4386 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4387 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4388 | // The first window gets cancel and the second gets down and pointer down |
| 4389 | firstWindow->consumeMotionCancel(); |
| 4390 | secondWindow->consumeMotionDown(); |
| 4391 | secondWindow->consumeMotionPointerDown(1); |
| 4392 | |
| 4393 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4394 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4395 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4396 | // The first window gets nothing and the second gets pointer up |
| 4397 | firstWindow->assertNoEvents(); |
| 4398 | secondWindow->consumeMotionPointerUp(1); |
| 4399 | |
| 4400 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4401 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4402 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4403 | // The first window gets nothing and the second gets up |
| 4404 | firstWindow->assertNoEvents(); |
| 4405 | secondWindow->consumeMotionUp(); |
| 4406 | } |
| 4407 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4408 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 4409 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4410 | |
| 4411 | // Create a couple of windows |
| 4412 | sp<FakeWindowHandle> firstWindow = |
| 4413 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4414 | ADISPLAY_ID_DEFAULT); |
| 4415 | firstWindow->setDupTouchToWallpaper(true); |
| 4416 | sp<FakeWindowHandle> secondWindow = |
| 4417 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4418 | ADISPLAY_ID_DEFAULT); |
| 4419 | secondWindow->setDupTouchToWallpaper(true); |
| 4420 | |
| 4421 | sp<FakeWindowHandle> wallpaper1 = |
| 4422 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 4423 | wallpaper1->setIsWallpaper(true); |
| 4424 | |
| 4425 | sp<FakeWindowHandle> wallpaper2 = |
| 4426 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 4427 | wallpaper2->setIsWallpaper(true); |
| 4428 | // Add the windows to the dispatcher |
| 4429 | mDispatcher->setInputWindows( |
| 4430 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 4431 | |
| 4432 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4433 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4434 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4435 | |
| 4436 | // Only the first window should get the down event |
| 4437 | firstWindow->consumeMotionDown(); |
| 4438 | secondWindow->assertNoEvents(); |
| 4439 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4440 | wallpaper2->assertNoEvents(); |
| 4441 | |
| 4442 | // Transfer touch focus to the second window |
| 4443 | TransferFunction f = GetParam(); |
| 4444 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4445 | ASSERT_TRUE(success); |
| 4446 | |
| 4447 | // The first window gets cancel and the second gets down |
| 4448 | firstWindow->consumeMotionCancel(); |
| 4449 | secondWindow->consumeMotionDown(); |
| 4450 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4451 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4452 | |
| 4453 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4454 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4455 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4456 | // The first window gets no events and the second gets up |
| 4457 | firstWindow->assertNoEvents(); |
| 4458 | secondWindow->consumeMotionUp(); |
| 4459 | wallpaper1->assertNoEvents(); |
| 4460 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4461 | } |
| 4462 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4463 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 4464 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 4465 | // for the case where there are multiple pointers split across several windows. |
| 4466 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 4467 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4468 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4469 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4470 | return dispatcher->transferTouch(destChannelToken, |
| 4471 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4472 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4473 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4474 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4475 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4476 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4477 | })); |
| 4478 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4479 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4480 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4481 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4482 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4483 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4484 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4485 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4486 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4487 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4488 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4489 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4490 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4491 | |
| 4492 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4493 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4494 | |
| 4495 | PointF pointInFirst = {300, 200}; |
| 4496 | PointF pointInSecond = {300, 600}; |
| 4497 | |
| 4498 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4499 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4500 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4501 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4502 | // Only the first window should get the down event |
| 4503 | firstWindow->consumeMotionDown(); |
| 4504 | secondWindow->assertNoEvents(); |
| 4505 | |
| 4506 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4507 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4508 | ADISPLAY_ID_DEFAULT, |
| 4509 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4510 | // The first window gets a move and the second a down |
| 4511 | firstWindow->consumeMotionMove(); |
| 4512 | secondWindow->consumeMotionDown(); |
| 4513 | |
| 4514 | // Transfer touch focus to the second window |
| 4515 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4516 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 4517 | firstWindow->consumeMotionCancel(); |
| 4518 | secondWindow->consumeMotionPointerDown(1); |
| 4519 | |
| 4520 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4521 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4522 | ADISPLAY_ID_DEFAULT, |
| 4523 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4524 | // The first window gets nothing and the second gets pointer up |
| 4525 | firstWindow->assertNoEvents(); |
| 4526 | secondWindow->consumeMotionPointerUp(1); |
| 4527 | |
| 4528 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4529 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4530 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4531 | // The first window gets nothing and the second gets up |
| 4532 | firstWindow->assertNoEvents(); |
| 4533 | secondWindow->consumeMotionUp(); |
| 4534 | } |
| 4535 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4536 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 4537 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 4538 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 4539 | // window should get nothing. |
| 4540 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 4541 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4542 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4543 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4544 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4545 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4546 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4547 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4548 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4549 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4550 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4551 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4552 | |
| 4553 | // Add the windows to the dispatcher |
| 4554 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4555 | |
| 4556 | PointF pointInFirst = {300, 200}; |
| 4557 | PointF pointInSecond = {300, 600}; |
| 4558 | |
| 4559 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4560 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4561 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4562 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4563 | // Only the first window should get the down event |
| 4564 | firstWindow->consumeMotionDown(); |
| 4565 | secondWindow->assertNoEvents(); |
| 4566 | |
| 4567 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4568 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4569 | ADISPLAY_ID_DEFAULT, |
| 4570 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4571 | // The first window gets a move and the second a down |
| 4572 | firstWindow->consumeMotionMove(); |
| 4573 | secondWindow->consumeMotionDown(); |
| 4574 | |
| 4575 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4576 | const bool transferred = |
| 4577 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4578 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 4579 | ASSERT_FALSE(transferred); |
| 4580 | firstWindow->assertNoEvents(); |
| 4581 | secondWindow->assertNoEvents(); |
| 4582 | |
| 4583 | // The rest of the dispatch should proceed as normal |
| 4584 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4585 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4586 | ADISPLAY_ID_DEFAULT, |
| 4587 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4588 | // The first window gets MOVE and the second gets pointer up |
| 4589 | firstWindow->consumeMotionMove(); |
| 4590 | secondWindow->consumeMotionUp(); |
| 4591 | |
| 4592 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4593 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4594 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4595 | // The first window gets nothing and the second gets up |
| 4596 | firstWindow->consumeMotionUp(); |
| 4597 | secondWindow->assertNoEvents(); |
| 4598 | } |
| 4599 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4600 | // This case will create two windows and one mirrored window on the default display and mirror |
| 4601 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 4602 | // the windows info of second display before default display. |
| 4603 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 4604 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4605 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4606 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4607 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4608 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4609 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4610 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4611 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4612 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4613 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4614 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4615 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4616 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4617 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4618 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4619 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4620 | |
| 4621 | // Update window info, let it find window handle of second display first. |
| 4622 | mDispatcher->setInputWindows( |
| 4623 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4624 | {ADISPLAY_ID_DEFAULT, |
| 4625 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4626 | |
| 4627 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4628 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4629 | {50, 50})) |
| 4630 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4631 | |
| 4632 | // Window should receive motion event. |
| 4633 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4634 | |
| 4635 | // Transfer touch focus |
| 4636 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 4637 | secondWindowInPrimary->getToken())); |
| 4638 | // The first window gets cancel. |
| 4639 | firstWindowInPrimary->consumeMotionCancel(); |
| 4640 | secondWindowInPrimary->consumeMotionDown(); |
| 4641 | |
| 4642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4643 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4644 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4645 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4646 | firstWindowInPrimary->assertNoEvents(); |
| 4647 | secondWindowInPrimary->consumeMotionMove(); |
| 4648 | |
| 4649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4650 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4651 | {150, 50})) |
| 4652 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4653 | firstWindowInPrimary->assertNoEvents(); |
| 4654 | secondWindowInPrimary->consumeMotionUp(); |
| 4655 | } |
| 4656 | |
| 4657 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 4658 | // 'transferTouch' api. |
| 4659 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 4660 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4661 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4662 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4663 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4664 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4665 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4666 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4667 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4668 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4669 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4670 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4671 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4672 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4673 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4674 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4675 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4676 | |
| 4677 | // Update window info, let it find window handle of second display first. |
| 4678 | mDispatcher->setInputWindows( |
| 4679 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4680 | {ADISPLAY_ID_DEFAULT, |
| 4681 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4682 | |
| 4683 | // Touch on second display. |
| 4684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4685 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 4686 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4687 | |
| 4688 | // Window should receive motion event. |
| 4689 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4690 | |
| 4691 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4692 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4693 | |
| 4694 | // The first window gets cancel. |
| 4695 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4696 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4697 | |
| 4698 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4699 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4700 | SECOND_DISPLAY_ID, {150, 50})) |
| 4701 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4702 | firstWindowInPrimary->assertNoEvents(); |
| 4703 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 4704 | |
| 4705 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4706 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 4707 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4708 | firstWindowInPrimary->assertNoEvents(); |
| 4709 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 4710 | } |
| 4711 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4712 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4713 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4714 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4715 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4716 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4717 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4718 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4719 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4720 | |
| 4721 | window->consumeFocusEvent(true); |
| 4722 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4723 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4724 | |
| 4725 | // Window should receive key down event. |
| 4726 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4727 | |
| 4728 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4729 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4730 | mFakePolicy->assertUserActivityPoked(); |
| 4731 | } |
| 4732 | |
| 4733 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 4734 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4735 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4736 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4737 | |
| 4738 | window->setDisableUserActivity(true); |
| 4739 | window->setFocusable(true); |
| 4740 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4741 | setFocusedWindow(window); |
| 4742 | |
| 4743 | window->consumeFocusEvent(true); |
| 4744 | |
| 4745 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4746 | |
| 4747 | // Window should receive key down event. |
| 4748 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4749 | |
| 4750 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4751 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4752 | mFakePolicy->assertUserActivityNotPoked(); |
| 4753 | } |
| 4754 | |
| 4755 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 4756 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4757 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4758 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4759 | |
| 4760 | window->setFocusable(true); |
| 4761 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4762 | setFocusedWindow(window); |
| 4763 | |
| 4764 | window->consumeFocusEvent(true); |
| 4765 | |
| 4766 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4767 | mDispatcher->waitForIdle(); |
| 4768 | |
| 4769 | // System key is not passed down |
| 4770 | window->assertNoEvents(); |
| 4771 | |
| 4772 | // Should have poked user activity |
| 4773 | mFakePolicy->assertUserActivityPoked(); |
| 4774 | } |
| 4775 | |
| 4776 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 4777 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4778 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4779 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4780 | |
| 4781 | window->setFocusable(true); |
| 4782 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4783 | setFocusedWindow(window); |
| 4784 | |
| 4785 | window->consumeFocusEvent(true); |
| 4786 | |
| 4787 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4788 | mDispatcher->waitForIdle(); |
| 4789 | |
| 4790 | // System key is not passed down |
| 4791 | window->assertNoEvents(); |
| 4792 | |
| 4793 | // Should have poked user activity |
| 4794 | mFakePolicy->assertUserActivityPoked(); |
| 4795 | } |
| 4796 | |
| 4797 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 4798 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4799 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4800 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4801 | |
| 4802 | window->setDisableUserActivity(true); |
| 4803 | window->setFocusable(true); |
| 4804 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4805 | setFocusedWindow(window); |
| 4806 | |
| 4807 | window->consumeFocusEvent(true); |
| 4808 | |
| 4809 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4810 | mDispatcher->waitForIdle(); |
| 4811 | |
| 4812 | // System key is not passed down |
| 4813 | window->assertNoEvents(); |
| 4814 | |
| 4815 | // Should have poked user activity |
| 4816 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4817 | } |
| 4818 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4819 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 4820 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4821 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4822 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4823 | |
| 4824 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4825 | |
| 4826 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4827 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4828 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 4829 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4830 | |
| 4831 | window->consumeMotionEvent( |
| 4832 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4833 | |
| 4834 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4835 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4836 | mFakePolicy->assertUserActivityPoked(); |
| 4837 | } |
| 4838 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4839 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4840 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4841 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4842 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4843 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4844 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4845 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4846 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4847 | mDispatcher->waitForIdle(); |
| 4848 | |
| 4849 | window->assertNoEvents(); |
| 4850 | } |
| 4851 | |
| 4852 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4853 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4854 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4855 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4856 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4857 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4858 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4859 | |
| 4860 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4861 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4862 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4863 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4864 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4865 | |
| 4866 | // Window should receive only the motion event |
| 4867 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4868 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4869 | } |
| 4870 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4871 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4872 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4873 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4874 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4875 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4876 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4877 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4878 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4879 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4880 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4881 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4882 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4883 | |
| 4884 | // Add the windows to the dispatcher |
| 4885 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4886 | |
| 4887 | PointF pointInFirst = {300, 200}; |
| 4888 | PointF pointInSecond = {300, 600}; |
| 4889 | |
| 4890 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4891 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4892 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4893 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4894 | // Only the first window should get the down event |
| 4895 | firstWindow->consumeMotionDown(); |
| 4896 | secondWindow->assertNoEvents(); |
| 4897 | |
| 4898 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4899 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4900 | ADISPLAY_ID_DEFAULT, |
| 4901 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4902 | // The first window gets a move and the second a down |
| 4903 | firstWindow->consumeMotionMove(); |
| 4904 | secondWindow->consumeMotionDown(); |
| 4905 | |
| 4906 | // Send pointer cancel to the second window |
| 4907 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4908 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4909 | {pointInFirst, pointInSecond}); |
| 4910 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4911 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4912 | // The first window gets move and the second gets cancel. |
| 4913 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4914 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4915 | |
| 4916 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4917 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4918 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4919 | // The first window gets up and the second gets nothing. |
| 4920 | firstWindow->consumeMotionUp(); |
| 4921 | secondWindow->assertNoEvents(); |
| 4922 | } |
| 4923 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4924 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 4925 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4926 | |
| 4927 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4928 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4929 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4930 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 4931 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 4932 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 4933 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4934 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4935 | window->assertNoEvents(); |
| 4936 | mDispatcher->waitForIdle(); |
| 4937 | } |
| 4938 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4939 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4940 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4941 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4942 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4943 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4944 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4945 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4946 | } |
| 4947 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4948 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 4949 | |
| 4950 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4951 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 4952 | expectedFlags); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4953 | } |
| 4954 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4955 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 4956 | |
| 4957 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 4958 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4959 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4960 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4961 | expectedDisplayId, expectedFlags); |
| 4962 | } |
| 4963 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4964 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4965 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4966 | expectedDisplayId, expectedFlags); |
| 4967 | } |
| 4968 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4969 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4970 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4971 | expectedDisplayId, expectedFlags); |
| 4972 | } |
| 4973 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4974 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4975 | mInputReceiver->consumeMotionEvent( |
| 4976 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4977 | WithDisplayId(expectedDisplayId), |
| 4978 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4981 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 4982 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 4983 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4984 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4985 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4988 | MotionEvent* consumeMotion() { |
| 4989 | InputEvent* event = mInputReceiver->consume(); |
| 4990 | if (!event) { |
| 4991 | ADD_FAILURE() << "No event was produced"; |
| 4992 | return nullptr; |
| 4993 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4994 | if (event->getType() != InputEventType::MOTION) { |
| 4995 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4996 | return nullptr; |
| 4997 | } |
| 4998 | return static_cast<MotionEvent*>(event); |
| 4999 | } |
| 5000 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5001 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 5002 | |
| 5003 | private: |
| 5004 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5005 | }; |
| 5006 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5007 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 5008 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5009 | /** |
| 5010 | * Two entities that receive touch: A window, and a global monitor. |
| 5011 | * The touch goes to the window, and then the window disappears. |
| 5012 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 5013 | * for the monitor, as well. |
| 5014 | * 1. foregroundWindow |
| 5015 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 5016 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5017 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5018 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5019 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5020 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5021 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5022 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5023 | |
| 5024 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5025 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5026 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5027 | {100, 200})) |
| 5028 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5029 | |
| 5030 | // Both the foreground window and the global monitor should receive the touch down |
| 5031 | window->consumeMotionDown(); |
| 5032 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5033 | |
| 5034 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5035 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5036 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5037 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5038 | |
| 5039 | window->consumeMotionMove(); |
| 5040 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5041 | |
| 5042 | // Now the foreground window goes away |
| 5043 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5044 | window->consumeMotionCancel(); |
| 5045 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 5046 | |
| 5047 | // If more events come in, there will be no more foreground window to send them to. This will |
| 5048 | // cause a cancel for the monitor, as well. |
| 5049 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5050 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5051 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 5052 | << "Injection should fail because the window was removed"; |
| 5053 | window->assertNoEvents(); |
| 5054 | // Global monitor now gets the cancel |
| 5055 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5056 | } |
| 5057 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5058 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5059 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5060 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5061 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5062 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5063 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5064 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5065 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5066 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5067 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5068 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5069 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5070 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5073 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 5074 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5075 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5076 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5077 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5078 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5079 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5080 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5081 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5082 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5083 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5084 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5085 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5086 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5087 | // Pilfer pointers from the monitor. |
| 5088 | // This should not do anything and the window should continue to receive events. |
| 5089 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5090 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5091 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5092 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5093 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5094 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5095 | |
| 5096 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5097 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5100 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5101 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5102 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5103 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5104 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5105 | window->setWindowOffset(20, 40); |
| 5106 | window->setWindowTransform(0, 1, -1, 0); |
| 5107 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5108 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5109 | |
| 5110 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5111 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5112 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5113 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5114 | MotionEvent* event = monitor.consumeMotion(); |
| 5115 | // Even though window has transform, gesture monitor must not. |
| 5116 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 5117 | } |
| 5118 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5119 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5120 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5121 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5122 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5123 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5124 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5125 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 5126 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5129 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5130 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5131 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5132 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5133 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5134 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5135 | |
| 5136 | NotifyMotionArgs motionArgs = |
| 5137 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5138 | ADISPLAY_ID_DEFAULT); |
| 5139 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5140 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5141 | // Window should receive motion down event. |
| 5142 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5143 | |
| 5144 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5145 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5146 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5147 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5148 | motionArgs.pointerCoords[0].getX() - 10); |
| 5149 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5150 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5151 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5152 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5153 | } |
| 5154 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5155 | /** |
| 5156 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 5157 | * the device default right away. In the test scenario, we check both the default value, |
| 5158 | * and the action of enabling / disabling. |
| 5159 | */ |
| 5160 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5161 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5162 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5163 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5164 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5165 | |
| 5166 | // Set focused application. |
| 5167 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5168 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5169 | |
| 5170 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5171 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5172 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5173 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5174 | |
| 5175 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5176 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5177 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5178 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5179 | |
| 5180 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5181 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5182 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5183 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5184 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5185 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5186 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5187 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5188 | |
| 5189 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5190 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5191 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5192 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5193 | |
| 5194 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5195 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5196 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5197 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5198 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5199 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5200 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5201 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5202 | |
| 5203 | window->assertNoEvents(); |
| 5204 | } |
| 5205 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5206 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5207 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5208 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5209 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5210 | |
| 5211 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5212 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5213 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5214 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5215 | setFocusedWindow(window); |
| 5216 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5217 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5218 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5219 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5220 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5221 | |
| 5222 | InputEvent* event = window->consume(); |
| 5223 | ASSERT_NE(event, nullptr); |
| 5224 | |
| 5225 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5226 | ASSERT_NE(verified, nullptr); |
| 5227 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 5228 | |
| 5229 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 5230 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 5231 | ASSERT_EQ(keyArgs.source, verified->source); |
| 5232 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 5233 | |
| 5234 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 5235 | |
| 5236 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5237 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5238 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5239 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 5240 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 5241 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 5242 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 5243 | } |
| 5244 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5245 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5246 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5247 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5248 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5249 | |
| 5250 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5251 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5252 | ui::Transform transform; |
| 5253 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5254 | |
| 5255 | gui::DisplayInfo displayInfo; |
| 5256 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5257 | displayInfo.transform = transform; |
| 5258 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5259 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5260 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5261 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5262 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5263 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5264 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5265 | |
| 5266 | InputEvent* event = window->consume(); |
| 5267 | ASSERT_NE(event, nullptr); |
| 5268 | |
| 5269 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5270 | ASSERT_NE(verified, nullptr); |
| 5271 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 5272 | |
| 5273 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 5274 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 5275 | EXPECT_EQ(motionArgs.source, verified->source); |
| 5276 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 5277 | |
| 5278 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 5279 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5280 | const vec2 rawXY = |
| 5281 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 5282 | motionArgs.pointerCoords[0].getXYValue()); |
| 5283 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 5284 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5285 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5286 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5287 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5288 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 5289 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 5290 | } |
| 5291 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5292 | /** |
| 5293 | * Ensure that separate calls to sign the same data are generating the same key. |
| 5294 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 5295 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 5296 | * tests. |
| 5297 | */ |
| 5298 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 5299 | KeyEvent event = getTestKeyEvent(); |
| 5300 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5301 | |
| 5302 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 5303 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5304 | ASSERT_EQ(hmac1, hmac2); |
| 5305 | } |
| 5306 | |
| 5307 | /** |
| 5308 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 5309 | */ |
| 5310 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 5311 | KeyEvent event = getTestKeyEvent(); |
| 5312 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5313 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 5314 | |
| 5315 | verifiedEvent.deviceId += 1; |
| 5316 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5317 | |
| 5318 | verifiedEvent.source += 1; |
| 5319 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5320 | |
| 5321 | verifiedEvent.eventTimeNanos += 1; |
| 5322 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5323 | |
| 5324 | verifiedEvent.displayId += 1; |
| 5325 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5326 | |
| 5327 | verifiedEvent.action += 1; |
| 5328 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5329 | |
| 5330 | verifiedEvent.downTimeNanos += 1; |
| 5331 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5332 | |
| 5333 | verifiedEvent.flags += 1; |
| 5334 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5335 | |
| 5336 | verifiedEvent.keyCode += 1; |
| 5337 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5338 | |
| 5339 | verifiedEvent.scanCode += 1; |
| 5340 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5341 | |
| 5342 | verifiedEvent.metaState += 1; |
| 5343 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5344 | |
| 5345 | verifiedEvent.repeatCount += 1; |
| 5346 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5347 | } |
| 5348 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5349 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 5350 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5351 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5352 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5353 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5354 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5355 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5356 | |
| 5357 | // Top window is also focusable but is not granted focus. |
| 5358 | windowTop->setFocusable(true); |
| 5359 | windowSecond->setFocusable(true); |
| 5360 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5361 | setFocusedWindow(windowSecond); |
| 5362 | |
| 5363 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5364 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5365 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5366 | |
| 5367 | // Focused window should receive event. |
| 5368 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5369 | windowTop->assertNoEvents(); |
| 5370 | } |
| 5371 | |
| 5372 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5373 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5374 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5375 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5376 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5377 | |
| 5378 | window->setFocusable(true); |
| 5379 | // Release channel for window is no longer valid. |
| 5380 | window->releaseChannel(); |
| 5381 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5382 | setFocusedWindow(window); |
| 5383 | |
| 5384 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5385 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5386 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5387 | |
| 5388 | // window channel is invalid, so it should not receive any input event. |
| 5389 | window->assertNoEvents(); |
| 5390 | } |
| 5391 | |
| 5392 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5393 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5394 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5395 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5396 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5397 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5398 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5399 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5400 | setFocusedWindow(window); |
| 5401 | |
| 5402 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5403 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5404 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5405 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5406 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5407 | window->assertNoEvents(); |
| 5408 | } |
| 5409 | |
| 5410 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5411 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5412 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5413 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5414 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5415 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5416 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5417 | |
| 5418 | windowTop->setFocusable(true); |
| 5419 | windowSecond->setFocusable(true); |
| 5420 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5421 | setFocusedWindow(windowTop); |
| 5422 | windowTop->consumeFocusEvent(true); |
| 5423 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5424 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
| 5425 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5426 | windowSecond->consumeFocusEvent(true); |
| 5427 | windowTop->consumeFocusEvent(false); |
| 5428 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5430 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5431 | |
| 5432 | // Focused window should receive event. |
| 5433 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5434 | } |
| 5435 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5436 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5437 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5438 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5439 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5440 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5441 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5442 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5443 | |
| 5444 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5445 | windowSecond->setFocusable(false); |
| 5446 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5447 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5448 | setFocusedWindow(windowTop); |
| 5449 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5450 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5451 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5452 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5453 | |
| 5454 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5455 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5456 | windowSecond->assertNoEvents(); |
| 5457 | } |
| 5458 | |
| 5459 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5460 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5461 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5462 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5463 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5464 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5465 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5466 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5467 | |
| 5468 | window->setFocusable(true); |
| 5469 | previousFocusedWindow->setFocusable(true); |
| 5470 | window->setVisible(false); |
| 5471 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5472 | setFocusedWindow(previousFocusedWindow); |
| 5473 | previousFocusedWindow->consumeFocusEvent(true); |
| 5474 | |
| 5475 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5476 | setFocusedWindow(window); |
| 5477 | previousFocusedWindow->consumeFocusEvent(false); |
| 5478 | |
| 5479 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5480 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5481 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5482 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5483 | |
| 5484 | // Window does not get focus event or key down. |
| 5485 | window->assertNoEvents(); |
| 5486 | |
| 5487 | // Window becomes visible. |
| 5488 | window->setVisible(true); |
| 5489 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5490 | |
| 5491 | // Window receives focus event. |
| 5492 | window->consumeFocusEvent(true); |
| 5493 | // Focused window receives key down. |
| 5494 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5495 | } |
| 5496 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5497 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5498 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5499 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5500 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5501 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5502 | |
| 5503 | // window is granted focus. |
| 5504 | window->setFocusable(true); |
| 5505 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5506 | setFocusedWindow(window); |
| 5507 | window->consumeFocusEvent(true); |
| 5508 | |
| 5509 | // When a display is removed window loses focus. |
| 5510 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5511 | window->consumeFocusEvent(false); |
| 5512 | } |
| 5513 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5514 | /** |
| 5515 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5516 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5517 | * of the 'slipperyEnterWindow'. |
| 5518 | * |
| 5519 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5520 | * a way so that the touched location is no longer covered by the top window. |
| 5521 | * |
| 5522 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5523 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5524 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5525 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5526 | * with ACTION_DOWN). |
| 5527 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5528 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5529 | * |
| 5530 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5531 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5532 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5533 | * |
| 5534 | * In this test, we ensure that the event received by the bottom window has |
| 5535 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5536 | */ |
| 5537 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5538 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5539 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5540 | |
| 5541 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5542 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5543 | |
| 5544 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5545 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5546 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5547 | // Make sure this one overlaps the bottom window |
| 5548 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5549 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5550 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5551 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5552 | |
| 5553 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5554 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5555 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5556 | |
| 5557 | mDispatcher->setInputWindows( |
| 5558 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5559 | |
| 5560 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5561 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5562 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5563 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5564 | slipperyExitWindow->consumeMotionDown(); |
| 5565 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5566 | mDispatcher->setInputWindows( |
| 5567 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5568 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5569 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 5570 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5571 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5572 | |
| 5573 | slipperyExitWindow->consumeMotionCancel(); |
| 5574 | |
| 5575 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5576 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5577 | } |
| 5578 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 5579 | /** |
| 5580 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 5581 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 5582 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 5583 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 5584 | * reproduces a crash. |
| 5585 | */ |
| 5586 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 5587 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5588 | |
| 5589 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 5590 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5591 | leftSlipperyWindow->setSlippery(true); |
| 5592 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5593 | |
| 5594 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 5595 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5596 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 5597 | rightDropTouchesWindow->setDropInput(true); |
| 5598 | |
| 5599 | mDispatcher->setInputWindows( |
| 5600 | {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}}); |
| 5601 | |
| 5602 | // Start touch in the left window |
| 5603 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5604 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5605 | .build()); |
| 5606 | leftSlipperyWindow->consumeMotionDown(); |
| 5607 | |
| 5608 | // And move it into the right window |
| 5609 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5610 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5611 | .build()); |
| 5612 | |
| 5613 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 5614 | // The left window continues to receive the gesture. |
| 5615 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5616 | rightDropTouchesWindow->assertNoEvents(); |
| 5617 | } |
| 5618 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5619 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5620 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5621 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5622 | |
| 5623 | sp<FakeWindowHandle> leftWindow = |
| 5624 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5625 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5626 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5627 | |
| 5628 | sp<FakeWindowHandle> rightSpy = |
| 5629 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 5630 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5631 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5632 | rightSpy->setSpy(true); |
| 5633 | rightSpy->setTrustedOverlay(true); |
| 5634 | |
| 5635 | sp<FakeWindowHandle> rightWindow = |
| 5636 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5637 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5638 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5639 | |
| 5640 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}}); |
| 5641 | |
| 5642 | // Touch in the left window |
| 5643 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5644 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5645 | .build()); |
| 5646 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 5647 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5648 | ASSERT_NO_FATAL_FAILURE( |
| 5649 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5650 | |
| 5651 | // Touch another finger over the right windows |
| 5652 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5653 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5654 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5655 | .build()); |
| 5656 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 5657 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 5658 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 5659 | mDispatcher->waitForIdle(); |
| 5660 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5661 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 5662 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5663 | |
| 5664 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 5665 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 5666 | // is part of the UP action, we do not treat this as device interaction. |
| 5667 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5668 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5669 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5670 | .build()); |
| 5671 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 5672 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 5673 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 5674 | mDispatcher->waitForIdle(); |
| 5675 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5676 | |
| 5677 | // Move remaining finger |
| 5678 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5679 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5680 | .build()); |
| 5681 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 5682 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 5683 | mDispatcher->waitForIdle(); |
| 5684 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5685 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5686 | |
| 5687 | // Release all fingers |
| 5688 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5689 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5690 | .build()); |
| 5691 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 5692 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 5693 | mDispatcher->waitForIdle(); |
| 5694 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5695 | } |
| 5696 | |
| 5697 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 5698 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5699 | |
| 5700 | sp<FakeWindowHandle> window = |
| 5701 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5702 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 5703 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5704 | |
| 5705 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5706 | setFocusedWindow(window); |
| 5707 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 5708 | |
| 5709 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 5710 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 5711 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5712 | ASSERT_NO_FATAL_FAILURE( |
| 5713 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5714 | |
| 5715 | // The UP actions are not treated as device interaction. |
| 5716 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 5717 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 5718 | mDispatcher->waitForIdle(); |
| 5719 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5720 | } |
| 5721 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5722 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5723 | protected: |
| 5724 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5725 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5726 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5727 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5728 | sp<FakeWindowHandle> mWindow; |
| 5729 | |
| 5730 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5731 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5732 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 5733 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 5734 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 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. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6125 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 6126 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6127 | |
| 6128 | // Enable InputFilter |
| 6129 | mDispatcher->setInputFilterEnabled(true); |
| 6130 | // Test touch on both primary and second display, and check if both events are filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6131 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 6132 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 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. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6137 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 6138 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 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. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6144 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6145 | |
| 6146 | // Enable InputFilter |
| 6147 | mDispatcher->setInputFilterEnabled(true); |
| 6148 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6149 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6150 | |
| 6151 | // Disable InputFilter |
| 6152 | mDispatcher->setInputFilterEnabled(false); |
| 6153 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6154 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 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. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6177 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 6178 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 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 | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 6217 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 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, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 6247 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 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 | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 6252 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 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 | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 6640 | mApplication->setDispatchingTimeout(100ms); |
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 | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 6644 | mWindow->setDispatchingTimeout(100ms); |
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, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 6710 | InputEventInjectionSync::NONE, /*injectionTimeout=*/50ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 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, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 6774 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*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, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 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, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 6825 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*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 | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 6849 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 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 | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7065 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 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 | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7170 | mApplication->setDispatchingTimeout(100ms); |
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 | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7179 | mFocusedWindow->setDispatchingTimeout(100ms); |
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 |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7265 | mUnfocusedWindow->setDispatchingTimeout( |
| 7266 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7267 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7268 | |
| 7269 | tapOnFocusedWindow(); |
| 7270 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7271 | // We don't know which window will ANR first. But both of them should happen eventually. |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7272 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 7273 | mFocusedWindow->getDispatchingTimeout( |
| 7274 | DISPATCHING_TIMEOUT)), |
| 7275 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 7276 | |
| 7277 | ASSERT_THAT(anrConnectionTokens, |
| 7278 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 7279 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7280 | |
| 7281 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7282 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7283 | |
| 7284 | mFocusedWindow->consumeMotionDown(); |
| 7285 | mFocusedWindow->consumeMotionUp(); |
| 7286 | mUnfocusedWindow->consumeMotionOutside(); |
| 7287 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7288 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 7289 | 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 |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7292 | ASSERT_THAT(responsiveTokens, |
| 7293 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 7294 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7295 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7296 | } |
| 7297 | |
| 7298 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 7299 | // We should also log an error to account for the dropped event (not tested here). |
| 7300 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 7301 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 7302 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7303 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7304 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7305 | // Receive the events, but don't respond |
| 7306 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 7307 | ASSERT_TRUE(downEventSequenceNum); |
| 7308 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 7309 | ASSERT_TRUE(upEventSequenceNum); |
| 7310 | const std::chrono::duration timeout = |
| 7311 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7312 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7313 | |
| 7314 | // Tap once again |
| 7315 | // 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] | 7316 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7317 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7318 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7319 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7320 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7321 | FOCUSED_WINDOW_LOCATION)); |
| 7322 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 7323 | // valid touch target |
| 7324 | mUnfocusedWindow->assertNoEvents(); |
| 7325 | |
| 7326 | // Consume the first tap |
| 7327 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 7328 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 7329 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7330 | // The second tap did not go to the focused window |
| 7331 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7332 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7333 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7334 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7335 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7336 | } |
| 7337 | |
| 7338 | // If you tap outside of all windows, there will not be ANR |
| 7339 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7340 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7341 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7342 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 7343 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7344 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7345 | } |
| 7346 | |
| 7347 | // Since the focused window is paused, tapping on it should not produce any events |
| 7348 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 7349 | mFocusedWindow->setPaused(true); |
| 7350 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7351 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7352 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7353 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7354 | FOCUSED_WINDOW_LOCATION)); |
| 7355 | |
| 7356 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 7357 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7358 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 7359 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7360 | |
| 7361 | mFocusedWindow->assertNoEvents(); |
| 7362 | mUnfocusedWindow->assertNoEvents(); |
| 7363 | } |
| 7364 | |
| 7365 | /** |
| 7366 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7367 | * not to to the focused window until the motion is processed. |
| 7368 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 7369 | * |
| 7370 | * Warning!!! |
| 7371 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7372 | * and the injection timeout that we specify when injecting the key. |
| 7373 | * We must have the injection timeout (10ms) be smaller than |
| 7374 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7375 | * |
| 7376 | * If that value changes, this test should also change. |
| 7377 | */ |
| 7378 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 7379 | // Set a long ANR timeout to prevent it from triggering |
| 7380 | mFocusedWindow->setDispatchingTimeout(2s); |
| 7381 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7382 | |
| 7383 | tapOnUnfocusedWindow(); |
| 7384 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7385 | ASSERT_TRUE(downSequenceNum); |
| 7386 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7387 | ASSERT_TRUE(upSequenceNum); |
| 7388 | // Don't finish the events yet, and send a key |
| 7389 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 7390 | // window even if motions are still being processed. |
| 7391 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7392 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7393 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7394 | InputEventInjectionSync::NONE, /*injectionTimeout=*/50ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7395 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7396 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7397 | // and the key remains pending, waiting for the touch events to be processed |
| 7398 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 7399 | ASSERT_FALSE(keySequenceNum); |
| 7400 | |
| 7401 | // 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] | 7402 | mFocusedWindow->setFocusable(false); |
| 7403 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7404 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7405 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7406 | |
| 7407 | // Focus events should precede the key events |
| 7408 | mUnfocusedWindow->consumeFocusEvent(true); |
| 7409 | mFocusedWindow->consumeFocusEvent(false); |
| 7410 | |
| 7411 | // Finish the tap events, which should unblock dispatcher |
| 7412 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 7413 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 7414 | |
| 7415 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 7416 | // can finally go to the newly focused "mUnfocusedWindow". |
| 7417 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7418 | mFocusedWindow->assertNoEvents(); |
| 7419 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7420 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7421 | } |
| 7422 | |
| 7423 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 7424 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 7425 | // The other window should not be affected by that. |
| 7426 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 7427 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7428 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7429 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7430 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7431 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7432 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7433 | |
| 7434 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7435 | mDispatcher->notifyMotion( |
| 7436 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7437 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7438 | |
| 7439 | const std::chrono::duration timeout = |
| 7440 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7441 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7442 | |
| 7443 | mUnfocusedWindow->consumeMotionDown(); |
| 7444 | mFocusedWindow->consumeMotionDown(); |
| 7445 | // Focused window may or may not receive ACTION_MOVE |
| 7446 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 7447 | InputEvent* event; |
| 7448 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 7449 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 7450 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 7451 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7452 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7453 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 7454 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 7455 | mFocusedWindow->consumeMotionCancel(); |
| 7456 | } else { |
| 7457 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 7458 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7459 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7460 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7461 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7462 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7463 | mUnfocusedWindow->assertNoEvents(); |
| 7464 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7465 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7466 | } |
| 7467 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7468 | /** |
| 7469 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 7470 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 7471 | * |
| 7472 | * If the user touches another application during this time, the key should be dropped. |
| 7473 | * Next, if a new focused window comes in, without toggling the focused application, |
| 7474 | * then no ANR should occur. |
| 7475 | * |
| 7476 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 7477 | * but in some cases the policy may not update the focused application. |
| 7478 | */ |
| 7479 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 7480 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 7481 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7482 | focusedApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7483 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 7484 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 7485 | mFocusedWindow->setFocusable(false); |
| 7486 | |
| 7487 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7488 | mFocusedWindow->consumeFocusEvent(false); |
| 7489 | |
| 7490 | // Send a key. The ANR timer should start because there is no focused window. |
| 7491 | // 'focusedApplication' will get blamed if this timer completes. |
| 7492 | // 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] | 7493 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7494 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7495 | InputEventInjectionSync::NONE, /*injectionTimeout=*/50ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7496 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7497 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7498 | |
| 7499 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 7500 | // then the injected touches won't cause the focused event to get dropped. |
| 7501 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 7502 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 7503 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 7504 | // For this test, it means that the key would get delivered to the window once it becomes |
| 7505 | // focused. |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 7506 | std::this_thread::sleep_for(50ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7507 | |
| 7508 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7509 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7510 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7511 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7512 | |
| 7513 | // We do not consume the motion right away, because that would require dispatcher to first |
| 7514 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 7515 | // Set the focused window first. |
| 7516 | mFocusedWindow->setFocusable(true); |
| 7517 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7518 | setFocusedWindow(mFocusedWindow); |
| 7519 | mFocusedWindow->consumeFocusEvent(true); |
| 7520 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 7521 | // to another application. This could be a bug / behaviour in the policy. |
| 7522 | |
| 7523 | mUnfocusedWindow->consumeMotionDown(); |
| 7524 | |
| 7525 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7526 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 7527 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 7528 | } |
| 7529 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7530 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 7531 | // that has feature NO_INPUT_CHANNEL. |
| 7532 | // Layout: |
| 7533 | // Top (closest to user) |
| 7534 | // mNoInputWindow (above all windows) |
| 7535 | // mBottomWindow |
| 7536 | // Bottom (furthest from user) |
| 7537 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 7538 | virtual void SetUp() override { |
| 7539 | InputDispatcherTest::SetUp(); |
| 7540 | |
| 7541 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7542 | mNoInputWindow = |
| 7543 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7544 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7545 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7546 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7547 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7548 | // It's perfectly valid for this window to not have an associated input channel |
| 7549 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7550 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 7551 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7552 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7553 | |
| 7554 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7555 | } |
| 7556 | |
| 7557 | protected: |
| 7558 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 7559 | sp<FakeWindowHandle> mNoInputWindow; |
| 7560 | sp<FakeWindowHandle> mBottomWindow; |
| 7561 | }; |
| 7562 | |
| 7563 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 7564 | PointF touchedPoint = {10, 10}; |
| 7565 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7566 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7567 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7568 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7569 | |
| 7570 | mNoInputWindow->assertNoEvents(); |
| 7571 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 7572 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 7573 | // and therefore should prevent mBottomWindow from receiving touches |
| 7574 | mBottomWindow->assertNoEvents(); |
| 7575 | } |
| 7576 | |
| 7577 | /** |
| 7578 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 7579 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 7580 | */ |
| 7581 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 7582 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7583 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7584 | "Window with input channel and NO_INPUT_CHANNEL", |
| 7585 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7586 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7587 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7588 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7589 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7590 | |
| 7591 | PointF touchedPoint = {10, 10}; |
| 7592 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7593 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7594 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7595 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7596 | |
| 7597 | mNoInputWindow->assertNoEvents(); |
| 7598 | mBottomWindow->assertNoEvents(); |
| 7599 | } |
| 7600 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7601 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7602 | protected: |
| 7603 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7604 | sp<FakeWindowHandle> mWindow; |
| 7605 | sp<FakeWindowHandle> mMirror; |
| 7606 | |
| 7607 | virtual void SetUp() override { |
| 7608 | InputDispatcherTest::SetUp(); |
| 7609 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7610 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7611 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7612 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7613 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7614 | mWindow->setFocusable(true); |
| 7615 | mMirror->setFocusable(true); |
| 7616 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7617 | } |
| 7618 | }; |
| 7619 | |
| 7620 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7621 | // Request focus on a mirrored window |
| 7622 | setFocusedWindow(mMirror); |
| 7623 | |
| 7624 | // window gets focused |
| 7625 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7627 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7628 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7629 | } |
| 7630 | |
| 7631 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7632 | // focusable. |
| 7633 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7634 | setFocusedWindow(mMirror); |
| 7635 | |
| 7636 | // window gets focused |
| 7637 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7639 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7640 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7642 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7643 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7644 | |
| 7645 | mMirror->setFocusable(false); |
| 7646 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7647 | |
| 7648 | // window loses focus since one of the windows associated with the token in not focusable |
| 7649 | mWindow->consumeFocusEvent(false); |
| 7650 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7651 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7652 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7653 | mWindow->assertNoEvents(); |
| 7654 | } |
| 7655 | |
| 7656 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7657 | // invisible. |
| 7658 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 7659 | setFocusedWindow(mMirror); |
| 7660 | |
| 7661 | // window gets focused |
| 7662 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7664 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7665 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7667 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7668 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7669 | |
| 7670 | mMirror->setVisible(false); |
| 7671 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7672 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7674 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7675 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7677 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7678 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7679 | |
| 7680 | mWindow->setVisible(false); |
| 7681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7682 | |
| 7683 | // window loses focus only after all windows associated with the token become invisible. |
| 7684 | mWindow->consumeFocusEvent(false); |
| 7685 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7686 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7687 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7688 | mWindow->assertNoEvents(); |
| 7689 | } |
| 7690 | |
| 7691 | // A focused & mirrored window remains focused until both windows are removed. |
| 7692 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7693 | setFocusedWindow(mMirror); |
| 7694 | |
| 7695 | // window gets focused |
| 7696 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7697 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7698 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7699 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7700 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7701 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7702 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7703 | |
| 7704 | // single window is removed but the window token remains focused |
| 7705 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7706 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7708 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7709 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7710 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7711 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7712 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7713 | |
| 7714 | // Both windows are removed |
| 7715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7716 | mWindow->consumeFocusEvent(false); |
| 7717 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7718 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7719 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7720 | mWindow->assertNoEvents(); |
| 7721 | } |
| 7722 | |
| 7723 | // Focus request can be pending until one window becomes visible. |
| 7724 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7725 | // Request focus on an invisible mirror. |
| 7726 | mWindow->setVisible(false); |
| 7727 | mMirror->setVisible(false); |
| 7728 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7729 | setFocusedWindow(mMirror); |
| 7730 | |
| 7731 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7732 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7733 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7734 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7735 | |
| 7736 | mMirror->setVisible(true); |
| 7737 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7738 | |
| 7739 | // window gets focused |
| 7740 | mWindow->consumeFocusEvent(true); |
| 7741 | // window gets the pending key event |
| 7742 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7743 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7744 | |
| 7745 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7746 | protected: |
| 7747 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7748 | sp<FakeWindowHandle> mWindow; |
| 7749 | sp<FakeWindowHandle> mSecondWindow; |
| 7750 | |
| 7751 | void SetUp() override { |
| 7752 | InputDispatcherTest::SetUp(); |
| 7753 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7754 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7755 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7756 | mSecondWindow = |
| 7757 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7758 | mSecondWindow->setFocusable(true); |
| 7759 | |
| 7760 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7761 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7762 | |
| 7763 | setFocusedWindow(mWindow); |
| 7764 | mWindow->consumeFocusEvent(true); |
| 7765 | } |
| 7766 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7767 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7768 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7769 | } |
| 7770 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7771 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7772 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7773 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7774 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7775 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7776 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7777 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7778 | } |
| 7779 | }; |
| 7780 | |
| 7781 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7782 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7783 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7784 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7785 | mSecondWindow->assertNoEvents(); |
| 7786 | |
| 7787 | // Ensure that capture can be enabled from the focus window. |
| 7788 | requestAndVerifyPointerCapture(mWindow, true); |
| 7789 | |
| 7790 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7791 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7792 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7793 | |
| 7794 | // Ensure that capture can be disabled from the window with capture. |
| 7795 | requestAndVerifyPointerCapture(mWindow, false); |
| 7796 | } |
| 7797 | |
| 7798 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7799 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7800 | |
| 7801 | setFocusedWindow(mSecondWindow); |
| 7802 | |
| 7803 | // Ensure that the capture disabled event was sent first. |
| 7804 | mWindow->consumeCaptureEvent(false); |
| 7805 | mWindow->consumeFocusEvent(false); |
| 7806 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7807 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7808 | |
| 7809 | // 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] | 7810 | notifyPointerCaptureChanged({}); |
| 7811 | notifyPointerCaptureChanged(request); |
| 7812 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7813 | mWindow->assertNoEvents(); |
| 7814 | mSecondWindow->assertNoEvents(); |
| 7815 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7816 | } |
| 7817 | |
| 7818 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7819 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7820 | |
| 7821 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7822 | notifyPointerCaptureChanged({}); |
| 7823 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7824 | |
| 7825 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7826 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7827 | mWindow->consumeCaptureEvent(false); |
| 7828 | mWindow->assertNoEvents(); |
| 7829 | } |
| 7830 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7831 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7832 | requestAndVerifyPointerCapture(mWindow, true); |
| 7833 | |
| 7834 | // The first window loses focus. |
| 7835 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7836 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7837 | mWindow->consumeCaptureEvent(false); |
| 7838 | |
| 7839 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7840 | // arrives. |
| 7841 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7842 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7843 | |
| 7844 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7845 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7846 | |
| 7847 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7848 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7849 | |
| 7850 | mSecondWindow->consumeFocusEvent(true); |
| 7851 | mSecondWindow->consumeCaptureEvent(true); |
| 7852 | } |
| 7853 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7854 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7855 | // App repeatedly enables and disables capture. |
| 7856 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7857 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7858 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7859 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7860 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7861 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7862 | |
| 7863 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7864 | // first request is now stale, this should do nothing. |
| 7865 | notifyPointerCaptureChanged(firstRequest); |
| 7866 | mWindow->assertNoEvents(); |
| 7867 | |
| 7868 | // InputReader notifies that the second request was enabled. |
| 7869 | notifyPointerCaptureChanged(secondRequest); |
| 7870 | mWindow->consumeCaptureEvent(true); |
| 7871 | } |
| 7872 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7873 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7874 | requestAndVerifyPointerCapture(mWindow, true); |
| 7875 | |
| 7876 | // App toggles pointer capture off and on. |
| 7877 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7878 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7879 | |
| 7880 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7881 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7882 | |
| 7883 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7884 | // preceding "disable" request. |
| 7885 | notifyPointerCaptureChanged(enableRequest); |
| 7886 | |
| 7887 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7888 | // any notifications. |
| 7889 | mWindow->assertNoEvents(); |
| 7890 | } |
| 7891 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7892 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7893 | protected: |
| 7894 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7895 | |
| 7896 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7897 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7898 | |
| 7899 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7900 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7901 | |
| 7902 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7903 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7904 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7905 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7906 | MAXIMUM_OBSCURING_OPACITY); |
| 7907 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7908 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 7909 | static constexpr gui::Uid APP_B_UID{10002}; |
| 7910 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7911 | |
| 7912 | sp<FakeWindowHandle> mTouchWindow; |
| 7913 | |
| 7914 | virtual void SetUp() override { |
| 7915 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7916 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7917 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7918 | } |
| 7919 | |
| 7920 | virtual void TearDown() override { |
| 7921 | InputDispatcherTest::TearDown(); |
| 7922 | mTouchWindow.clear(); |
| 7923 | } |
| 7924 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7925 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7926 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7927 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7928 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7929 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7930 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7931 | return window; |
| 7932 | } |
| 7933 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7934 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7935 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7936 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7937 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7938 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7939 | 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] | 7940 | return window; |
| 7941 | } |
| 7942 | |
| 7943 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7944 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7945 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7946 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7947 | } |
| 7948 | }; |
| 7949 | |
| 7950 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7951 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7952 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7953 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7954 | |
| 7955 | touch(); |
| 7956 | |
| 7957 | mTouchWindow->assertNoEvents(); |
| 7958 | } |
| 7959 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7960 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7961 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7962 | const sp<FakeWindowHandle>& w = |
| 7963 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7964 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7965 | |
| 7966 | touch(); |
| 7967 | |
| 7968 | mTouchWindow->assertNoEvents(); |
| 7969 | } |
| 7970 | |
| 7971 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7972 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7973 | const sp<FakeWindowHandle>& w = |
| 7974 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7975 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7976 | |
| 7977 | touch(); |
| 7978 | |
| 7979 | w->assertNoEvents(); |
| 7980 | } |
| 7981 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7982 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7983 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7984 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7985 | |
| 7986 | touch(); |
| 7987 | |
| 7988 | mTouchWindow->consumeAnyMotionDown(); |
| 7989 | } |
| 7990 | |
| 7991 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7992 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7993 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7994 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7995 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7996 | |
| 7997 | touch({PointF{100, 100}}); |
| 7998 | |
| 7999 | mTouchWindow->consumeAnyMotionDown(); |
| 8000 | } |
| 8001 | |
| 8002 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8003 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8004 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8005 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8006 | |
| 8007 | touch(); |
| 8008 | |
| 8009 | mTouchWindow->consumeAnyMotionDown(); |
| 8010 | } |
| 8011 | |
| 8012 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 8013 | const sp<FakeWindowHandle>& w = |
| 8014 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 8015 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8016 | |
| 8017 | touch(); |
| 8018 | |
| 8019 | mTouchWindow->consumeAnyMotionDown(); |
| 8020 | } |
| 8021 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8022 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 8023 | const sp<FakeWindowHandle>& w = |
| 8024 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 8025 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8026 | |
| 8027 | touch(); |
| 8028 | |
| 8029 | w->assertNoEvents(); |
| 8030 | } |
| 8031 | |
| 8032 | /** |
| 8033 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 8034 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 8035 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 8036 | */ |
| 8037 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8038 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 8039 | const sp<FakeWindowHandle>& w = |
| 8040 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8041 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8042 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8043 | |
| 8044 | touch(); |
| 8045 | |
| 8046 | w->consumeMotionOutside(); |
| 8047 | } |
| 8048 | |
| 8049 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 8050 | const sp<FakeWindowHandle>& w = |
| 8051 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8052 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8053 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8054 | |
| 8055 | touch(); |
| 8056 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8057 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8058 | } |
| 8059 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8060 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8061 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8062 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8063 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8064 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8065 | |
| 8066 | touch(); |
| 8067 | |
| 8068 | mTouchWindow->consumeAnyMotionDown(); |
| 8069 | } |
| 8070 | |
| 8071 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 8072 | const sp<FakeWindowHandle>& w = |
| 8073 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8074 | MAXIMUM_OBSCURING_OPACITY); |
| 8075 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8076 | |
| 8077 | touch(); |
| 8078 | |
| 8079 | mTouchWindow->consumeAnyMotionDown(); |
| 8080 | } |
| 8081 | |
| 8082 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8083 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8084 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8085 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8086 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8087 | |
| 8088 | touch(); |
| 8089 | |
| 8090 | mTouchWindow->assertNoEvents(); |
| 8091 | } |
| 8092 | |
| 8093 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 8094 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 8095 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8096 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8097 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8098 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8099 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8100 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8101 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8102 | |
| 8103 | touch(); |
| 8104 | |
| 8105 | mTouchWindow->assertNoEvents(); |
| 8106 | } |
| 8107 | |
| 8108 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 8109 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 8110 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8111 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8112 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8113 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8114 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8115 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8116 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8117 | |
| 8118 | touch(); |
| 8119 | |
| 8120 | mTouchWindow->consumeAnyMotionDown(); |
| 8121 | } |
| 8122 | |
| 8123 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8124 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 8125 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8126 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8127 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8128 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8129 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8130 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8131 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 8132 | |
| 8133 | touch(); |
| 8134 | |
| 8135 | mTouchWindow->consumeAnyMotionDown(); |
| 8136 | } |
| 8137 | |
| 8138 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 8139 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8140 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8141 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8142 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8143 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8144 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8145 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8146 | |
| 8147 | touch(); |
| 8148 | |
| 8149 | mTouchWindow->assertNoEvents(); |
| 8150 | } |
| 8151 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8152 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8153 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 8154 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8155 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8156 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8157 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8158 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8159 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8160 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 8161 | |
| 8162 | touch(); |
| 8163 | |
| 8164 | mTouchWindow->assertNoEvents(); |
| 8165 | } |
| 8166 | |
| 8167 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8168 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 8169 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8170 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8171 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8172 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8173 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8174 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8175 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 8176 | |
| 8177 | touch(); |
| 8178 | |
| 8179 | mTouchWindow->consumeAnyMotionDown(); |
| 8180 | } |
| 8181 | |
| 8182 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 8183 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8184 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8185 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8186 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8187 | |
| 8188 | touch(); |
| 8189 | |
| 8190 | mTouchWindow->consumeAnyMotionDown(); |
| 8191 | } |
| 8192 | |
| 8193 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 8194 | const sp<FakeWindowHandle>& w = |
| 8195 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 8196 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8197 | |
| 8198 | touch(); |
| 8199 | |
| 8200 | mTouchWindow->consumeAnyMotionDown(); |
| 8201 | } |
| 8202 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8203 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8204 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 8205 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 8206 | const sp<FakeWindowHandle>& w = |
| 8207 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 8208 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8209 | |
| 8210 | touch(); |
| 8211 | |
| 8212 | mTouchWindow->assertNoEvents(); |
| 8213 | } |
| 8214 | |
| 8215 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 8216 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 8217 | const sp<FakeWindowHandle>& w = |
| 8218 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 8219 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8220 | |
| 8221 | touch(); |
| 8222 | |
| 8223 | mTouchWindow->consumeAnyMotionDown(); |
| 8224 | } |
| 8225 | |
| 8226 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8227 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 8228 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 8229 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8230 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8231 | OPACITY_ABOVE_THRESHOLD); |
| 8232 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8233 | |
| 8234 | touch(); |
| 8235 | |
| 8236 | mTouchWindow->consumeAnyMotionDown(); |
| 8237 | } |
| 8238 | |
| 8239 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8240 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 8241 | const sp<FakeWindowHandle>& w1 = |
| 8242 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 8243 | OPACITY_BELOW_THRESHOLD); |
| 8244 | const sp<FakeWindowHandle>& w2 = |
| 8245 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8246 | OPACITY_BELOW_THRESHOLD); |
| 8247 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8248 | |
| 8249 | touch(); |
| 8250 | |
| 8251 | mTouchWindow->assertNoEvents(); |
| 8252 | } |
| 8253 | |
| 8254 | /** |
| 8255 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 8256 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 8257 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 8258 | */ |
| 8259 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8260 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 8261 | const sp<FakeWindowHandle>& wB = |
| 8262 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 8263 | OPACITY_BELOW_THRESHOLD); |
| 8264 | const sp<FakeWindowHandle>& wC = |
| 8265 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8266 | OPACITY_BELOW_THRESHOLD); |
| 8267 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 8268 | |
| 8269 | touch(); |
| 8270 | |
| 8271 | mTouchWindow->assertNoEvents(); |
| 8272 | } |
| 8273 | |
| 8274 | /** |
| 8275 | * This test is testing that a window from a different UID but with same application token doesn't |
| 8276 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 8277 | */ |
| 8278 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8279 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 8280 | const sp<FakeWindowHandle>& w = |
| 8281 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 8282 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8283 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8284 | |
| 8285 | touch(); |
| 8286 | |
| 8287 | mTouchWindow->consumeAnyMotionDown(); |
| 8288 | } |
| 8289 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8290 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 8291 | protected: |
| 8292 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8293 | sp<FakeWindowHandle> mWindow; |
| 8294 | sp<FakeWindowHandle> mSecondWindow; |
| 8295 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8296 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8297 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 8298 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8299 | |
| 8300 | void SetUp() override { |
| 8301 | InputDispatcherTest::SetUp(); |
| 8302 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8303 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8304 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8305 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8306 | mSecondWindow = |
| 8307 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8308 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8309 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8310 | mSpyWindow = |
| 8311 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8312 | mSpyWindow->setSpy(true); |
| 8313 | mSpyWindow->setTrustedOverlay(true); |
| 8314 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 8315 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8316 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8317 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8318 | } |
| 8319 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8320 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 8321 | switch (fromSource) { |
| 8322 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 8323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8324 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8325 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8326 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8327 | break; |
| 8328 | case AINPUT_SOURCE_STYLUS: |
| 8329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8330 | injectMotionEvent( |
| 8331 | mDispatcher, |
| 8332 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8333 | AINPUT_SOURCE_STYLUS) |
| 8334 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8335 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8336 | .x(50) |
| 8337 | .y(50)) |
| 8338 | .build())); |
| 8339 | break; |
| 8340 | case AINPUT_SOURCE_MOUSE: |
| 8341 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8342 | injectMotionEvent( |
| 8343 | mDispatcher, |
| 8344 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8345 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8346 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8347 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8348 | .x(50) |
| 8349 | .y(50)) |
| 8350 | .build())); |
| 8351 | break; |
| 8352 | default: |
| 8353 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 8354 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8355 | |
| 8356 | // Window should receive motion event. |
| 8357 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8358 | // Spy window should also receive motion event |
| 8359 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8360 | } |
| 8361 | |
| 8362 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 8363 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 8364 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8365 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8366 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8367 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8368 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8369 | |
| 8370 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8371 | mDragWindow = |
| 8372 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8373 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8374 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8375 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8376 | |
| 8377 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8378 | bool transferred = |
| 8379 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8380 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8381 | if (transferred) { |
| 8382 | mWindow->consumeMotionCancel(); |
| 8383 | mDragWindow->consumeMotionDown(); |
| 8384 | } |
| 8385 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8386 | } |
| 8387 | }; |
| 8388 | |
| 8389 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8390 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8391 | |
| 8392 | // Move on window. |
| 8393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8394 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8395 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8396 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8397 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8398 | mWindow->consumeDragEvent(false, 50, 50); |
| 8399 | mSecondWindow->assertNoEvents(); |
| 8400 | |
| 8401 | // Move to another window. |
| 8402 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8403 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8404 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8405 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8406 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8407 | mWindow->consumeDragEvent(true, 150, 50); |
| 8408 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8409 | |
| 8410 | // Move back to original window. |
| 8411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8412 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8413 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8414 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8415 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8416 | mWindow->consumeDragEvent(false, 50, 50); |
| 8417 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 8418 | |
| 8419 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8420 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8421 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8422 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8423 | mWindow->assertNoEvents(); |
| 8424 | mSecondWindow->assertNoEvents(); |
| 8425 | } |
| 8426 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8427 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8428 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8429 | |
| 8430 | // No cancel event after drag start |
| 8431 | mSpyWindow->assertNoEvents(); |
| 8432 | |
| 8433 | const MotionEvent secondFingerDownEvent = |
| 8434 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8435 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8436 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8437 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8438 | .build(); |
| 8439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8440 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8441 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8442 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8443 | |
| 8444 | // Receives cancel for first pointer after next pointer down |
| 8445 | mSpyWindow->consumeMotionCancel(); |
| 8446 | mSpyWindow->consumeMotionDown(); |
| 8447 | |
| 8448 | mSpyWindow->assertNoEvents(); |
| 8449 | } |
| 8450 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8451 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8452 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8453 | |
| 8454 | // Move on window. |
| 8455 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8456 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8457 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8458 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8459 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8460 | mWindow->consumeDragEvent(false, 50, 50); |
| 8461 | mSecondWindow->assertNoEvents(); |
| 8462 | |
| 8463 | // Move to another window. |
| 8464 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8465 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8466 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8467 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8468 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8469 | mWindow->consumeDragEvent(true, 150, 50); |
| 8470 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8471 | |
| 8472 | // drop to another window. |
| 8473 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8474 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8475 | {150, 50})) |
| 8476 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8477 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8478 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8479 | mWindow->assertNoEvents(); |
| 8480 | mSecondWindow->assertNoEvents(); |
| 8481 | } |
| 8482 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8483 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8484 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8485 | |
| 8486 | // Move on window and keep button pressed. |
| 8487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8488 | injectMotionEvent(mDispatcher, |
| 8489 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8490 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8491 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8492 | .build())) |
| 8493 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8494 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8495 | mWindow->consumeDragEvent(false, 50, 50); |
| 8496 | mSecondWindow->assertNoEvents(); |
| 8497 | |
| 8498 | // Move to another window and release button, expect to drop item. |
| 8499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8500 | injectMotionEvent(mDispatcher, |
| 8501 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8502 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8503 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8504 | .build())) |
| 8505 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8506 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8507 | mWindow->assertNoEvents(); |
| 8508 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8509 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8510 | |
| 8511 | // nothing to the window. |
| 8512 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8513 | injectMotionEvent(mDispatcher, |
| 8514 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 8515 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8516 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8517 | .build())) |
| 8518 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8519 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8520 | mWindow->assertNoEvents(); |
| 8521 | mSecondWindow->assertNoEvents(); |
| 8522 | } |
| 8523 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8524 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8525 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8526 | |
| 8527 | // Set second window invisible. |
| 8528 | mSecondWindow->setVisible(false); |
| 8529 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 8530 | |
| 8531 | // Move on window. |
| 8532 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8533 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8534 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8535 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8536 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8537 | mWindow->consumeDragEvent(false, 50, 50); |
| 8538 | mSecondWindow->assertNoEvents(); |
| 8539 | |
| 8540 | // Move to another window. |
| 8541 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8542 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8543 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8544 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8545 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8546 | mWindow->consumeDragEvent(true, 150, 50); |
| 8547 | mSecondWindow->assertNoEvents(); |
| 8548 | |
| 8549 | // drop to another window. |
| 8550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8551 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8552 | {150, 50})) |
| 8553 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8554 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8555 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8556 | mWindow->assertNoEvents(); |
| 8557 | mSecondWindow->assertNoEvents(); |
| 8558 | } |
| 8559 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8560 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8561 | // Ensure window could track pointerIds if it didn't support split touch. |
| 8562 | mWindow->setPreventSplitting(true); |
| 8563 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8565 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8566 | {50, 50})) |
| 8567 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8568 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8569 | |
| 8570 | const MotionEvent secondFingerDownEvent = |
| 8571 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8572 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8573 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8574 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8575 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8576 | .build(); |
| 8577 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8578 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8579 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8580 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8581 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8582 | |
| 8583 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8584 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8585 | } |
| 8586 | |
| 8587 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 8588 | // First down on second window. |
| 8589 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8590 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8591 | {150, 50})) |
| 8592 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8593 | |
| 8594 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8595 | |
| 8596 | // Second down on first window. |
| 8597 | const MotionEvent secondFingerDownEvent = |
| 8598 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8599 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8600 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8601 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8602 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8603 | .build(); |
| 8604 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8605 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8606 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8607 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8608 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8609 | |
| 8610 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8611 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8612 | |
| 8613 | // Move on window. |
| 8614 | const MotionEvent secondFingerMoveEvent = |
| 8615 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8616 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8617 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8618 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8619 | .build(); |
| 8620 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8621 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8622 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8623 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8624 | mWindow->consumeDragEvent(false, 50, 50); |
| 8625 | mSecondWindow->consumeMotionMove(); |
| 8626 | |
| 8627 | // Release the drag pointer should perform drop. |
| 8628 | const MotionEvent secondFingerUpEvent = |
| 8629 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8630 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8631 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8632 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8633 | .build(); |
| 8634 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8635 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8636 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8637 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8638 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8639 | mWindow->assertNoEvents(); |
| 8640 | mSecondWindow->consumeMotionMove(); |
| 8641 | } |
| 8642 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8643 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8644 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8645 | |
| 8646 | // Update window of second display. |
| 8647 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8648 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8649 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8650 | |
| 8651 | // Let second display has a touch state. |
| 8652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8653 | injectMotionEvent(mDispatcher, |
| 8654 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8655 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8656 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8657 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8658 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8659 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8660 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8661 | // Update window again. |
| 8662 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8663 | |
| 8664 | // Move on window. |
| 8665 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8666 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8667 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8668 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8669 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8670 | mWindow->consumeDragEvent(false, 50, 50); |
| 8671 | mSecondWindow->assertNoEvents(); |
| 8672 | |
| 8673 | // Move to another window. |
| 8674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8675 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8676 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8677 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8678 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8679 | mWindow->consumeDragEvent(true, 150, 50); |
| 8680 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8681 | |
| 8682 | // drop to another window. |
| 8683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8684 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8685 | {150, 50})) |
| 8686 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8687 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8688 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8689 | mWindow->assertNoEvents(); |
| 8690 | mSecondWindow->assertNoEvents(); |
| 8691 | } |
| 8692 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8693 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8694 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8695 | // Move on window. |
| 8696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8697 | injectMotionEvent(mDispatcher, |
| 8698 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8699 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8700 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8701 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8702 | .x(50) |
| 8703 | .y(50)) |
| 8704 | .build())) |
| 8705 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8706 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8707 | mWindow->consumeDragEvent(false, 50, 50); |
| 8708 | mSecondWindow->assertNoEvents(); |
| 8709 | |
| 8710 | // Move to another window. |
| 8711 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8712 | injectMotionEvent(mDispatcher, |
| 8713 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8714 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8715 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8716 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8717 | .x(150) |
| 8718 | .y(50)) |
| 8719 | .build())) |
| 8720 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8721 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8722 | mWindow->consumeDragEvent(true, 150, 50); |
| 8723 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8724 | |
| 8725 | // drop to another window. |
| 8726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8727 | injectMotionEvent(mDispatcher, |
| 8728 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8729 | .buttonState(0) |
| 8730 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8731 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8732 | .x(150) |
| 8733 | .y(50)) |
| 8734 | .build())) |
| 8735 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8736 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 8737 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8738 | mWindow->assertNoEvents(); |
| 8739 | mSecondWindow->assertNoEvents(); |
| 8740 | } |
| 8741 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8742 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8743 | |
| 8744 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8745 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8746 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8747 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8748 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8749 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8750 | window->setFocusable(true); |
| 8751 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8752 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8753 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8754 | |
| 8755 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8756 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8757 | window->assertNoEvents(); |
| 8758 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8759 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8760 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8761 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 8762 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8763 | window->assertNoEvents(); |
| 8764 | |
| 8765 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8766 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8767 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8768 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8769 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8770 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8771 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8772 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8773 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8774 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8775 | window->assertNoEvents(); |
| 8776 | } |
| 8777 | |
| 8778 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8779 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8780 | std::make_shared<FakeApplicationHandle>(); |
| 8781 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8782 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8783 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8784 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8785 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8786 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8787 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8788 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8789 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8790 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8791 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8792 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8793 | window->setFocusable(true); |
| 8794 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8795 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8796 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8797 | |
| 8798 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8799 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8800 | window->assertNoEvents(); |
| 8801 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8802 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8803 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8804 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 8805 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8806 | window->assertNoEvents(); |
| 8807 | |
| 8808 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8809 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8810 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8811 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8812 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8813 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8814 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8815 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8816 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8817 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8818 | window->assertNoEvents(); |
| 8819 | } |
| 8820 | |
| 8821 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8822 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8823 | std::make_shared<FakeApplicationHandle>(); |
| 8824 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8825 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8826 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8827 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8828 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8829 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8830 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8831 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8832 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8833 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8834 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8835 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8836 | window->setFocusable(true); |
| 8837 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8838 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8839 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8840 | |
| 8841 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8842 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8843 | window->assertNoEvents(); |
| 8844 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8845 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8846 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8847 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 8848 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8849 | window->assertNoEvents(); |
| 8850 | |
| 8851 | // When the window is no longer obscured because it went on top, it should get input |
| 8852 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8853 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8854 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8855 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8856 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8857 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8858 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8859 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8860 | window->assertNoEvents(); |
| 8861 | } |
| 8862 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8863 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8864 | protected: |
| 8865 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8866 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8867 | sp<FakeWindowHandle> mWindow; |
| 8868 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8869 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8870 | |
| 8871 | void SetUp() override { |
| 8872 | InputDispatcherTest::SetUp(); |
| 8873 | |
| 8874 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8875 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8876 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8877 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8878 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8879 | mSecondWindow = |
| 8880 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8881 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8882 | mThirdWindow = |
| 8883 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8884 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8885 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8886 | |
| 8887 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8889 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8890 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8891 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8892 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8893 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8894 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8895 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8896 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8897 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8898 | mThirdWindow->assertNoEvents(); |
| 8899 | } |
| 8900 | |
| 8901 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8902 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8903 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8904 | SECOND_DISPLAY_ID)) { |
| 8905 | mWindow->assertNoEvents(); |
| 8906 | mSecondWindow->assertNoEvents(); |
| 8907 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8908 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8909 | } |
| 8910 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8911 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8912 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8913 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8914 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8915 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8916 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8917 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8918 | } |
| 8919 | }; |
| 8920 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8921 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8922 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8923 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8924 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8925 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8926 | } |
| 8927 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8928 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8929 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8930 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8931 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8932 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8933 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8934 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8935 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8936 | mWindow->assertNoEvents(); |
| 8937 | mSecondWindow->assertNoEvents(); |
| 8938 | } |
| 8939 | |
| 8940 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8941 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8942 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8943 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8944 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8945 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8946 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8947 | } |
| 8948 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8949 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8950 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8951 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8952 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8953 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8954 | mWindow->assertNoEvents(); |
| 8955 | mSecondWindow->assertNoEvents(); |
| 8956 | } |
| 8957 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8958 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8959 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8960 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8961 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8962 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8963 | mWindow->assertNoEvents(); |
| 8964 | mSecondWindow->assertNoEvents(); |
| 8965 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8966 | } |
| 8967 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8968 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8969 | // Interact with the window first. |
| 8970 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8971 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8972 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8973 | |
| 8974 | // Then remove focus. |
| 8975 | mWindow->setFocusable(false); |
| 8976 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8977 | |
| 8978 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8979 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8980 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8981 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8982 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8983 | } |
| 8984 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8985 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8986 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8987 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8988 | std::shared_ptr<FakeApplicationHandle> application = |
| 8989 | std::make_shared<FakeApplicationHandle>(); |
| 8990 | std::string name = "Fake Spy "; |
| 8991 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8992 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8993 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8994 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8995 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8996 | return spy; |
| 8997 | } |
| 8998 | |
| 8999 | sp<FakeWindowHandle> createForeground() { |
| 9000 | std::shared_ptr<FakeApplicationHandle> application = |
| 9001 | std::make_shared<FakeApplicationHandle>(); |
| 9002 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9003 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 9004 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9005 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9006 | return window; |
| 9007 | } |
| 9008 | |
| 9009 | private: |
| 9010 | int mSpyCount{0}; |
| 9011 | }; |
| 9012 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9013 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9014 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9015 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 9016 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9017 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 9018 | ScopedSilentDeath _silentDeath; |
| 9019 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9020 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 9021 | spy->setTrustedOverlay(false); |
| 9022 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 9023 | ".* not a trusted overlay"); |
| 9024 | } |
| 9025 | |
| 9026 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9027 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 9028 | */ |
| 9029 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9030 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9031 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 9032 | |
| 9033 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9034 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9035 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9036 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9037 | } |
| 9038 | |
| 9039 | /** |
| 9040 | * Verify the order in which different input windows receive events. The touched foreground window |
| 9041 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 9042 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 9043 | * receive events before ones belows it. |
| 9044 | * |
| 9045 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 9046 | * spy1, spy2, window, spy3. |
| 9047 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 9048 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 9049 | * window. |
| 9050 | */ |
| 9051 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 9052 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9053 | auto spy1 = createSpy(); |
| 9054 | auto spy2 = createSpy(); |
| 9055 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9056 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 9057 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 9058 | const size_t numChannels = channels.size(); |
| 9059 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 9060 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9061 | if (!epollFd.ok()) { |
| 9062 | FAIL() << "Failed to create epoll fd"; |
| 9063 | } |
| 9064 | |
| 9065 | for (size_t i = 0; i < numChannels; i++) { |
| 9066 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 9067 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 9068 | FAIL() << "Failed to add fd to epoll"; |
| 9069 | } |
| 9070 | } |
| 9071 | |
| 9072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9073 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9074 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9075 | |
| 9076 | std::vector<size_t> eventOrder; |
| 9077 | std::vector<struct epoll_event> events(numChannels); |
| 9078 | for (;;) { |
| 9079 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 9080 | (100ms).count()); |
| 9081 | if (nFds < 0) { |
| 9082 | FAIL() << "Failed to call epoll_wait"; |
| 9083 | } |
| 9084 | if (nFds == 0) { |
| 9085 | break; // epoll_wait timed out |
| 9086 | } |
| 9087 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 9088 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 9089 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9090 | channels[i]->consumeMotionDown(); |
| 9091 | } |
| 9092 | } |
| 9093 | |
| 9094 | // Verify the order in which the events were received. |
| 9095 | EXPECT_EQ(3u, eventOrder.size()); |
| 9096 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 9097 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 9098 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 9099 | } |
| 9100 | |
| 9101 | /** |
| 9102 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 9103 | */ |
| 9104 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 9105 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9106 | auto spy = createSpy(); |
| 9107 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9108 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9109 | |
| 9110 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9111 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9112 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9113 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9114 | spy->assertNoEvents(); |
| 9115 | } |
| 9116 | |
| 9117 | /** |
| 9118 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 9119 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 9120 | * to the window. |
| 9121 | */ |
| 9122 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 9123 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9124 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9125 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 9126 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9127 | |
| 9128 | // Inject an event outside the spy window's touchable region. |
| 9129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9130 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9131 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9132 | window->consumeMotionDown(); |
| 9133 | spy->assertNoEvents(); |
| 9134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9135 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9136 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9137 | window->consumeMotionUp(); |
| 9138 | spy->assertNoEvents(); |
| 9139 | |
| 9140 | // Inject an event inside the spy window's touchable region. |
| 9141 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9142 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9143 | {5, 10})) |
| 9144 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9145 | window->consumeMotionDown(); |
| 9146 | spy->consumeMotionDown(); |
| 9147 | } |
| 9148 | |
| 9149 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9150 | * 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] | 9151 | * 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] | 9152 | */ |
| 9153 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 9154 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9155 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9156 | auto spy = createSpy(); |
| 9157 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9158 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9159 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 9160 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9161 | |
| 9162 | // Inject an event outside the spy window's frame and touchable region. |
| 9163 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9164 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9165 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9166 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9167 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9168 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9169 | } |
| 9170 | |
| 9171 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9172 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 9173 | * pointers that are down within its bounds. |
| 9174 | */ |
| 9175 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 9176 | auto windowLeft = createForeground(); |
| 9177 | windowLeft->setFrame({0, 0, 100, 200}); |
| 9178 | auto windowRight = createForeground(); |
| 9179 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9180 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9181 | spy->setFrame({0, 0, 200, 200}); |
| 9182 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 9183 | |
| 9184 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9185 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9186 | {50, 50})) |
| 9187 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9188 | windowLeft->consumeMotionDown(); |
| 9189 | spy->consumeMotionDown(); |
| 9190 | |
| 9191 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9192 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9193 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9194 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9195 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9196 | .build(); |
| 9197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9198 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9199 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9200 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9201 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9202 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9203 | } |
| 9204 | |
| 9205 | /** |
| 9206 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 9207 | * the spy should receive the second pointer with ACTION_DOWN. |
| 9208 | */ |
| 9209 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 9210 | auto window = createForeground(); |
| 9211 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9212 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9213 | spyRight->setFrame({100, 0, 200, 200}); |
| 9214 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 9215 | |
| 9216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9217 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9218 | {50, 50})) |
| 9219 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9220 | window->consumeMotionDown(); |
| 9221 | spyRight->assertNoEvents(); |
| 9222 | |
| 9223 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9224 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9225 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9226 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9227 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9228 | .build(); |
| 9229 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9230 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9231 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9232 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9233 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9234 | spyRight->consumeMotionDown(); |
| 9235 | } |
| 9236 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9237 | /** |
| 9238 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 9239 | * windows should be allowed to control split touch. |
| 9240 | */ |
| 9241 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9242 | // 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] | 9243 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9244 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9245 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9246 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9247 | auto window = createForeground(); |
| 9248 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9249 | |
| 9250 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9251 | |
| 9252 | // First finger down, no window touched. |
| 9253 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9254 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9255 | {100, 200})) |
| 9256 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9257 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9258 | window->assertNoEvents(); |
| 9259 | |
| 9260 | // Second finger down on window, the window should receive touch down. |
| 9261 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9262 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9263 | .displayId(ADISPLAY_ID_DEFAULT) |
| 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(100).y(200)) |
| 9266 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -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 | |
| 9273 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9274 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9275 | } |
| 9276 | |
| 9277 | /** |
| 9278 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 9279 | * do not receive key events. |
| 9280 | */ |
| 9281 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9282 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9283 | spy->setFocusable(false); |
| 9284 | |
| 9285 | auto window = createForeground(); |
| 9286 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9287 | setFocusedWindow(window); |
| 9288 | window->consumeFocusEvent(true); |
| 9289 | |
| 9290 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 9291 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9292 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9293 | |
| 9294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 9295 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9296 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9297 | |
| 9298 | spy->assertNoEvents(); |
| 9299 | } |
| 9300 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9301 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 9302 | |
| 9303 | /** |
| 9304 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 9305 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 9306 | */ |
| 9307 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 9308 | auto window = createForeground(); |
| 9309 | auto spy1 = createSpy(); |
| 9310 | auto spy2 = createSpy(); |
| 9311 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 9312 | |
| 9313 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9314 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9315 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9316 | window->consumeMotionDown(); |
| 9317 | spy1->consumeMotionDown(); |
| 9318 | spy2->consumeMotionDown(); |
| 9319 | |
| 9320 | // Pilfer pointers from the second spy window. |
| 9321 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 9322 | spy2->assertNoEvents(); |
| 9323 | spy1->consumeMotionCancel(); |
| 9324 | window->consumeMotionCancel(); |
| 9325 | |
| 9326 | // The rest of the gesture should only be sent to the second spy window. |
| 9327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9328 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 9329 | ADISPLAY_ID_DEFAULT)) |
| 9330 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9331 | spy2->consumeMotionMove(); |
| 9332 | spy1->assertNoEvents(); |
| 9333 | window->assertNoEvents(); |
| 9334 | } |
| 9335 | |
| 9336 | /** |
| 9337 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 9338 | * in the middle of the gesture. |
| 9339 | */ |
| 9340 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 9341 | auto window = createForeground(); |
| 9342 | auto spy = createSpy(); |
| 9343 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9344 | |
| 9345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9346 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9347 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9348 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9349 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9350 | |
| 9351 | window->releaseChannel(); |
| 9352 | |
| 9353 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9354 | |
| 9355 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9356 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9357 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9358 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9359 | } |
| 9360 | |
| 9361 | /** |
| 9362 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 9363 | * the spy, but not to any other windows. |
| 9364 | */ |
| 9365 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 9366 | auto spy = createSpy(); |
| 9367 | auto window = createForeground(); |
| 9368 | |
| 9369 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9370 | |
| 9371 | // First finger down on the window and the spy. |
| 9372 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9373 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9374 | {100, 200})) |
| 9375 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9376 | spy->consumeMotionDown(); |
| 9377 | window->consumeMotionDown(); |
| 9378 | |
| 9379 | // Spy window pilfers the pointers. |
| 9380 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9381 | window->consumeMotionCancel(); |
| 9382 | |
| 9383 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 9384 | const MotionEvent secondFingerDownEvent = |
| 9385 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9386 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9387 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9388 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9389 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9390 | .build(); |
| 9391 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9392 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9393 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9394 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9395 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9396 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9397 | |
| 9398 | // Third finger goes down outside all windows, so injection should fail. |
| 9399 | const MotionEvent thirdFingerDownEvent = |
| 9400 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9401 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9402 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9403 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9404 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 9405 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9406 | .build(); |
| 9407 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 9408 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9409 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9410 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9411 | |
| 9412 | spy->assertNoEvents(); |
| 9413 | window->assertNoEvents(); |
| 9414 | } |
| 9415 | |
| 9416 | /** |
| 9417 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 9418 | */ |
| 9419 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 9420 | auto spy = createSpy(); |
| 9421 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9422 | auto window = createForeground(); |
| 9423 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9424 | |
| 9425 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9426 | |
| 9427 | // First finger down on the window only |
| 9428 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9429 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9430 | {150, 150})) |
| 9431 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9432 | window->consumeMotionDown(); |
| 9433 | |
| 9434 | // Second finger down on the spy and window |
| 9435 | const MotionEvent secondFingerDownEvent = |
| 9436 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9437 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9438 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9439 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9440 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9441 | .build(); |
| 9442 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9443 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9444 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9445 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9446 | spy->consumeMotionDown(); |
| 9447 | window->consumeMotionPointerDown(1); |
| 9448 | |
| 9449 | // Third finger down on the spy and window |
| 9450 | const MotionEvent thirdFingerDownEvent = |
| 9451 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9452 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9453 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9454 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9455 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 9456 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9457 | .build(); |
| 9458 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9459 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9460 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9461 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9462 | spy->consumeMotionPointerDown(1); |
| 9463 | window->consumeMotionPointerDown(2); |
| 9464 | |
| 9465 | // Spy window pilfers the pointers. |
| 9466 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9467 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9468 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9469 | |
| 9470 | spy->assertNoEvents(); |
| 9471 | window->assertNoEvents(); |
| 9472 | } |
| 9473 | |
| 9474 | /** |
| 9475 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 9476 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 9477 | * window, then that window should receive ACTION_CANCEL. |
| 9478 | */ |
| 9479 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 9480 | auto spy = createSpy(); |
| 9481 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9482 | auto window = createForeground(); |
| 9483 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9484 | |
| 9485 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9486 | |
| 9487 | // First finger down on both spy and window |
| 9488 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9489 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9490 | {10, 10})) |
| 9491 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9492 | window->consumeMotionDown(); |
| 9493 | spy->consumeMotionDown(); |
| 9494 | |
| 9495 | // Second finger down on the spy and window |
| 9496 | const MotionEvent secondFingerDownEvent = |
| 9497 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9498 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9499 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9500 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9501 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9502 | .build(); |
| 9503 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9504 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9505 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9506 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9507 | spy->consumeMotionPointerDown(1); |
| 9508 | window->consumeMotionPointerDown(1); |
| 9509 | |
| 9510 | // Spy window pilfers the pointers. |
| 9511 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9512 | window->consumeMotionCancel(); |
| 9513 | |
| 9514 | spy->assertNoEvents(); |
| 9515 | window->assertNoEvents(); |
| 9516 | } |
| 9517 | |
| 9518 | /** |
| 9519 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 9520 | * be sent to other windows |
| 9521 | */ |
| 9522 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 9523 | auto spy = createSpy(); |
| 9524 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9525 | auto window = createForeground(); |
| 9526 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9527 | |
| 9528 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9529 | |
| 9530 | // First finger down on both window and spy |
| 9531 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9532 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9533 | {10, 10})) |
| 9534 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9535 | window->consumeMotionDown(); |
| 9536 | spy->consumeMotionDown(); |
| 9537 | |
| 9538 | // Spy window pilfers the pointers. |
| 9539 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9540 | window->consumeMotionCancel(); |
| 9541 | |
| 9542 | // Second finger down on the window only |
| 9543 | const MotionEvent secondFingerDownEvent = |
| 9544 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9545 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9546 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9547 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9548 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9549 | .build(); |
| 9550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9551 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9552 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9553 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9554 | window->consumeMotionDown(); |
| 9555 | window->assertNoEvents(); |
| 9556 | |
| 9557 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 9558 | spy->consumeMotionMove(); |
| 9559 | spy->assertNoEvents(); |
| 9560 | } |
| 9561 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9562 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 9563 | public: |
| 9564 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 9565 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9566 | std::make_shared<FakeApplicationHandle>(); |
| 9567 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9568 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 9569 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9570 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9571 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9572 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9573 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9574 | overlay->setTrustedOverlay(true); |
| 9575 | |
| 9576 | std::shared_ptr<FakeApplicationHandle> application = |
| 9577 | std::make_shared<FakeApplicationHandle>(); |
| 9578 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9579 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9580 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9581 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9582 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9583 | |
| 9584 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9585 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9586 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9587 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9588 | return {std::move(overlay), std::move(window)}; |
| 9589 | } |
| 9590 | |
| 9591 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9592 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9593 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9594 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9595 | } |
| 9596 | |
| 9597 | void sendStylusEvent(int32_t action) { |
| 9598 | NotifyMotionArgs motionArgs = |
| 9599 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9600 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9601 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9602 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9603 | } |
| 9604 | }; |
| 9605 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9606 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9607 | |
| 9608 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 9609 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9610 | ScopedSilentDeath _silentDeath; |
| 9611 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9612 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9613 | overlay->setTrustedOverlay(false); |
| 9614 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9615 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9616 | ".* not a trusted overlay"); |
| 9617 | } |
| 9618 | |
| 9619 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9620 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9621 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9622 | |
| 9623 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9624 | overlay->consumeMotionDown(); |
| 9625 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9626 | overlay->consumeMotionUp(); |
| 9627 | |
| 9628 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9629 | window->consumeMotionDown(); |
| 9630 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9631 | window->consumeMotionUp(); |
| 9632 | |
| 9633 | overlay->assertNoEvents(); |
| 9634 | window->assertNoEvents(); |
| 9635 | } |
| 9636 | |
| 9637 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9638 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9639 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9640 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9641 | |
| 9642 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9643 | overlay->consumeMotionDown(); |
| 9644 | window->consumeMotionDown(); |
| 9645 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9646 | overlay->consumeMotionUp(); |
| 9647 | window->consumeMotionUp(); |
| 9648 | |
| 9649 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9650 | window->consumeMotionDown(); |
| 9651 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9652 | window->consumeMotionUp(); |
| 9653 | |
| 9654 | overlay->assertNoEvents(); |
| 9655 | window->assertNoEvents(); |
| 9656 | } |
| 9657 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9658 | /** |
| 9659 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9660 | * The scenario is as follows: |
| 9661 | * - The stylus interceptor overlay is configured as a spy window. |
| 9662 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9663 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9664 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9665 | */ |
| 9666 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9667 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9668 | overlay->setSpy(true); |
| 9669 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9670 | |
| 9671 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9672 | overlay->consumeMotionDown(); |
| 9673 | window->consumeMotionDown(); |
| 9674 | |
| 9675 | // The interceptor pilfers the pointers. |
| 9676 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9677 | window->consumeMotionCancel(); |
| 9678 | |
| 9679 | // The interceptor configures itself so that it is no longer a spy. |
| 9680 | overlay->setSpy(false); |
| 9681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9682 | |
| 9683 | // It continues to receive the rest of the stylus gesture. |
| 9684 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9685 | overlay->consumeMotionMove(); |
| 9686 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9687 | overlay->consumeMotionUp(); |
| 9688 | |
| 9689 | window->assertNoEvents(); |
| 9690 | } |
| 9691 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9692 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9693 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9694 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9695 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9696 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9697 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9698 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9699 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9700 | |
| 9701 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9702 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9703 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9704 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9705 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9706 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9707 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9708 | } |
| 9709 | |
| 9710 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9711 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9712 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9713 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9714 | mPolicyFlags); |
| 9715 | } |
| 9716 | |
| 9717 | sp<FakeWindowHandle> createWindow() const { |
| 9718 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9719 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9720 | sp<FakeWindowHandle> window = |
| 9721 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9722 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9723 | window->setOwnerInfo(mPid, mUid); |
| 9724 | return window; |
| 9725 | } |
| 9726 | }; |
| 9727 | |
| 9728 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9729 | |
| 9730 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9731 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9732 | auto window = owner.createWindow(); |
| 9733 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9734 | |
| 9735 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9736 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9737 | window->consumeMotionDown(); |
| 9738 | |
| 9739 | setFocusedWindow(window); |
| 9740 | window->consumeFocusEvent(true); |
| 9741 | |
| 9742 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9743 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9744 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9745 | } |
| 9746 | |
| 9747 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9748 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9749 | auto window = owner.createWindow(); |
| 9750 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9751 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9752 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9753 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9754 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9755 | |
| 9756 | setFocusedWindow(window); |
| 9757 | window->consumeFocusEvent(true); |
| 9758 | |
| 9759 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9760 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9761 | window->assertNoEvents(); |
| 9762 | } |
| 9763 | |
| 9764 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9765 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9766 | auto window = owner.createWindow(); |
| 9767 | auto spy = owner.createWindow(); |
| 9768 | spy->setSpy(true); |
| 9769 | spy->setTrustedOverlay(true); |
| 9770 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9771 | |
| 9772 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9773 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9774 | spy->consumeMotionDown(); |
| 9775 | window->consumeMotionDown(); |
| 9776 | } |
| 9777 | |
| 9778 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9779 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9780 | auto window = owner.createWindow(); |
| 9781 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9782 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9783 | auto randosSpy = rando.createWindow(); |
| 9784 | randosSpy->setSpy(true); |
| 9785 | randosSpy->setTrustedOverlay(true); |
| 9786 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9787 | |
| 9788 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9789 | // not receive the event. |
| 9790 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9791 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9792 | randosSpy->assertNoEvents(); |
| 9793 | window->consumeMotionDown(); |
| 9794 | } |
| 9795 | |
| 9796 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9797 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9798 | auto window = owner.createWindow(); |
| 9799 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9800 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9801 | auto randosSpy = rando.createWindow(); |
| 9802 | randosSpy->setSpy(true); |
| 9803 | randosSpy->setTrustedOverlay(true); |
| 9804 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9805 | |
| 9806 | // A user that has injection permission can inject into any window. |
| 9807 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9808 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9809 | ADISPLAY_ID_DEFAULT)); |
| 9810 | randosSpy->consumeMotionDown(); |
| 9811 | window->consumeMotionDown(); |
| 9812 | |
| 9813 | setFocusedWindow(randosSpy); |
| 9814 | randosSpy->consumeFocusEvent(true); |
| 9815 | |
| 9816 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9817 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9818 | window->assertNoEvents(); |
| 9819 | } |
| 9820 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9821 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9822 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9823 | auto window = owner.createWindow(); |
| 9824 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9825 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9826 | auto randosWindow = rando.createWindow(); |
| 9827 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9828 | randosWindow->setWatchOutsideTouch(true); |
| 9829 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9830 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9831 | // 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] | 9832 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9833 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9834 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9835 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9836 | } |
| 9837 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9838 | } // namespace android::inputdispatcher |