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" |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 19 | #include "EventBuilders.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 20 | |
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 | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 98 | static constexpr int32_t 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. |
| 102 | static constexpr int32_t 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 |
| 106 | static constexpr int32_t MONITOR_PID = 2001; |
| 107 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 108 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 109 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 110 | static constexpr int expectedWallpaperFlags = |
| 111 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 112 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 113 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 114 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 115 | struct PointF { |
| 116 | float x; |
| 117 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 118 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 119 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 121 | /** |
| 122 | * Return a DOWN key event with KEYCODE_A. |
| 123 | */ |
| 124 | static KeyEvent getTestKeyEvent() { |
| 125 | KeyEvent event; |
| 126 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 127 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 128 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 129 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 130 | return event; |
| 131 | } |
| 132 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 133 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 134 | ASSERT_EQ(expectedAction, receivedAction) |
| 135 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 136 | << MotionEvent::actionToString(receivedAction); |
| 137 | } |
| 138 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 139 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 140 | bool matches = action == arg.getAction(); |
| 141 | if (!matches) { |
| 142 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 143 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 144 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 145 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 146 | if (!matches) { |
| 147 | *result_listener << "; "; |
| 148 | } |
| 149 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 150 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 151 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 152 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 153 | if (!matches) { |
| 154 | *result_listener << "; "; |
| 155 | } |
| 156 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 157 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 158 | } |
| 159 | return matches; |
| 160 | } |
| 161 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 162 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 163 | return arg.getDownTime() == downTime; |
| 164 | } |
| 165 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 166 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { |
| 167 | return arg.getDisplayId() == displayId; |
| 168 | } |
| 169 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 170 | MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") { |
| 171 | return arg.getDeviceId() == deviceId; |
| 172 | } |
| 173 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 174 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 175 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 176 | << inputEventSourceToString(arg.getSource()); |
| 177 | return arg.getSource() == source; |
| 178 | } |
| 179 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 180 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { |
| 181 | return arg.getFlags() == flags; |
| 182 | } |
| 183 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 184 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 185 | if (arg.getPointerCount() != 1) { |
| 186 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 187 | return false; |
| 188 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 189 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 192 | MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") { |
| 193 | return arg.getPointerCount() == pointerCount; |
| 194 | } |
| 195 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 196 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 197 | // Build a map for the received pointers, by pointer id |
| 198 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 199 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 200 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 201 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 202 | } |
| 203 | return pointers == actualPointers; |
| 204 | } |
| 205 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | // --- FakeInputDispatcherPolicy --- |
| 207 | |
| 208 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 209 | InputDispatcherConfiguration mConfig; |
| 210 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 211 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 212 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 213 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 214 | FakeInputDispatcherPolicy() = default; |
| 215 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 216 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 217 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 218 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 219 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 220 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 221 | |
| 222 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 223 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 224 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 225 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 226 | } |
| 227 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 228 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 229 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 230 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 231 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 232 | |
| 233 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 234 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 235 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 236 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 237 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 238 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 239 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 240 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 241 | } |
| 242 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 243 | void assertFilterInputEventWasNotCalled() { |
| 244 | std::scoped_lock lock(mLock); |
| 245 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 246 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 247 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 248 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 249 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 250 | ASSERT_TRUE(mConfigurationChangedTime) |
| 251 | << "Timed out waiting for configuration changed call"; |
| 252 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 253 | mConfigurationChangedTime = std::nullopt; |
| 254 | } |
| 255 | |
| 256 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 257 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 258 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 259 | // 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] | 260 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 261 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 262 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 263 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 264 | mLastNotifySwitch = std::nullopt; |
| 265 | } |
| 266 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 267 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 268 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 269 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 270 | mOnPointerDownToken.clear(); |
| 271 | } |
| 272 | |
| 273 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 274 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 275 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 276 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 279 | // This function must be called soon after the expected ANR timer starts, |
| 280 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 281 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 282 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 283 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 284 | std::unique_lock lock(mLock); |
| 285 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 286 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 287 | ASSERT_NO_FATAL_FAILURE( |
| 288 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 289 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 292 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 293 | const sp<WindowInfoHandle>& window) { |
| 294 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 295 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 296 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 299 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 300 | const sp<IBinder>& expectedToken, |
| 301 | int32_t expectedPid) { |
| 302 | std::unique_lock lock(mLock); |
| 303 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 304 | AnrResult result; |
| 305 | ASSERT_NO_FATAL_FAILURE(result = |
| 306 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 307 | const auto& [token, pid] = result; |
| 308 | ASSERT_EQ(expectedToken, token); |
| 309 | ASSERT_EQ(expectedPid, 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, |
| 322 | int32_t expectedPid) { |
| 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)); |
| 328 | const auto& [token, pid] = result; |
| 329 | ASSERT_EQ(expectedToken, token); |
| 330 | ASSERT_EQ(expectedPid, pid); |
| 331 | } |
| 332 | |
| 333 | /** 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] | 334 | sp<IBinder> getResponsiveWindowToken() { |
| 335 | std::unique_lock lock(mLock); |
| 336 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 337 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 338 | const auto& [token, _] = result; |
| 339 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void assertNotifyAnrWasNotCalled() { |
| 343 | std::scoped_lock lock(mLock); |
| 344 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 345 | ASSERT_TRUE(mAnrWindows.empty()); |
| 346 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 347 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 348 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 351 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 352 | mConfig.keyRepeatTimeout = timeout; |
| 353 | mConfig.keyRepeatDelay = delay; |
| 354 | } |
| 355 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 356 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 357 | std::unique_lock lock(mLock); |
| 358 | base::ScopedLockAssertion assumeLocked(mLock); |
| 359 | |
| 360 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 361 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 362 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 363 | enabled; |
| 364 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 365 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 366 | << ") to be called."; |
| 367 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 368 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 369 | auto request = *mPointerCaptureRequest; |
| 370 | mPointerCaptureRequest.reset(); |
| 371 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void assertSetPointerCaptureNotCalled() { |
| 375 | std::unique_lock lock(mLock); |
| 376 | base::ScopedLockAssertion assumeLocked(mLock); |
| 377 | |
| 378 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 379 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 380 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 381 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 382 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 383 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 384 | } |
| 385 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 386 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 387 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 388 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 389 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 390 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 391 | } |
| 392 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 393 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 394 | std::unique_lock lock(mLock); |
| 395 | base::ScopedLockAssertion assumeLocked(mLock); |
| 396 | std::optional<sp<IBinder>> receivedToken = |
| 397 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 398 | mNotifyInputChannelBroken); |
| 399 | ASSERT_TRUE(receivedToken.has_value()); |
| 400 | ASSERT_EQ(token, *receivedToken); |
| 401 | } |
| 402 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 403 | /** |
| 404 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 405 | */ |
| 406 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 407 | mInterceptKeyTimeout = timeout; |
| 408 | } |
| 409 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 410 | void assertUserActivityPoked() { |
| 411 | std::scoped_lock lock(mLock); |
| 412 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; |
| 413 | } |
| 414 | |
| 415 | void assertUserActivityNotPoked() { |
| 416 | std::scoped_lock lock(mLock); |
| 417 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; |
| 418 | } |
| 419 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 420 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 421 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 422 | } |
| 423 | |
| 424 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 425 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 426 | } |
| 427 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 428 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 429 | std::mutex mLock; |
| 430 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 431 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 432 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 433 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 434 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 435 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 436 | |
| 437 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 438 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 439 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 440 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 441 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 442 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 443 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 444 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 445 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 446 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 447 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 448 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 449 | bool mPokedUserActivity GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 450 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 451 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 452 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 453 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 454 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 455 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 456 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 457 | // first entry from the container and erase it. |
| 458 | template <class T> |
| 459 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 460 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 461 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 462 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 463 | // before checking if ANR was called. |
| 464 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 465 | // to provide it some time to act. 100ms seems reasonable. |
| 466 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 467 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 468 | std::optional<T> token = |
| 469 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 470 | if (!token.has_value()) { |
| 471 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 472 | return {}; |
| 473 | } |
| 474 | |
| 475 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 476 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 477 | // the dispatcher started counting before this function was called |
| 478 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 479 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 480 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 481 | << "ms, but waited " |
| 482 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 483 | << "ms instead"; |
| 484 | } |
| 485 | return *token; |
| 486 | } |
| 487 | |
| 488 | template <class T> |
| 489 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 490 | std::queue<T>& storage, |
| 491 | std::unique_lock<std::mutex>& lock, |
| 492 | std::condition_variable& condition) |
| 493 | REQUIRES(mLock) { |
| 494 | condition.wait_for(lock, timeout, |
| 495 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 496 | if (storage.empty()) { |
| 497 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 498 | return std::nullopt; |
| 499 | } |
| 500 | T item = storage.front(); |
| 501 | storage.pop(); |
| 502 | return std::make_optional(item); |
| 503 | } |
| 504 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 505 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 506 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 507 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 510 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 511 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 512 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 513 | ASSERT_TRUE(pid.has_value()); |
| 514 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 515 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 516 | } |
| 517 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 518 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 519 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 520 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 521 | ASSERT_TRUE(pid.has_value()); |
| 522 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 523 | mNotifyAnr.notify_all(); |
| 524 | } |
| 525 | |
| 526 | void notifyNoFocusedWindowAnr( |
| 527 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 528 | std::scoped_lock lock(mLock); |
| 529 | mAnrApplications.push(applicationHandle); |
| 530 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 533 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 534 | std::scoped_lock lock(mLock); |
| 535 | mBrokenInputChannels.push(connectionToken); |
| 536 | mNotifyInputChannelBroken.notify_all(); |
| 537 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 538 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 539 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 540 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 541 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 542 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 543 | const std::vector<float>& values) override {} |
| 544 | |
| 545 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 546 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 547 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 548 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 549 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 550 | InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 552 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 553 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 554 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 555 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 556 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 557 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 561 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 562 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 563 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 564 | break; |
| 565 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 566 | default: { |
| 567 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 568 | break; |
| 569 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 570 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 571 | return true; |
| 572 | } |
| 573 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 574 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 575 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 576 | // Clear intercept state when we handled the event. |
| 577 | mInterceptKeyTimeout = 0ms; |
| 578 | } |
| 579 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 581 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 583 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 584 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 585 | // Clear intercept state so we could dispatch the event in next wake. |
| 586 | mInterceptKeyTimeout = 0ms; |
| 587 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 590 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&, |
| 591 | uint32_t) override { |
| 592 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 595 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 596 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 597 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 598 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 599 | * essentially a passthrough for notifySwitch. |
| 600 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 601 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 604 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { |
| 605 | std::scoped_lock lock(mLock); |
| 606 | mPokedUserActivity = true; |
| 607 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 608 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 609 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 610 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 611 | mOnPointerDownToken = newToken; |
| 612 | } |
| 613 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 614 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 615 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 616 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 617 | mPointerCaptureChangedCondition.notify_all(); |
| 618 | } |
| 619 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 620 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 621 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 622 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 623 | mDropTargetWindowToken = token; |
| 624 | } |
| 625 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 626 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 627 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 628 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 629 | } |
| 630 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 631 | void assertFilterInputEventWasCalledInternal( |
| 632 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 633 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 634 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 635 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 636 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 637 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 638 | }; |
| 639 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 640 | // --- InputDispatcherTest --- |
| 641 | |
| 642 | class InputDispatcherTest : public testing::Test { |
| 643 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 644 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 645 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 646 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 647 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 648 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
| 649 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 650 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 651 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 652 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 655 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 656 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 657 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 658 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 660 | |
| 661 | /** |
| 662 | * Used for debugging when writing the test |
| 663 | */ |
| 664 | void dumpDispatcherState() { |
| 665 | std::string dump; |
| 666 | mDispatcher->dump(dump); |
| 667 | std::stringstream ss(dump); |
| 668 | std::string to; |
| 669 | |
| 670 | while (std::getline(ss, to, '\n')) { |
| 671 | ALOGE("%s", to.c_str()); |
| 672 | } |
| 673 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 674 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 675 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 676 | FocusRequest request; |
| 677 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 678 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 679 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 680 | request.displayId = window->getInfo()->displayId; |
| 681 | mDispatcher->setFocusedWindow(request); |
| 682 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 683 | }; |
| 684 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 685 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 686 | KeyEvent event; |
| 687 | |
| 688 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 689 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 690 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 691 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 692 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 693 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 694 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 695 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 696 | << "Should reject key events with undefined action."; |
| 697 | |
| 698 | // 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] | 699 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 700 | 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] | 701 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 702 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 703 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 704 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 705 | << "Should reject key events with ACTION_MULTIPLE."; |
| 706 | } |
| 707 | |
| 708 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 709 | MotionEvent event; |
| 710 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 711 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 712 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 713 | pointerProperties[i].clear(); |
| 714 | pointerProperties[i].id = i; |
| 715 | pointerCoords[i].clear(); |
| 716 | } |
| 717 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 718 | // Some constants commonly used below |
| 719 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 720 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 721 | constexpr int32_t metaState = AMETA_NONE; |
| 722 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 723 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 724 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 725 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 726 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 727 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 728 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 729 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 730 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 731 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 732 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 733 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 734 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 735 | << "Should reject motion events with undefined action."; |
| 736 | |
| 737 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 738 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 739 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 740 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 741 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 742 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 743 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 744 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 745 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 746 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | << "Should reject motion events with pointer down index too large."; |
| 748 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 749 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 750 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 751 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 752 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 753 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 754 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 755 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 756 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 757 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 758 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 759 | << "Should reject motion events with pointer down index too small."; |
| 760 | |
| 761 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 762 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 763 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 764 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 765 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 766 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 767 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 768 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 769 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 770 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 771 | << "Should reject motion events with pointer up index too large."; |
| 772 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 773 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 774 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 775 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 776 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 777 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 778 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 779 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 780 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 781 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 782 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 783 | << "Should reject motion events with pointer up index too small."; |
| 784 | |
| 785 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 786 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 787 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 788 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 789 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 790 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 791 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 792 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 793 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 794 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | << "Should reject motion events with 0 pointers."; |
| 796 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 797 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 798 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 799 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 800 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 801 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 802 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 803 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 804 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 805 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 806 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 807 | |
| 808 | // Rejects motion events with invalid pointer ids. |
| 809 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 810 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 811 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 812 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 813 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 814 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 815 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 816 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 817 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 818 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 819 | << "Should reject motion events with pointer ids less than 0."; |
| 820 | |
| 821 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 822 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 823 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 824 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 825 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 826 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 827 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 828 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 829 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 830 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 831 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 832 | |
| 833 | // Rejects motion events with duplicate pointer ids. |
| 834 | pointerProperties[0].id = 1; |
| 835 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 836 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 837 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 838 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 839 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 840 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 841 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 842 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 843 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 844 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 845 | << "Should reject motion events with duplicate pointer ids."; |
| 846 | } |
| 847 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 848 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 849 | |
| 850 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 851 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 852 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 853 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 854 | |
| 855 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 856 | } |
| 857 | |
| 858 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 859 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 860 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 861 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 862 | |
| 863 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 864 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 865 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 866 | } |
| 867 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 868 | namespace { |
| 869 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 870 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 871 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 872 | // Default input dispatching timeout if there is no focused application or paused window |
| 873 | // from which to determine an appropriate dispatching timeout. |
| 874 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 875 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 876 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 877 | |
| 878 | class FakeApplicationHandle : public InputApplicationHandle { |
| 879 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 880 | FakeApplicationHandle() { |
| 881 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 882 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 883 | mInfo.dispatchingTimeoutMillis = |
| 884 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 885 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 886 | virtual ~FakeApplicationHandle() {} |
| 887 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 888 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 889 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 890 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 891 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 892 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 893 | }; |
| 894 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 895 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 896 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 897 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 898 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 899 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 900 | } |
| 901 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 902 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 903 | InputEvent* event; |
| 904 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 905 | if (!consumeSeq) { |
| 906 | return nullptr; |
| 907 | } |
| 908 | finishEvent(*consumeSeq); |
| 909 | return event; |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Receive an event without acknowledging it. |
| 914 | * Return the sequence number that could later be used to send finished signal. |
| 915 | */ |
| 916 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 917 | uint32_t consumeSeq; |
| 918 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 919 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 920 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 921 | status_t status = WOULD_BLOCK; |
| 922 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 923 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 924 | &event); |
| 925 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 926 | if (elapsed > 100ms) { |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | if (status == WOULD_BLOCK) { |
| 932 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 933 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | if (status != OK) { |
| 937 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 938 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 939 | } |
| 940 | if (event == nullptr) { |
| 941 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 942 | return std::nullopt; |
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 | if (outEvent != nullptr) { |
| 945 | *outEvent = event; |
| 946 | } |
| 947 | return consumeSeq; |
| 948 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 949 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 950 | /** |
| 951 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 952 | */ |
| 953 | void finishEvent(uint32_t consumeSeq) { |
| 954 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 955 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 958 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 959 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 960 | ASSERT_EQ(OK, status); |
| 961 | } |
| 962 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 963 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 964 | std::optional<int32_t> expectedDisplayId, |
| 965 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 966 | InputEvent* event = consume(); |
| 967 | |
| 968 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 969 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 970 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 971 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 972 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 973 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 974 | if (expectedDisplayId.has_value()) { |
| 975 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 976 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 977 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 978 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 979 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 980 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 981 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 982 | if (expectedFlags.has_value()) { |
| 983 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 984 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 985 | break; |
| 986 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 987 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 988 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 989 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 990 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 991 | if (expectedFlags.has_value()) { |
| 992 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 993 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 994 | break; |
| 995 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 996 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 997 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 998 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 999 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1000 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 1001 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1002 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1003 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 1004 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1005 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1006 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 1007 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1008 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1011 | MotionEvent* consumeMotion() { |
| 1012 | InputEvent* event = consume(); |
| 1013 | |
| 1014 | if (event == nullptr) { |
| 1015 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 1016 | return nullptr; |
| 1017 | } |
| 1018 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1019 | if (event->getType() != InputEventType::MOTION) { |
| 1020 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1021 | return nullptr; |
| 1022 | } |
| 1023 | return static_cast<MotionEvent*>(event); |
| 1024 | } |
| 1025 | |
| 1026 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1027 | MotionEvent* motionEvent = consumeMotion(); |
| 1028 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1029 | ASSERT_THAT(*motionEvent, matcher); |
| 1030 | } |
| 1031 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1032 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 1033 | InputEvent* event = consume(); |
| 1034 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1035 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1036 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1037 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1038 | |
| 1039 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1040 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1041 | |
| 1042 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1043 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1046 | void consumeCaptureEvent(bool hasCapture) { |
| 1047 | const InputEvent* event = consume(); |
| 1048 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1049 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1050 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1051 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1052 | |
| 1053 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1054 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1055 | |
| 1056 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1057 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1058 | } |
| 1059 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1060 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1061 | const InputEvent* event = consume(); |
| 1062 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1063 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1064 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1065 | |
| 1066 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1067 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1068 | |
| 1069 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1070 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1071 | EXPECT_EQ(x, dragEvent.getX()); |
| 1072 | EXPECT_EQ(y, dragEvent.getY()); |
| 1073 | } |
| 1074 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1075 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1076 | const InputEvent* event = consume(); |
| 1077 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1078 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1079 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1080 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1081 | |
| 1082 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1083 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1084 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1085 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1086 | } |
| 1087 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1088 | void assertNoEvents() { |
| 1089 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1090 | if (event == nullptr) { |
| 1091 | return; |
| 1092 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1093 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1094 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1095 | ADD_FAILURE() << "Received key event " |
| 1096 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1097 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1098 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1099 | ADD_FAILURE() << "Received motion event " |
| 1100 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1101 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1102 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1103 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1104 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1105 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1106 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1107 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1108 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1109 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1110 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1111 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1112 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1113 | } |
| 1114 | FAIL() << mName.c_str() |
| 1115 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1119 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1120 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1121 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1122 | protected: |
| 1123 | std::unique_ptr<InputConsumer> mConsumer; |
| 1124 | PreallocatedInputEventFactory mEventFactory; |
| 1125 | |
| 1126 | std::string mName; |
| 1127 | }; |
| 1128 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1129 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1130 | public: |
| 1131 | static const int32_t WIDTH = 600; |
| 1132 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1133 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1134 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1135 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1136 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1137 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1138 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1139 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1140 | dispatcher->createInputChannel(name); |
| 1141 | token = (*channel)->getConnectionToken(); |
| 1142 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | inputApplicationHandle->updateInfo(); |
| 1146 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1147 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1148 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1149 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1150 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1151 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1152 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1153 | mInfo.frameLeft = 0; |
| 1154 | mInfo.frameTop = 0; |
| 1155 | mInfo.frameRight = WIDTH; |
| 1156 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1157 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1158 | mInfo.globalScaleFactor = 1.0; |
| 1159 | mInfo.touchableRegion.clear(); |
| 1160 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1161 | mInfo.ownerPid = WINDOW_PID; |
| 1162 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1163 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1164 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1167 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1168 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1169 | handle->mInfo = mInfo; |
| 1170 | handle->mInfo.displayId = displayId; |
| 1171 | handle->mInfo.id = sId++; |
| 1172 | handle->mInputReceiver = mInputReceiver; |
| 1173 | return handle; |
| 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * This is different from clone, because clone will make a "mirror" window - a window with the |
| 1178 | * same token, but a different ID. The original window and the clone window are allowed to be |
| 1179 | * sent to the dispatcher at the same time - they can coexist inside the dispatcher. |
| 1180 | * This function will create a different object of WindowInfoHandle, but with the same |
| 1181 | * properties as the original object - including the ID. |
| 1182 | * You can use either the old or the new object to consume the events. |
| 1183 | * IMPORTANT: The duplicated object is supposed to replace the original object, and not appear |
| 1184 | * at the same time inside dispatcher. |
| 1185 | */ |
| 1186 | sp<FakeWindowHandle> duplicate() { |
| 1187 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mName); |
| 1188 | handle->mInfo = mInfo; |
| 1189 | handle->mInputReceiver = mInputReceiver; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1190 | return handle; |
| 1191 | } |
| 1192 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1193 | void setTouchable(bool touchable) { |
| 1194 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1195 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1196 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1197 | void setFocusable(bool focusable) { |
| 1198 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1199 | } |
| 1200 | |
| 1201 | void setVisible(bool visible) { |
| 1202 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1203 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1204 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1205 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1206 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1209 | void setPaused(bool paused) { |
| 1210 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1211 | } |
| 1212 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1213 | void setPreventSplitting(bool preventSplitting) { |
| 1214 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | void setSlippery(bool slippery) { |
| 1218 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1219 | } |
| 1220 | |
| 1221 | void setWatchOutsideTouch(bool watchOutside) { |
| 1222 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1223 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1224 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1225 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1226 | |
| 1227 | void setInterceptsStylus(bool interceptsStylus) { |
| 1228 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1229 | } |
| 1230 | |
| 1231 | void setDropInput(bool dropInput) { |
| 1232 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1233 | } |
| 1234 | |
| 1235 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1236 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1237 | } |
| 1238 | |
| 1239 | void setNoInputChannel(bool noInputChannel) { |
| 1240 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1241 | } |
| 1242 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1243 | void setDisableUserActivity(bool disableUserActivity) { |
| 1244 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1245 | } |
| 1246 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1247 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1248 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1249 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1250 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1251 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1252 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1253 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1254 | mInfo.frameLeft = frame.left; |
| 1255 | mInfo.frameTop = frame.top; |
| 1256 | mInfo.frameRight = frame.right; |
| 1257 | mInfo.frameBottom = frame.bottom; |
| 1258 | mInfo.touchableRegion.clear(); |
| 1259 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1260 | |
| 1261 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1262 | ui::Transform translate; |
| 1263 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1264 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1267 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1268 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1269 | void setIsWallpaper(bool isWallpaper) { |
| 1270 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1271 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1272 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1273 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1274 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1275 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1276 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1277 | void setTrustedOverlay(bool trustedOverlay) { |
| 1278 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1279 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1280 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1281 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1282 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1283 | } |
| 1284 | |
| 1285 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1286 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1287 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1288 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1289 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1290 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1293 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1294 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1297 | void consumeMotionCancel(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(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1300 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1304 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1305 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1306 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1310 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1311 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1312 | } |
| 1313 | |
| 1314 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1315 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1316 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1317 | expectedFlags); |
| 1318 | } |
| 1319 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1320 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1321 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1322 | int32_t expectedFlags = 0) { |
| 1323 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1324 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1325 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1329 | int32_t expectedFlags = 0) { |
| 1330 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1331 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1332 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1336 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1337 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1338 | expectedFlags); |
| 1339 | } |
| 1340 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1341 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1342 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1343 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1344 | expectedFlags); |
| 1345 | } |
| 1346 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1347 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1348 | int32_t expectedFlags = 0) { |
| 1349 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1350 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1351 | ASSERT_EQ(InputEventType::MOTION, event->getType()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1352 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1353 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1354 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1355 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1356 | } |
| 1357 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1358 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1359 | ASSERT_NE(mInputReceiver, nullptr) |
| 1360 | << "Cannot consume events from a window with no receiver"; |
| 1361 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1362 | } |
| 1363 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1364 | void consumeCaptureEvent(bool hasCapture) { |
| 1365 | ASSERT_NE(mInputReceiver, nullptr) |
| 1366 | << "Cannot consume events from a window with no receiver"; |
| 1367 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1368 | } |
| 1369 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1370 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1371 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1372 | 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] | 1373 | ASSERT_THAT(*motionEvent, matcher); |
| 1374 | } |
| 1375 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1376 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1377 | std::optional<int32_t> expectedDisplayId, |
| 1378 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1379 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1380 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1381 | expectedFlags); |
| 1382 | } |
| 1383 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1384 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1385 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1386 | } |
| 1387 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1388 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1389 | ASSERT_NE(mInputReceiver, nullptr) |
| 1390 | << "Cannot consume events from a window with no receiver"; |
| 1391 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1392 | } |
| 1393 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1394 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1395 | if (mInputReceiver == nullptr) { |
| 1396 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1397 | return std::nullopt; |
| 1398 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1399 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | void finishEvent(uint32_t sequenceNum) { |
| 1403 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1404 | mInputReceiver->finishEvent(sequenceNum); |
| 1405 | } |
| 1406 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1407 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1408 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1409 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1410 | } |
| 1411 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1412 | InputEvent* consume() { |
| 1413 | if (mInputReceiver == nullptr) { |
| 1414 | return nullptr; |
| 1415 | } |
| 1416 | return mInputReceiver->consume(); |
| 1417 | } |
| 1418 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1419 | MotionEvent* consumeMotion() { |
| 1420 | InputEvent* event = consume(); |
| 1421 | if (event == nullptr) { |
| 1422 | ADD_FAILURE() << "Consume failed : no event"; |
| 1423 | return nullptr; |
| 1424 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1425 | if (event->getType() != InputEventType::MOTION) { |
| 1426 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1427 | return nullptr; |
| 1428 | } |
| 1429 | return static_cast<MotionEvent*>(event); |
| 1430 | } |
| 1431 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1432 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1433 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1434 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1435 | return; // Can't receive events if the window does not have input channel |
| 1436 | } |
| 1437 | ASSERT_NE(nullptr, mInputReceiver) |
| 1438 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1439 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1440 | } |
| 1441 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1442 | sp<IBinder> getToken() { return mInfo.token; } |
| 1443 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1444 | const std::string& getName() { return mName; } |
| 1445 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 1446 | void setOwnerInfo(int32_t ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1447 | mInfo.ownerPid = ownerPid; |
| 1448 | mInfo.ownerUid = ownerUid; |
| 1449 | } |
| 1450 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1451 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1452 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1453 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1454 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1455 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1456 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1457 | private: |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1458 | FakeWindowHandle(std::string name) : mName(name){}; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1459 | const std::string mName; |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1460 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1461 | 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] | 1462 | friend class sp<FakeWindowHandle>; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1463 | }; |
| 1464 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1465 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1466 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1467 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1468 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1469 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1470 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1471 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 1472 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1473 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1474 | KeyEvent event; |
| 1475 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1476 | |
| 1477 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1478 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1479 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1480 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1481 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1482 | if (!allowKeyRepeat) { |
| 1483 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1484 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1485 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1486 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1487 | } |
| 1488 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1489 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1490 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1491 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1494 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1495 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1496 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1497 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1498 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1499 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1500 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1501 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1504 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1505 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1506 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1509 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1510 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1511 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1512 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 1513 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1514 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1515 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1518 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1519 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1520 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1521 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1522 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1523 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1524 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1525 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 1526 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1527 | MotionEventBuilder motionBuilder = |
| 1528 | MotionEventBuilder(action, source) |
| 1529 | .displayId(displayId) |
| 1530 | .eventTime(eventTime) |
| 1531 | .rawXCursorPosition(cursorPosition.x) |
| 1532 | .rawYCursorPosition(cursorPosition.y) |
| 1533 | .pointer( |
| 1534 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1535 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1536 | motionBuilder.downTime(eventTime); |
| 1537 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1538 | |
| 1539 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1540 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1541 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1542 | } |
| 1543 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1544 | static InputEventInjectionResult injectMotionDown( |
| 1545 | const std::unique_ptr<InputDispatcher>& dispatcher, 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_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1550 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1551 | int32_t source, int32_t displayId, |
| 1552 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1553 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1556 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1557 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1558 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1559 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1560 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1561 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1562 | |
| 1563 | return args; |
| 1564 | } |
| 1565 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1566 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1567 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1568 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1569 | // Define a valid key event. |
| 1570 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1571 | displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON, |
| 1572 | currentTime); |
| 1573 | |
| 1574 | return args; |
| 1575 | } |
| 1576 | |
| 1577 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1578 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1579 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1580 | // Define a valid key event. |
| 1581 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1582 | displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT, |
| 1583 | AMETA_NONE, currentTime); |
| 1584 | |
| 1585 | return args; |
| 1586 | } |
| 1587 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1588 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1589 | int32_t displayId, |
| 1590 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1591 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1592 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1593 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1594 | } |
| 1595 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1596 | PointerProperties pointerProperties[pointerCount]; |
| 1597 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1598 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1599 | for (size_t i = 0; i < pointerCount; i++) { |
| 1600 | pointerProperties[i].clear(); |
| 1601 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1602 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1603 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1604 | pointerCoords[i].clear(); |
| 1605 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1606 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1607 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1608 | |
| 1609 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1610 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1611 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1612 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1613 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1614 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1615 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1616 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1617 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1618 | |
| 1619 | return args; |
| 1620 | } |
| 1621 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1622 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1623 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1624 | } |
| 1625 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1626 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1627 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1628 | } |
| 1629 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1630 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1631 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1632 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1635 | } // namespace |
| 1636 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1637 | /** |
| 1638 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1639 | * broken channel. |
| 1640 | */ |
| 1641 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1642 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1643 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1644 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1645 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1646 | |
| 1647 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1648 | |
| 1649 | // Window closes its channel, but the window remains. |
| 1650 | window->destroyReceiver(); |
| 1651 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1652 | } |
| 1653 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1654 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1655 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1656 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1657 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1658 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1659 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1661 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1662 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1663 | |
| 1664 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1665 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1666 | } |
| 1667 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1668 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1669 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1670 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1671 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1672 | |
| 1673 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1674 | // Inject a MotionEvent to an unknown display. |
| 1675 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1676 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1677 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1678 | |
| 1679 | // Window should receive motion event. |
| 1680 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1681 | } |
| 1682 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1683 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1684 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1685 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1686 | * called twice. |
| 1687 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1688 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1689 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1690 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1691 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1692 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1693 | |
| 1694 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1695 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1696 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1697 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1698 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1699 | |
| 1700 | // Window should receive motion event. |
| 1701 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1702 | } |
| 1703 | |
| 1704 | /** |
| 1705 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1706 | */ |
| 1707 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1708 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1709 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1710 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1711 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1712 | |
| 1713 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1714 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1715 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1716 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1717 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1718 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1719 | |
| 1720 | // Window should receive motion event. |
| 1721 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1722 | } |
| 1723 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1724 | // The foreground window should receive the first touch down event. |
| 1725 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1726 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1727 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1728 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1729 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1730 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1731 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1732 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1734 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1735 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1736 | |
| 1737 | // 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] | 1738 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1739 | windowSecond->assertNoEvents(); |
| 1740 | } |
| 1741 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1742 | /** |
| 1743 | * Two windows: A top window, and a wallpaper behind the window. |
| 1744 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1745 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1746 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1747 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1748 | */ |
| 1749 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1750 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1751 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1752 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1753 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1754 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1755 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1756 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1757 | |
| 1758 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1759 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1760 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1761 | {100, 200})) |
| 1762 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1763 | |
| 1764 | // Both foreground window and its wallpaper should receive the touch down |
| 1765 | foregroundWindow->consumeMotionDown(); |
| 1766 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1767 | |
| 1768 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1769 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1770 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1771 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1772 | |
| 1773 | foregroundWindow->consumeMotionMove(); |
| 1774 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1775 | |
| 1776 | // Now the foreground window goes away, but the wallpaper stays |
| 1777 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1778 | foregroundWindow->consumeMotionCancel(); |
| 1779 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1780 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1781 | } |
| 1782 | |
| 1783 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1784 | * Two fingers down on the window, and lift off the first finger. |
| 1785 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1786 | * contains a single pointer. |
| 1787 | */ |
| 1788 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1789 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1790 | sp<FakeWindowHandle> window = |
| 1791 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1792 | |
| 1793 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1794 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1795 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1796 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1797 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1798 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1799 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1800 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1801 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1802 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1803 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1804 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1805 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1806 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1807 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1808 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1809 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1810 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1811 | |
| 1812 | // Remove the window. The gesture should be canceled |
| 1813 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 1814 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1815 | window->consumeMotionEvent( |
| 1816 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1817 | } |
| 1818 | |
| 1819 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1820 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1821 | * with the following differences: |
| 1822 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1823 | * clean up the connection. |
| 1824 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1825 | * Ensure that there's no crash in the dispatcher. |
| 1826 | */ |
| 1827 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1828 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1829 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1830 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1831 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1832 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1833 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1834 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1835 | |
| 1836 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1837 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1838 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1839 | {100, 200})) |
| 1840 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1841 | |
| 1842 | // Both foreground window and its wallpaper should receive the touch down |
| 1843 | foregroundWindow->consumeMotionDown(); |
| 1844 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1845 | |
| 1846 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1847 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1848 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1849 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1850 | |
| 1851 | foregroundWindow->consumeMotionMove(); |
| 1852 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1853 | |
| 1854 | // Wallpaper closes its channel, but the window remains. |
| 1855 | wallpaperWindow->destroyReceiver(); |
| 1856 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1857 | |
| 1858 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1859 | // is no longer valid. |
| 1860 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1861 | foregroundWindow->consumeMotionCancel(); |
| 1862 | } |
| 1863 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1864 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1865 | public ::testing::WithParamInterface<bool> {}; |
| 1866 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1867 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1868 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1869 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1870 | * The top window gets a multitouch gesture. |
| 1871 | * Ensure that wallpaper gets the same gesture. |
| 1872 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1873 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1874 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1875 | sp<FakeWindowHandle> foregroundWindow = |
| 1876 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1877 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1878 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1879 | |
| 1880 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1881 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1882 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1883 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1884 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1885 | |
| 1886 | // Touch down on top window |
| 1887 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1888 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1889 | {100, 100})) |
| 1890 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1891 | |
| 1892 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1893 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1894 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1895 | |
| 1896 | // Second finger down on the top window |
| 1897 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1898 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1899 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1900 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1901 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1902 | .build(); |
| 1903 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1904 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1905 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1906 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1907 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1908 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1909 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1910 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1911 | |
| 1912 | const MotionEvent secondFingerUpEvent = |
| 1913 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1914 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1915 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1916 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1917 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1918 | .build(); |
| 1919 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1920 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1921 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1922 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1923 | foregroundWindow->consumeMotionPointerUp(0); |
| 1924 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1925 | |
| 1926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1927 | injectMotionEvent(mDispatcher, |
| 1928 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1929 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1930 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1931 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1932 | .pointer(PointerBuilder(/* id */ 1, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1933 | ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1934 | .x(100) |
| 1935 | .y(100)) |
| 1936 | .build(), |
| 1937 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1939 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1940 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * Two windows: a window on the left and window on the right. |
| 1945 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1946 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1947 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1948 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1949 | * ACTION_POINTER_DOWN(1). |
| 1950 | */ |
| 1951 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1952 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1953 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1954 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1955 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1956 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1957 | |
| 1958 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1959 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1960 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1961 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1962 | |
| 1963 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1964 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1965 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1966 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1967 | |
| 1968 | mDispatcher->setInputWindows( |
| 1969 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1970 | |
| 1971 | // Touch down on left window |
| 1972 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1973 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1974 | {100, 100})) |
| 1975 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1976 | |
| 1977 | // Both foreground window and its wallpaper should receive the touch down |
| 1978 | leftWindow->consumeMotionDown(); |
| 1979 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1980 | |
| 1981 | // Second finger down on the right window |
| 1982 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1983 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1984 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1985 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1986 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1987 | .build(); |
| 1988 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1989 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1990 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1991 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1992 | |
| 1993 | leftWindow->consumeMotionMove(); |
| 1994 | // Since the touch is split, right window gets ACTION_DOWN |
| 1995 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1996 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1997 | expectedWallpaperFlags); |
| 1998 | |
| 1999 | // Now, leftWindow, which received the first finger, disappears. |
| 2000 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 2001 | leftWindow->consumeMotionCancel(); |
| 2002 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2003 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2004 | |
| 2005 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2006 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2007 | const MotionEvent secondFingerMoveEvent = |
| 2008 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2009 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2010 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2011 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2012 | .build(); |
| 2013 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2014 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2015 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2016 | rightWindow->consumeMotionMove(); |
| 2017 | |
| 2018 | leftWindow->assertNoEvents(); |
| 2019 | rightWindow->assertNoEvents(); |
| 2020 | wallpaperWindow->assertNoEvents(); |
| 2021 | } |
| 2022 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2023 | /** |
| 2024 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2025 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2026 | * The right window should receive ACTION_DOWN. |
| 2027 | */ |
| 2028 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2029 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2030 | sp<FakeWindowHandle> leftWindow = |
| 2031 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2032 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2033 | leftWindow->setDupTouchToWallpaper(true); |
| 2034 | leftWindow->setSlippery(true); |
| 2035 | |
| 2036 | sp<FakeWindowHandle> rightWindow = |
| 2037 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2038 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2039 | |
| 2040 | sp<FakeWindowHandle> wallpaperWindow = |
| 2041 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2042 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2043 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2044 | mDispatcher->setInputWindows( |
| 2045 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2046 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2047 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2048 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2049 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2050 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2051 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2052 | |
| 2053 | // Both foreground window and its wallpaper should receive the touch down |
| 2054 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2055 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2056 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2057 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2058 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2059 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2060 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2061 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2062 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2063 | leftWindow->consumeMotionCancel(); |
| 2064 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2065 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2068 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2069 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2070 | * interactive, it might stop sending this flag. |
| 2071 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2072 | * to have a consistent input stream. |
| 2073 | * |
| 2074 | * Test procedure: |
| 2075 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2076 | * DOWN (new gesture). |
| 2077 | * |
| 2078 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2079 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2080 | * |
| 2081 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2082 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2083 | */ |
| 2084 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2085 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2086 | sp<FakeWindowHandle> spyWindow = |
| 2087 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2088 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2089 | spyWindow->setTrustedOverlay(true); |
| 2090 | spyWindow->setSpy(true); |
| 2091 | |
| 2092 | sp<FakeWindowHandle> window = |
| 2093 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2094 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2095 | |
| 2096 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2097 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2098 | |
| 2099 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2100 | mDispatcher->notifyMotion( |
| 2101 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2102 | .deviceId(touchDeviceId) |
| 2103 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2104 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2105 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2106 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2107 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2108 | .deviceId(touchDeviceId) |
| 2109 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2110 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2111 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2112 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2113 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2114 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2115 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2116 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2117 | |
| 2118 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2119 | mDispatcher->notifyMotion( |
| 2120 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2121 | .deviceId(touchDeviceId) |
| 2122 | .policyFlags(0) |
| 2123 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2124 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2125 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2126 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2127 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2128 | |
| 2129 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2130 | // 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] | 2131 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2132 | |
| 2133 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2134 | mDispatcher->notifyMotion( |
| 2135 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2136 | .deviceId(touchDeviceId) |
| 2137 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2138 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2139 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2140 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2141 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2142 | |
| 2143 | // No more events |
| 2144 | spyWindow->assertNoEvents(); |
| 2145 | window->assertNoEvents(); |
| 2146 | } |
| 2147 | |
| 2148 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2149 | * Two windows: a window on the left and a window on the right. |
| 2150 | * Mouse is hovered from the right window into the left window. |
| 2151 | * Next, we tap on the left window, where the cursor was last seen. |
| 2152 | * The second tap is done onto the right window. |
| 2153 | * The mouse and tap are from two different devices. |
| 2154 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2155 | * explicitly helps during debugging. |
| 2156 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2157 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2158 | */ |
| 2159 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2160 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2161 | sp<FakeWindowHandle> leftWindow = |
| 2162 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2163 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2164 | |
| 2165 | sp<FakeWindowHandle> rightWindow = |
| 2166 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2167 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2168 | |
| 2169 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2170 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2171 | // stale. |
| 2172 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2173 | const int32_t mouseDeviceId = 6; |
| 2174 | const int32_t touchDeviceId = 4; |
| 2175 | // Move the cursor from right |
| 2176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2177 | injectMotionEvent(mDispatcher, |
| 2178 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2179 | AINPUT_SOURCE_MOUSE) |
| 2180 | .deviceId(mouseDeviceId) |
| 2181 | .downTime(baseTime + 10) |
| 2182 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2183 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2184 | .x(300) |
| 2185 | .y(100)) |
| 2186 | .build())); |
| 2187 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2188 | |
| 2189 | // .. to the left window |
| 2190 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2191 | injectMotionEvent(mDispatcher, |
| 2192 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2193 | AINPUT_SOURCE_MOUSE) |
| 2194 | .deviceId(mouseDeviceId) |
| 2195 | .downTime(baseTime + 10) |
| 2196 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2197 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2198 | .x(110) |
| 2199 | .y(100)) |
| 2200 | .build())); |
| 2201 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2202 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2203 | // Now tap the left window |
| 2204 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2205 | injectMotionEvent(mDispatcher, |
| 2206 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2207 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2208 | .deviceId(touchDeviceId) |
| 2209 | .downTime(baseTime + 40) |
| 2210 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2211 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2212 | .x(100) |
| 2213 | .y(100)) |
| 2214 | .build())); |
| 2215 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2216 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2217 | |
| 2218 | // release tap |
| 2219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2220 | injectMotionEvent(mDispatcher, |
| 2221 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2222 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2223 | .deviceId(touchDeviceId) |
| 2224 | .downTime(baseTime + 40) |
| 2225 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2226 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2227 | .x(100) |
| 2228 | .y(100)) |
| 2229 | .build())); |
| 2230 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2231 | |
| 2232 | // Tap the window on the right |
| 2233 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2234 | injectMotionEvent(mDispatcher, |
| 2235 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2236 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2237 | .deviceId(touchDeviceId) |
| 2238 | .downTime(baseTime + 60) |
| 2239 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2240 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2241 | .x(300) |
| 2242 | .y(100)) |
| 2243 | .build())); |
| 2244 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2245 | |
| 2246 | // release tap |
| 2247 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2248 | injectMotionEvent(mDispatcher, |
| 2249 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2250 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2251 | .deviceId(touchDeviceId) |
| 2252 | .downTime(baseTime + 60) |
| 2253 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2254 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2255 | .x(300) |
| 2256 | .y(100)) |
| 2257 | .build())); |
| 2258 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2259 | |
| 2260 | // No more events |
| 2261 | leftWindow->assertNoEvents(); |
| 2262 | rightWindow->assertNoEvents(); |
| 2263 | } |
| 2264 | |
| 2265 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2266 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2267 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2268 | * While the top window is present, the hovering is stopped. |
| 2269 | * Later, hovering gets resumed again. |
| 2270 | * Ensure that new hover gesture is handled correctly. |
| 2271 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2272 | * to the window that's currently being hovered over. |
| 2273 | */ |
| 2274 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2275 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2276 | sp<FakeWindowHandle> window = |
| 2277 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2278 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2279 | |
| 2280 | // Only a single window is present at first |
| 2281 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2282 | |
| 2283 | // Start hovering in the window |
| 2284 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2285 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2286 | .build()); |
| 2287 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2288 | |
| 2289 | // Now, an obscuring window appears! |
| 2290 | sp<FakeWindowHandle> obscuringWindow = |
| 2291 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2292 | ADISPLAY_ID_DEFAULT, |
| 2293 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2294 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2295 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2296 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2297 | obscuringWindow->setNoInputChannel(true); |
| 2298 | obscuringWindow->setFocusable(false); |
| 2299 | obscuringWindow->setAlpha(1.0); |
| 2300 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2301 | |
| 2302 | // While this new obscuring window is present, the hovering is stopped |
| 2303 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2304 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2305 | .build()); |
| 2306 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2307 | |
| 2308 | // Now the obscuring window goes away. |
| 2309 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2310 | |
| 2311 | // And a new hover gesture starts. |
| 2312 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2313 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2314 | .build()); |
| 2315 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2316 | } |
| 2317 | |
| 2318 | /** |
| 2319 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2320 | * the obscuring window. |
| 2321 | */ |
| 2322 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2323 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2324 | sp<FakeWindowHandle> window = |
| 2325 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2326 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2327 | |
| 2328 | // Only a single window is present at first |
| 2329 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2330 | |
| 2331 | // Start hovering in the window |
| 2332 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2333 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2334 | .build()); |
| 2335 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2336 | |
| 2337 | // Now, an obscuring window appears! |
| 2338 | sp<FakeWindowHandle> obscuringWindow = |
| 2339 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2340 | ADISPLAY_ID_DEFAULT, |
| 2341 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2342 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2343 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2344 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2345 | obscuringWindow->setNoInputChannel(true); |
| 2346 | obscuringWindow->setFocusable(false); |
| 2347 | obscuringWindow->setAlpha(1.0); |
| 2348 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2349 | |
| 2350 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2351 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2352 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2353 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2354 | .build()); |
| 2355 | obscuringWindow->assertNoEvents(); |
| 2356 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2357 | |
| 2358 | // Now the obscuring window goes away. |
| 2359 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2360 | |
| 2361 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2362 | // so it should generate a HOVER_ENTER |
| 2363 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2364 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2365 | .build()); |
| 2366 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2367 | |
| 2368 | // Now the MOVE should be getting dispatched normally |
| 2369 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2370 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2371 | .build()); |
| 2372 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2373 | } |
| 2374 | |
| 2375 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2376 | * Two windows: a window on the left and a window on the right. |
| 2377 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2378 | * down. Then, on the left window, also place second touch pointer down. |
| 2379 | * This test tries to reproduce a crash. |
| 2380 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2381 | */ |
| 2382 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { |
| 2383 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2384 | sp<FakeWindowHandle> leftWindow = |
| 2385 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2386 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2387 | |
| 2388 | sp<FakeWindowHandle> rightWindow = |
| 2389 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2390 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2391 | |
| 2392 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2393 | |
| 2394 | const int32_t touchDeviceId = 4; |
| 2395 | const int32_t mouseDeviceId = 6; |
| 2396 | NotifyMotionArgs args; |
| 2397 | |
| 2398 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2399 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2400 | .deviceId(mouseDeviceId) |
| 2401 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2402 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2403 | leftWindow->consumeMotionEvent( |
| 2404 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2405 | |
| 2406 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2407 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2408 | .deviceId(mouseDeviceId) |
| 2409 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2410 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2411 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2412 | |
| 2413 | leftWindow->consumeMotionEvent( |
| 2414 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2415 | leftWindow->consumeMotionEvent( |
| 2416 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2417 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2418 | mDispatcher->notifyMotion( |
| 2419 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2420 | .deviceId(mouseDeviceId) |
| 2421 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2422 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2423 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2424 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2425 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2426 | |
| 2427 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2428 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2429 | .deviceId(touchDeviceId) |
| 2430 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2431 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2432 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2433 | |
| 2434 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2435 | |
| 2436 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2437 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2438 | .deviceId(touchDeviceId) |
| 2439 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2440 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2441 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2442 | leftWindow->consumeMotionEvent( |
| 2443 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2444 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2445 | // current implementation. |
| 2446 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2447 | rightWindow->consumeMotionEvent( |
| 2448 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2449 | |
| 2450 | leftWindow->assertNoEvents(); |
| 2451 | rightWindow->assertNoEvents(); |
| 2452 | } |
| 2453 | |
| 2454 | /** |
| 2455 | * On a single window, use two different devices: mouse and touch. |
| 2456 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2457 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2458 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2459 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2460 | * represent a new gesture. |
| 2461 | */ |
| 2462 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { |
| 2463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2464 | sp<FakeWindowHandle> window = |
| 2465 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2466 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2467 | |
| 2468 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2469 | |
| 2470 | const int32_t touchDeviceId = 4; |
| 2471 | const int32_t mouseDeviceId = 6; |
| 2472 | NotifyMotionArgs args; |
| 2473 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2474 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2475 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2476 | .deviceId(touchDeviceId) |
| 2477 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2478 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2479 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2480 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2481 | .deviceId(touchDeviceId) |
| 2482 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2483 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2484 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2485 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2486 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2487 | .deviceId(touchDeviceId) |
| 2488 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2489 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2490 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2491 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2492 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2493 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2494 | |
| 2495 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2496 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2497 | .deviceId(mouseDeviceId) |
| 2498 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2499 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2500 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2501 | |
| 2502 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2503 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2504 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2505 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2506 | mDispatcher->notifyMotion( |
| 2507 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2508 | .deviceId(mouseDeviceId) |
| 2509 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2510 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2511 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2512 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2513 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2514 | |
| 2515 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2516 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2517 | .deviceId(touchDeviceId) |
| 2518 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2519 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2520 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2521 | // The pointer_down event should be ignored |
| 2522 | window->assertNoEvents(); |
| 2523 | } |
| 2524 | |
| 2525 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2526 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 2527 | * the injected event. |
| 2528 | */ |
| 2529 | TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) { |
| 2530 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2531 | sp<FakeWindowHandle> window = |
| 2532 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2533 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2534 | |
| 2535 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2536 | |
| 2537 | const int32_t touchDeviceId = 4; |
| 2538 | NotifyMotionArgs args; |
| 2539 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 2540 | // completion. |
| 2541 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2542 | injectMotionEvent(mDispatcher, |
| 2543 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2544 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2545 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2546 | .x(50) |
| 2547 | .y(50)) |
| 2548 | .build())); |
| 2549 | window->consumeMotionEvent( |
| 2550 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2551 | |
| 2552 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 2553 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2554 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2555 | .deviceId(touchDeviceId) |
| 2556 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2557 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2558 | |
| 2559 | window->consumeMotionEvent( |
| 2560 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2561 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2562 | } |
| 2563 | |
| 2564 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2565 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2566 | * |
| 2567 | * Two windows: a window on the left and a window on the right. |
| 2568 | * Mouse is hovered over the left window. |
| 2569 | * Next, we tap on the left window, where the cursor was last seen. |
| 2570 | * |
| 2571 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2572 | * the left window. |
| 2573 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2574 | * window (first), and then another on the left window (second). |
| 2575 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2576 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2577 | */ |
| 2578 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2579 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2580 | sp<FakeWindowHandle> leftWindow = |
| 2581 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2582 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2583 | |
| 2584 | sp<FakeWindowHandle> rightWindow = |
| 2585 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2586 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2587 | |
| 2588 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2589 | |
| 2590 | const int32_t mouseDeviceId = 6; |
| 2591 | const int32_t touchDeviceId = 4; |
| 2592 | // Hover over the left window. Keep the cursor there. |
| 2593 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2594 | injectMotionEvent(mDispatcher, |
| 2595 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2596 | AINPUT_SOURCE_MOUSE) |
| 2597 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2598 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2599 | .x(50) |
| 2600 | .y(50)) |
| 2601 | .build())); |
| 2602 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2603 | |
| 2604 | // Tap on left window |
| 2605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2606 | injectMotionEvent(mDispatcher, |
| 2607 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 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 | |
| 2615 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2616 | injectMotionEvent(mDispatcher, |
| 2617 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2618 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2619 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2620 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2621 | .x(100) |
| 2622 | .y(100)) |
| 2623 | .build())); |
| 2624 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2625 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2626 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2627 | |
| 2628 | // First finger down on right window |
| 2629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2630 | injectMotionEvent(mDispatcher, |
| 2631 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2632 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2633 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2634 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2635 | .x(300) |
| 2636 | .y(100)) |
| 2637 | .build())); |
| 2638 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2639 | |
| 2640 | // Second finger down on the left window |
| 2641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2642 | injectMotionEvent(mDispatcher, |
| 2643 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2644 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2645 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2646 | .x(300) |
| 2647 | .y(100)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2648 | .pointer(PointerBuilder(1, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2649 | .x(100) |
| 2650 | .y(100)) |
| 2651 | .build())); |
| 2652 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2653 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2654 | |
| 2655 | // No more events |
| 2656 | leftWindow->assertNoEvents(); |
| 2657 | rightWindow->assertNoEvents(); |
| 2658 | } |
| 2659 | |
| 2660 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2661 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2662 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2663 | * touch is gone, stylus hovering should start working again. |
| 2664 | */ |
| 2665 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 2666 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2667 | sp<FakeWindowHandle> window = |
| 2668 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2669 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2670 | |
| 2671 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2672 | |
| 2673 | const int32_t stylusDeviceId = 5; |
| 2674 | const int32_t touchDeviceId = 4; |
| 2675 | // Start hovering with stylus |
| 2676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2677 | injectMotionEvent(mDispatcher, |
| 2678 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2679 | AINPUT_SOURCE_STYLUS) |
| 2680 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2681 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2682 | .x(50) |
| 2683 | .y(50)) |
| 2684 | .build())); |
| 2685 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2686 | |
| 2687 | // Finger down on the window |
| 2688 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2689 | injectMotionEvent(mDispatcher, |
| 2690 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2691 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2692 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2693 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2694 | .x(100) |
| 2695 | .y(100)) |
| 2696 | .build())); |
| 2697 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2698 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2699 | |
| 2700 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2701 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2702 | injectMotionEvent(mDispatcher, |
| 2703 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2704 | AINPUT_SOURCE_STYLUS) |
| 2705 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2706 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2707 | .x(50) |
| 2708 | .y(50)) |
| 2709 | .build())); |
| 2710 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2711 | // is already down. |
| 2712 | |
| 2713 | // Lift up the finger |
| 2714 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2715 | injectMotionEvent(mDispatcher, |
| 2716 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2717 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2718 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2719 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2720 | .x(100) |
| 2721 | .y(100)) |
| 2722 | .build())); |
| 2723 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2724 | |
| 2725 | // Now that the touch is gone, stylus hovering should start working again |
| 2726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2727 | injectMotionEvent(mDispatcher, |
| 2728 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2729 | AINPUT_SOURCE_STYLUS) |
| 2730 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2731 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2732 | .x(50) |
| 2733 | .y(50)) |
| 2734 | .build())); |
| 2735 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2736 | // No more events |
| 2737 | window->assertNoEvents(); |
| 2738 | } |
| 2739 | |
| 2740 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2741 | * A spy window above a window with no input channel. |
| 2742 | * Start hovering with a stylus device, and then tap with it. |
| 2743 | * Ensure spy window receives the entire sequence. |
| 2744 | */ |
| 2745 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 2746 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2747 | sp<FakeWindowHandle> spyWindow = |
| 2748 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2749 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2750 | spyWindow->setTrustedOverlay(true); |
| 2751 | spyWindow->setSpy(true); |
| 2752 | sp<FakeWindowHandle> window = |
| 2753 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2754 | window->setNoInputChannel(true); |
| 2755 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2756 | |
| 2757 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2758 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2759 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2760 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2761 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2762 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2763 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2764 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2765 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2766 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2767 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2768 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2769 | |
| 2770 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2771 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2772 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2773 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2774 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2775 | |
| 2776 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2777 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 2778 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2779 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2780 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 2781 | |
| 2782 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2783 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2784 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2785 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2786 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2787 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2788 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2789 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2790 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2791 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2792 | |
| 2793 | // No more events |
| 2794 | spyWindow->assertNoEvents(); |
| 2795 | window->assertNoEvents(); |
| 2796 | } |
| 2797 | |
| 2798 | /** |
| 2799 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 2800 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 2801 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 2802 | * While the mouse is down, new move events from the touch device should be ignored. |
| 2803 | */ |
| 2804 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 2805 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2806 | sp<FakeWindowHandle> spyWindow = |
| 2807 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2808 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2809 | spyWindow->setTrustedOverlay(true); |
| 2810 | spyWindow->setSpy(true); |
| 2811 | sp<FakeWindowHandle> window = |
| 2812 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2813 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2814 | |
| 2815 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2816 | |
| 2817 | const int32_t mouseDeviceId = 7; |
| 2818 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2819 | |
| 2820 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2821 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2822 | .deviceId(mouseDeviceId) |
| 2823 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2824 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2825 | spyWindow->consumeMotionEvent( |
| 2826 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2827 | window->consumeMotionEvent( |
| 2828 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2829 | |
| 2830 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2831 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2832 | .deviceId(touchDeviceId) |
| 2833 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 2834 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2835 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2836 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2837 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2838 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2839 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2840 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2841 | .deviceId(touchDeviceId) |
| 2842 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 2843 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2844 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2845 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2846 | |
| 2847 | // Pilfer the stream |
| 2848 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2849 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2850 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2851 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2852 | .deviceId(touchDeviceId) |
| 2853 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 2854 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2855 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2856 | |
| 2857 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2858 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2859 | .deviceId(mouseDeviceId) |
| 2860 | .buttonState(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 | |
| 2864 | spyWindow->consumeMotionEvent( |
| 2865 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2866 | spyWindow->consumeMotionEvent( |
| 2867 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2868 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2869 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2870 | mDispatcher->notifyMotion( |
| 2871 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2872 | .deviceId(mouseDeviceId) |
| 2873 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2874 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2875 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2876 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2877 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2878 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2879 | |
| 2880 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2881 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 2882 | .deviceId(mouseDeviceId) |
| 2883 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2884 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 2885 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2886 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2887 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2888 | |
| 2889 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2890 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2891 | .deviceId(touchDeviceId) |
| 2892 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 2893 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2894 | |
| 2895 | // No more events |
| 2896 | spyWindow->assertNoEvents(); |
| 2897 | window->assertNoEvents(); |
| 2898 | } |
| 2899 | |
| 2900 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2901 | * On the display, have a single window, and also an area where there's no window. |
| 2902 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2903 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2904 | */ |
| 2905 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2906 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2907 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2908 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2909 | |
| 2910 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2911 | |
| 2912 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2913 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2914 | |
| 2915 | mDispatcher->waitForIdle(); |
| 2916 | window->assertNoEvents(); |
| 2917 | |
| 2918 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2919 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2920 | mDispatcher->waitForIdle(); |
| 2921 | window->consumeMotionDown(); |
| 2922 | } |
| 2923 | |
| 2924 | /** |
| 2925 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2926 | * non-touchable window. |
| 2927 | */ |
| 2928 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2929 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2930 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2931 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2932 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2933 | window1->setTouchable(false); |
| 2934 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2935 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2936 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2937 | |
| 2938 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2939 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2940 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2941 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2942 | |
| 2943 | mDispatcher->waitForIdle(); |
| 2944 | window1->assertNoEvents(); |
| 2945 | window2->assertNoEvents(); |
| 2946 | |
| 2947 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2948 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2949 | mDispatcher->waitForIdle(); |
| 2950 | window2->consumeMotionDown(); |
| 2951 | } |
| 2952 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2953 | /** |
| 2954 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2955 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2956 | */ |
| 2957 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2958 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2959 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2960 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2961 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2962 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2963 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2964 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2965 | |
| 2966 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2967 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2968 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2969 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2970 | |
| 2971 | mDispatcher->waitForIdle(); |
| 2972 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2973 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2974 | window2->assertNoEvents(); |
| 2975 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2976 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2977 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2978 | |
| 2979 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2980 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2981 | mDispatcher->waitForIdle(); |
| 2982 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2983 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2984 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2985 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2986 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2987 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2988 | |
| 2989 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2990 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2991 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2992 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2993 | |
| 2994 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2995 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2996 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2997 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2998 | |
| 2999 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3000 | window1->consumeMotionMove(); |
| 3001 | window1->assertNoEvents(); |
| 3002 | |
| 3003 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3004 | mDispatcher->notifyMotion( |
| 3005 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3006 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3007 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3008 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3009 | mDispatcher->notifyMotion( |
| 3010 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3011 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3012 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3015 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3016 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3017 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3018 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3019 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3020 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3021 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3022 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3023 | |
| 3024 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3025 | |
| 3026 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 3027 | |
| 3028 | // 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] | 3029 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3030 | injectMotionEvent(mDispatcher, |
| 3031 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3032 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3033 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3034 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3035 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3036 | |
| 3037 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3038 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3039 | injectMotionEvent(mDispatcher, |
| 3040 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3041 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3042 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3043 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3044 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3045 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3046 | |
| 3047 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3048 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3049 | injectMotionEvent(mDispatcher, |
| 3050 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3051 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3052 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3053 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3054 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3055 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3056 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3057 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3058 | injectMotionEvent(mDispatcher, |
| 3059 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3060 | AINPUT_SOURCE_MOUSE) |
| 3061 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3062 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3063 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3064 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3065 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3066 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3067 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3068 | injectMotionEvent(mDispatcher, |
| 3069 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3070 | AINPUT_SOURCE_MOUSE) |
| 3071 | .buttonState(0) |
| 3072 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3073 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3074 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3075 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3076 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3077 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3078 | injectMotionEvent(mDispatcher, |
| 3079 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3080 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3081 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3082 | .build())); |
| 3083 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3084 | |
| 3085 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3086 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3087 | injectMotionEvent(mDispatcher, |
| 3088 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3089 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3090 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3091 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3092 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3093 | |
| 3094 | // No more events |
| 3095 | windowLeft->assertNoEvents(); |
| 3096 | windowRight->assertNoEvents(); |
| 3097 | } |
| 3098 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3099 | /** |
| 3100 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3101 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3102 | * currently active gesture should be canceled, and the new one should proceed. |
| 3103 | */ |
| 3104 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3105 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3106 | sp<FakeWindowHandle> window = |
| 3107 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3108 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3109 | |
| 3110 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3111 | |
| 3112 | const int32_t touchDeviceId = 4; |
| 3113 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3114 | |
| 3115 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3116 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3117 | .deviceId(touchDeviceId) |
| 3118 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3119 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3120 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3121 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3122 | .deviceId(touchDeviceId) |
| 3123 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3124 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3125 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3126 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3127 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3128 | |
| 3129 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3130 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3131 | .deviceId(mouseDeviceId) |
| 3132 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3133 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3134 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3135 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3136 | WithPointerCount(2u))); |
| 3137 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3138 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3139 | mDispatcher->notifyMotion( |
| 3140 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3141 | .deviceId(mouseDeviceId) |
| 3142 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3143 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3144 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3145 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3146 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3147 | |
| 3148 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3149 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3150 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3151 | .deviceId(touchDeviceId) |
| 3152 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3153 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3154 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3155 | window->assertNoEvents(); |
| 3156 | } |
| 3157 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3158 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3159 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3160 | |
| 3161 | sp<FakeWindowHandle> spyWindow = |
| 3162 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3163 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3164 | spyWindow->setTrustedOverlay(true); |
| 3165 | spyWindow->setSpy(true); |
| 3166 | sp<FakeWindowHandle> window = |
| 3167 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3168 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3169 | |
| 3170 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3171 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3172 | |
| 3173 | // Send mouse cursor to the window |
| 3174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3175 | injectMotionEvent(mDispatcher, |
| 3176 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3177 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3178 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3179 | .x(100) |
| 3180 | .y(100)) |
| 3181 | .build())); |
| 3182 | |
| 3183 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3184 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3185 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3186 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3187 | |
| 3188 | window->assertNoEvents(); |
| 3189 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3192 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3193 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3194 | |
| 3195 | sp<FakeWindowHandle> spyWindow = |
| 3196 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3197 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3198 | spyWindow->setTrustedOverlay(true); |
| 3199 | spyWindow->setSpy(true); |
| 3200 | sp<FakeWindowHandle> window = |
| 3201 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3202 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3203 | |
| 3204 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3205 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3206 | |
| 3207 | // Send mouse cursor to the window |
| 3208 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3209 | injectMotionEvent(mDispatcher, |
| 3210 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3211 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3212 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3213 | .x(100) |
| 3214 | .y(100)) |
| 3215 | .build())); |
| 3216 | |
| 3217 | // Move mouse cursor |
| 3218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3219 | injectMotionEvent(mDispatcher, |
| 3220 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3221 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3222 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3223 | .x(110) |
| 3224 | .y(110)) |
| 3225 | .build())); |
| 3226 | |
| 3227 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3228 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3229 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3230 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3231 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3232 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3233 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3234 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3235 | // Touch down on the window |
| 3236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3237 | injectMotionEvent(mDispatcher, |
| 3238 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3239 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3240 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3241 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3242 | .x(200) |
| 3243 | .y(200)) |
| 3244 | .build())); |
| 3245 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3246 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3247 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3248 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3249 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3250 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3251 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3252 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3253 | |
| 3254 | // pilfer the motion, retaining the gesture on the spy window. |
| 3255 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3256 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3257 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3258 | |
| 3259 | // Touch UP on the window |
| 3260 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3261 | injectMotionEvent(mDispatcher, |
| 3262 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3263 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3264 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3265 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3266 | .x(200) |
| 3267 | .y(200)) |
| 3268 | .build())); |
| 3269 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3270 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3271 | |
| 3272 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3273 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3274 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3275 | |
| 3276 | // One more tap - DOWN |
| 3277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3278 | injectMotionEvent(mDispatcher, |
| 3279 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3280 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3281 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3282 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3283 | .x(250) |
| 3284 | .y(250)) |
| 3285 | .build())); |
| 3286 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3287 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3288 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3289 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3290 | |
| 3291 | // Touch UP on the window |
| 3292 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3293 | injectMotionEvent(mDispatcher, |
| 3294 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3295 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3296 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3297 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3298 | .x(250) |
| 3299 | .y(250)) |
| 3300 | .build())); |
| 3301 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3302 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3303 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3304 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3305 | |
| 3306 | window->assertNoEvents(); |
| 3307 | spyWindow->assertNoEvents(); |
| 3308 | } |
| 3309 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3310 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3311 | // directly in this test. |
| 3312 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3313 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3314 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3315 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3316 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3317 | |
| 3318 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3319 | |
| 3320 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3321 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3322 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3323 | injectMotionEvent(mDispatcher, |
| 3324 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3325 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3326 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3327 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3328 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3329 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3330 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3331 | injectMotionEvent(mDispatcher, |
| 3332 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3333 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3334 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3335 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3336 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3337 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3338 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3339 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3340 | injectMotionEvent(mDispatcher, |
| 3341 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3342 | AINPUT_SOURCE_MOUSE) |
| 3343 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3344 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3345 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3346 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3347 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3348 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3349 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3350 | injectMotionEvent(mDispatcher, |
| 3351 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3352 | AINPUT_SOURCE_MOUSE) |
| 3353 | .buttonState(0) |
| 3354 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3355 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3356 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3357 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3358 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3359 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3360 | injectMotionEvent(mDispatcher, |
| 3361 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3362 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3363 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3364 | .build())); |
| 3365 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3366 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3367 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 3368 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 3369 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3370 | injectMotionEvent(mDispatcher, |
| 3371 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3372 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3373 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3374 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3375 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3376 | } |
| 3377 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3378 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3379 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3380 | * is generated. |
| 3381 | */ |
| 3382 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3383 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3384 | sp<FakeWindowHandle> window = |
| 3385 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3386 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3387 | |
| 3388 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3389 | |
| 3390 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3391 | |
| 3392 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3393 | injectMotionEvent(mDispatcher, |
| 3394 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3395 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3396 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3397 | .x(300) |
| 3398 | .y(400)) |
| 3399 | .build())); |
| 3400 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3401 | |
| 3402 | // Remove the window, but keep the channel. |
| 3403 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3404 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3405 | } |
| 3406 | |
| 3407 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3408 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3409 | */ |
| 3410 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3411 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3412 | sp<FakeWindowHandle> window = |
| 3413 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3414 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3415 | |
| 3416 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3417 | |
| 3418 | const int32_t mouseDeviceId = 7; |
| 3419 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3420 | |
| 3421 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3422 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3423 | .deviceId(mouseDeviceId) |
| 3424 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 3425 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3426 | window->consumeMotionEvent( |
| 3427 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3428 | |
| 3429 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3430 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3431 | .deviceId(touchDeviceId) |
| 3432 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3433 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3434 | |
| 3435 | window->consumeMotionEvent( |
| 3436 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3437 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3438 | } |
| 3439 | |
| 3440 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3441 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3442 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3443 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3444 | */ |
| 3445 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3447 | sp<FakeWindowHandle> window = |
| 3448 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3449 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3450 | |
| 3451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3452 | |
| 3453 | // Inject a hover_move from mouse. |
| 3454 | NotifyMotionArgs motionArgs = |
| 3455 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 3456 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3457 | motionArgs.xCursorPosition = 50; |
| 3458 | motionArgs.yCursorPosition = 50; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3459 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3460 | ASSERT_NO_FATAL_FAILURE( |
| 3461 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3462 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3463 | |
| 3464 | // Tap on the window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3465 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3466 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3467 | {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3468 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3469 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3470 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3471 | |
| 3472 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3473 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3474 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3475 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3476 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3477 | ADISPLAY_ID_DEFAULT, {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3478 | ASSERT_NO_FATAL_FAILURE( |
| 3479 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3480 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3481 | } |
| 3482 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3483 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3484 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3485 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3486 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3487 | ADISPLAY_ID_DEFAULT); |
| 3488 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3489 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3490 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3491 | SECOND_DISPLAY_ID); |
| 3492 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3493 | |
| 3494 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3495 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3496 | |
| 3497 | // Set cursor position in window in default display and check that hover enter and move |
| 3498 | // events are generated. |
| 3499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3500 | injectMotionEvent(mDispatcher, |
| 3501 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3502 | AINPUT_SOURCE_MOUSE) |
| 3503 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3504 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3505 | .x(300) |
| 3506 | .y(600)) |
| 3507 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3508 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3509 | |
| 3510 | // Remove all windows in secondary display and check that no event happens on window in |
| 3511 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3512 | mDispatcher->setInputWindows( |
| 3513 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3514 | windowDefaultDisplay->assertNoEvents(); |
| 3515 | |
| 3516 | // Move cursor position in window in default display and check that only hover move |
| 3517 | // event is generated and not hover enter event. |
| 3518 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3519 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3520 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3521 | injectMotionEvent(mDispatcher, |
| 3522 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3523 | AINPUT_SOURCE_MOUSE) |
| 3524 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3525 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3526 | .x(400) |
| 3527 | .y(700)) |
| 3528 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3529 | windowDefaultDisplay->consumeMotionEvent( |
| 3530 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3531 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3532 | windowDefaultDisplay->assertNoEvents(); |
| 3533 | } |
| 3534 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3535 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3536 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3537 | |
| 3538 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3539 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3540 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3541 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3542 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3543 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3544 | |
| 3545 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3546 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3547 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3548 | |
| 3549 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3550 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3551 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3552 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3553 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3554 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3555 | windowRight->assertNoEvents(); |
| 3556 | } |
| 3557 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3558 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3559 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3560 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3561 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3562 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3563 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3564 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3565 | setFocusedWindow(window); |
| 3566 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3567 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3568 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3569 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3570 | |
| 3571 | // Window should receive key down event. |
| 3572 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3573 | |
| 3574 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3575 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3576 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3577 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3578 | AKEY_EVENT_FLAG_CANCELED); |
| 3579 | } |
| 3580 | |
| 3581 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3582 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3583 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3584 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3585 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3586 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3587 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3588 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3589 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3590 | |
| 3591 | // Window should receive motion down event. |
| 3592 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3593 | |
| 3594 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 3595 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3596 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3597 | window->consumeMotionEvent( |
| 3598 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3599 | } |
| 3600 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 3601 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 3602 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3603 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3604 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3605 | |
| 3606 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3607 | |
| 3608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3609 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 3610 | .build()); |
| 3611 | |
| 3612 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3613 | |
| 3614 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 3615 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 3616 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3617 | |
| 3618 | // After the device has been reset, a new hovering stream can be sent to the window |
| 3619 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3620 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 3621 | .build()); |
| 3622 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3623 | } |
| 3624 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3625 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 3626 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3627 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3628 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3629 | window->setFocusable(true); |
| 3630 | |
| 3631 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3632 | setFocusedWindow(window); |
| 3633 | |
| 3634 | window->consumeFocusEvent(true); |
| 3635 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3636 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3637 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3638 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3639 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3640 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3641 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3642 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3643 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3644 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3645 | } |
| 3646 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 3647 | /** |
| 3648 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 3649 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3650 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3651 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3652 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3653 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3654 | window->setFocusable(true); |
| 3655 | |
| 3656 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3657 | setFocusedWindow(window); |
| 3658 | |
| 3659 | window->consumeFocusEvent(true); |
| 3660 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3661 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3662 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 3663 | |
| 3664 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 3665 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 3666 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 3667 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 3668 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3669 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3670 | } |
| 3671 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3672 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3673 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3674 | * WATCH_OUTSIDE_TOUCH. |
| 3675 | * Both windows are owned by the same UID. |
| 3676 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3677 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3678 | */ |
| 3679 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3680 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3681 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3682 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3683 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3684 | |
| 3685 | sp<FakeWindowHandle> outsideWindow = |
| 3686 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3687 | ADISPLAY_ID_DEFAULT); |
| 3688 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3689 | outsideWindow->setWatchOutsideTouch(true); |
| 3690 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3691 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3692 | |
| 3693 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3694 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3695 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3696 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3697 | window->consumeMotionDown(); |
| 3698 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3699 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3700 | outsideWindow->consumeMotionEvent( |
| 3701 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3702 | } |
| 3703 | |
| 3704 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3705 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3706 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3707 | * ACTION_OUTSIDE event is sent per gesture. |
| 3708 | */ |
| 3709 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3710 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 3711 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3712 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3713 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3714 | window->setWatchOutsideTouch(true); |
| 3715 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3716 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3717 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3718 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3719 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3720 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3721 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3722 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3723 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3724 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3725 | |
| 3726 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3727 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3728 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3729 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3730 | window->assertNoEvents(); |
| 3731 | secondWindow->assertNoEvents(); |
| 3732 | |
| 3733 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3734 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3735 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3736 | ADISPLAY_ID_DEFAULT, |
| 3737 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3738 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3739 | window->consumeMotionEvent( |
| 3740 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3741 | secondWindow->consumeMotionDown(); |
| 3742 | thirdWindow->assertNoEvents(); |
| 3743 | |
| 3744 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3745 | // 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] | 3746 | mDispatcher->notifyMotion( |
| 3747 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3748 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3749 | window->assertNoEvents(); |
| 3750 | secondWindow->consumeMotionMove(); |
| 3751 | thirdWindow->consumeMotionDown(); |
| 3752 | } |
| 3753 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3754 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3755 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3756 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3757 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3758 | window->setFocusable(true); |
| 3759 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3760 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3761 | setFocusedWindow(window); |
| 3762 | |
| 3763 | window->consumeFocusEvent(true); |
| 3764 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3765 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3766 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3767 | mDispatcher->notifyKey(keyDown); |
| 3768 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3769 | |
| 3770 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3771 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3772 | |
| 3773 | // 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] | 3774 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3775 | |
| 3776 | window->consumeFocusEvent(false); |
| 3777 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3778 | mDispatcher->notifyKey(keyDown); |
| 3779 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3780 | window->assertNoEvents(); |
| 3781 | } |
| 3782 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3783 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3784 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3785 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3786 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3787 | // Ensure window is non-split and have some transform. |
| 3788 | window->setPreventSplitting(true); |
| 3789 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3790 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3791 | |
| 3792 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3793 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3794 | {50, 50})) |
| 3795 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3796 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3797 | |
| 3798 | const MotionEvent secondFingerDownEvent = |
| 3799 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3800 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3801 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3802 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 3803 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3804 | .build(); |
| 3805 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3806 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3807 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3808 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3809 | |
| 3810 | const MotionEvent* event = window->consumeMotion(); |
| 3811 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3812 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3813 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3814 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3815 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3816 | } |
| 3817 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 3818 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 3819 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3820 | sp<FakeWindowHandle> window = |
| 3821 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3822 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3823 | sp<FakeWindowHandle> trustedOverlay = |
| 3824 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 3825 | ADISPLAY_ID_DEFAULT); |
| 3826 | trustedOverlay->setSpy(true); |
| 3827 | trustedOverlay->setTrustedOverlay(true); |
| 3828 | |
| 3829 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}}); |
| 3830 | |
| 3831 | // Start a three-finger touchpad swipe |
| 3832 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3833 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3834 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3835 | .build()); |
| 3836 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 3837 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3838 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3839 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3840 | .build()); |
| 3841 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 3842 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3843 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3844 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 3845 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3846 | .build()); |
| 3847 | |
| 3848 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3849 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3850 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 3851 | |
| 3852 | // Move the swipe a bit |
| 3853 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3854 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3855 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3856 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3857 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3858 | .build()); |
| 3859 | |
| 3860 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3861 | |
| 3862 | // End the swipe |
| 3863 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 3864 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3865 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3866 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3867 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3868 | .build()); |
| 3869 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 3870 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3871 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3872 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3873 | .build()); |
| 3874 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3875 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3876 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3877 | .build()); |
| 3878 | |
| 3879 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 3880 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 3881 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3882 | |
| 3883 | window->assertNoEvents(); |
| 3884 | } |
| 3885 | |
| 3886 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 3887 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3888 | sp<FakeWindowHandle> window = |
| 3889 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3890 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3891 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3892 | |
| 3893 | // Start a three-finger touchpad swipe |
| 3894 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3895 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3896 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3897 | .build()); |
| 3898 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 3899 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3900 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3901 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3902 | .build()); |
| 3903 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 3904 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3905 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3906 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 3907 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3908 | .build()); |
| 3909 | |
| 3910 | // Move the swipe a bit |
| 3911 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3912 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3913 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3914 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3915 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3916 | .build()); |
| 3917 | |
| 3918 | // End the swipe |
| 3919 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 3920 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3921 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3922 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3923 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3924 | .build()); |
| 3925 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 3926 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3927 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3928 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3929 | .build()); |
| 3930 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3931 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3932 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3933 | .build()); |
| 3934 | |
| 3935 | window->assertNoEvents(); |
| 3936 | } |
| 3937 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3938 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 3939 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 3940 | * the first pointer. |
| 3941 | * Ensure that the second pointer is not sent to the window. |
| 3942 | * |
| 3943 | * The subsequent gesture should be correctly delivered to the window. |
| 3944 | */ |
| 3945 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 3946 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3947 | sp<FakeWindowHandle> window = |
| 3948 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3949 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3950 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3951 | |
| 3952 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3953 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3954 | .downTime(baseTime + 10) |
| 3955 | .eventTime(baseTime + 10) |
| 3956 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3957 | .build()); |
| 3958 | |
| 3959 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3960 | |
| 3961 | // We need a new window object for the same window, because dispatcher will store objects by |
| 3962 | // reference. That means that the testing code and the dispatcher will refer to the same shared |
| 3963 | // object. Calling window->setTransform here would affect dispatcher's comparison |
| 3964 | // of the old window to the new window, since both the old window and the new window would be |
| 3965 | // updated to the same value. |
| 3966 | sp<FakeWindowHandle> windowDup = window->duplicate(); |
| 3967 | |
| 3968 | // Change the transform so that the orientation is now different from original. |
| 3969 | windowDup->setWindowTransform(0, -1, 1, 0); |
| 3970 | |
| 3971 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDup}}}); |
| 3972 | |
| 3973 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3974 | |
| 3975 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3976 | .downTime(baseTime + 10) |
| 3977 | .eventTime(baseTime + 30) |
| 3978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3979 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 3980 | .build()); |
| 3981 | |
| 3982 | // Finish the gesture and start a new one. Ensure the new gesture is sent to the window |
| 3983 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3984 | .downTime(baseTime + 10) |
| 3985 | .eventTime(baseTime + 40) |
| 3986 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3987 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 3988 | .build()); |
| 3989 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3990 | .downTime(baseTime + 10) |
| 3991 | .eventTime(baseTime + 50) |
| 3992 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3993 | .build()); |
| 3994 | |
| 3995 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3996 | .downTime(baseTime + 60) |
| 3997 | .eventTime(baseTime + 60) |
| 3998 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 3999 | .build()); |
| 4000 | |
| 4001 | windowDup->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4002 | } |
| 4003 | |
| 4004 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4005 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4006 | * |
| 4007 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4008 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4009 | * space. |
| 4010 | */ |
| 4011 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4012 | public: |
| 4013 | void SetUp() override { |
| 4014 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4015 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4019 | gui::DisplayInfo info; |
| 4020 | info.displayId = displayId; |
| 4021 | info.transform = transform; |
| 4022 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4023 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4024 | } |
| 4025 | |
| 4026 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4027 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4028 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4029 | } |
| 4030 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4031 | void removeAllWindowsAndDisplays() { |
| 4032 | mDisplayInfos.clear(); |
| 4033 | mWindowInfos.clear(); |
| 4034 | } |
| 4035 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4036 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4037 | // on the display. |
| 4038 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4039 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4040 | // respectively. |
| 4041 | ui::Transform displayTransform; |
| 4042 | displayTransform.set(2, 0, 0, 4); |
| 4043 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4044 | |
| 4045 | std::shared_ptr<FakeApplicationHandle> application = |
| 4046 | std::make_shared<FakeApplicationHandle>(); |
| 4047 | |
| 4048 | // Add two windows to the display. Their frames are represented in the display space. |
| 4049 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4050 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4051 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4052 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 4053 | addWindow(firstWindow); |
| 4054 | |
| 4055 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4056 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4057 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4058 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 4059 | addWindow(secondWindow); |
| 4060 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 4061 | } |
| 4062 | |
| 4063 | private: |
| 4064 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 4065 | std::vector<gui::WindowInfo> mWindowInfos; |
| 4066 | }; |
| 4067 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4068 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4069 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4070 | // 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] | 4071 | // selected so that if the hit test was performed with the point and the bounds being in |
| 4072 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4073 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4074 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4075 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4076 | |
| 4077 | firstWindow->consumeMotionDown(); |
| 4078 | secondWindow->assertNoEvents(); |
| 4079 | } |
| 4080 | |
| 4081 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 4082 | // the event should be treated as being in the logical display space. |
| 4083 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 4084 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4085 | // Send down to the first window. The point is represented in the logical display space. The |
| 4086 | // point is selected so that if the hit test was done in logical display space, then it would |
| 4087 | // end up in the incorrect window. |
| 4088 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4089 | PointF{75 * 2, 55 * 4}); |
| 4090 | |
| 4091 | firstWindow->consumeMotionDown(); |
| 4092 | secondWindow->assertNoEvents(); |
| 4093 | } |
| 4094 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4095 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 4096 | // event should be treated as being in the logical display space. |
| 4097 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 4098 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4099 | |
| 4100 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4101 | ui::Transform injectedEventTransform; |
| 4102 | injectedEventTransform.set(matrix); |
| 4103 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 4104 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 4105 | |
| 4106 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4107 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4108 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4109 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4110 | .x(untransformedPoint.x) |
| 4111 | .y(untransformedPoint.y)) |
| 4112 | .build(); |
| 4113 | event.transform(matrix); |
| 4114 | |
| 4115 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 4116 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4117 | |
| 4118 | firstWindow->consumeMotionDown(); |
| 4119 | secondWindow->assertNoEvents(); |
| 4120 | } |
| 4121 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4122 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4123 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4124 | |
| 4125 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4126 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4127 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4128 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4129 | |
| 4130 | firstWindow->assertNoEvents(); |
| 4131 | const MotionEvent* event = secondWindow->consumeMotion(); |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 4132 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4133 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 4134 | |
| 4135 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 4136 | EXPECT_EQ(300, event->getRawX(0)); |
| 4137 | EXPECT_EQ(880, event->getRawY(0)); |
| 4138 | |
| 4139 | // Ensure that the x and y values are in the window's coordinate space. |
| 4140 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4141 | // the logical display space. This will be the origin of the window space. |
| 4142 | EXPECT_EQ(100, event->getX(0)); |
| 4143 | EXPECT_EQ(80, event->getY(0)); |
| 4144 | } |
| 4145 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4146 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4147 | class InputDispatcherDisplayOrientationFixture |
| 4148 | : public InputDispatcherDisplayProjectionTest, |
| 4149 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 4150 | |
| 4151 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 4152 | // in different locations on the display, specifically points close to the four corners of a |
| 4153 | // window. |
| 4154 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 4155 | constexpr static int32_t displayWidth = 400; |
| 4156 | constexpr static int32_t displayHeight = 800; |
| 4157 | |
| 4158 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4159 | |
| 4160 | const auto rotation = GetParam(); |
| 4161 | |
| 4162 | // Set up the display with the specified rotation. |
| 4163 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4164 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4165 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4166 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4167 | logicalDisplayWidth, logicalDisplayHeight); |
| 4168 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4169 | |
| 4170 | // Create a window with its bounds determined in the logical display. |
| 4171 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 4172 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4173 | sp<FakeWindowHandle> window = |
| 4174 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4175 | window->setFrame(frameInDisplay, displayTransform); |
| 4176 | addWindow(window); |
| 4177 | |
| 4178 | // The following points in logical display space should be inside the window. |
| 4179 | static const std::array<vec2, 4> insidePoints{ |
| 4180 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4181 | for (const auto pointInsideWindow : insidePoints) { |
| 4182 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4183 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4184 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4185 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4186 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4187 | window->consumeMotionDown(); |
| 4188 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4189 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4190 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4191 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4192 | window->consumeMotionUp(); |
| 4193 | } |
| 4194 | |
| 4195 | // The following points in logical display space should be outside the window. |
| 4196 | static const std::array<vec2, 5> outsidePoints{ |
| 4197 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4198 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4199 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4200 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4201 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4202 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4203 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4204 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4205 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4206 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4207 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4208 | } |
| 4209 | window->assertNoEvents(); |
| 4210 | } |
| 4211 | |
| 4212 | // Run the precision tests for all rotations. |
| 4213 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4214 | InputDispatcherDisplayOrientationFixture, |
| 4215 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4216 | ui::ROTATION_270), |
| 4217 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4218 | return ftl::enum_string(testParamInfo.param); |
| 4219 | }); |
| 4220 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4221 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4222 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4223 | |
| 4224 | class TransferTouchFixture : public InputDispatcherTest, |
| 4225 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4226 | |
| 4227 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4228 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4229 | |
| 4230 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4231 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4232 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4233 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4234 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4235 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4236 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4237 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4238 | sp<FakeWindowHandle> wallpaper = |
| 4239 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 4240 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4241 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4242 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4243 | |
| 4244 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4245 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4246 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4247 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4248 | // Only the first window should get the down event |
| 4249 | firstWindow->consumeMotionDown(); |
| 4250 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4251 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4252 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4253 | // Transfer touch to the second window |
| 4254 | TransferFunction f = GetParam(); |
| 4255 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4256 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4257 | // The first window gets cancel and the second gets down |
| 4258 | firstWindow->consumeMotionCancel(); |
| 4259 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4260 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4261 | |
| 4262 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4263 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4264 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4265 | // The first window gets no events and the second gets up |
| 4266 | firstWindow->assertNoEvents(); |
| 4267 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4268 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4269 | } |
| 4270 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4271 | /** |
| 4272 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 4273 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 4274 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 4275 | * natural to the user. |
| 4276 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 4277 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 4278 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 4279 | * the first window to the second. |
| 4280 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 4281 | * the other API, as well. |
| 4282 | */ |
| 4283 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 4284 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4285 | |
| 4286 | // Create a couple of windows + a spy window |
| 4287 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4288 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4289 | spyWindow->setTrustedOverlay(true); |
| 4290 | spyWindow->setSpy(true); |
| 4291 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4292 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4293 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4294 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4295 | |
| 4296 | // Add the windows to the dispatcher |
| 4297 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 4298 | |
| 4299 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4300 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4301 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4302 | // Only the first window and spy should get the down event |
| 4303 | spyWindow->consumeMotionDown(); |
| 4304 | firstWindow->consumeMotionDown(); |
| 4305 | |
| 4306 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 4307 | // if f === 'transferTouch'. |
| 4308 | TransferFunction f = GetParam(); |
| 4309 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4310 | ASSERT_TRUE(success); |
| 4311 | // The first window gets cancel and the second gets down |
| 4312 | firstWindow->consumeMotionCancel(); |
| 4313 | secondWindow->consumeMotionDown(); |
| 4314 | |
| 4315 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4316 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4317 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4318 | // The first window gets no events and the second+spy get up |
| 4319 | firstWindow->assertNoEvents(); |
| 4320 | spyWindow->consumeMotionUp(); |
| 4321 | secondWindow->consumeMotionUp(); |
| 4322 | } |
| 4323 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4324 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4325 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4326 | |
| 4327 | PointF touchPoint = {10, 10}; |
| 4328 | |
| 4329 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4330 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4331 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4332 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4333 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4334 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4335 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4336 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4337 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4338 | |
| 4339 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4340 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4341 | |
| 4342 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4343 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4344 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4345 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4346 | // Only the first window should get the down event |
| 4347 | firstWindow->consumeMotionDown(); |
| 4348 | secondWindow->assertNoEvents(); |
| 4349 | |
| 4350 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4351 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4352 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4353 | // Only the first window should get the pointer down event |
| 4354 | firstWindow->consumeMotionPointerDown(1); |
| 4355 | secondWindow->assertNoEvents(); |
| 4356 | |
| 4357 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4358 | TransferFunction f = GetParam(); |
| 4359 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4360 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4361 | // The first window gets cancel and the second gets down and pointer down |
| 4362 | firstWindow->consumeMotionCancel(); |
| 4363 | secondWindow->consumeMotionDown(); |
| 4364 | secondWindow->consumeMotionPointerDown(1); |
| 4365 | |
| 4366 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4367 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4368 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4369 | // The first window gets nothing and the second gets pointer up |
| 4370 | firstWindow->assertNoEvents(); |
| 4371 | secondWindow->consumeMotionPointerUp(1); |
| 4372 | |
| 4373 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4374 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4375 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4376 | // The first window gets nothing and the second gets up |
| 4377 | firstWindow->assertNoEvents(); |
| 4378 | secondWindow->consumeMotionUp(); |
| 4379 | } |
| 4380 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4381 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 4382 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4383 | |
| 4384 | // Create a couple of windows |
| 4385 | sp<FakeWindowHandle> firstWindow = |
| 4386 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4387 | ADISPLAY_ID_DEFAULT); |
| 4388 | firstWindow->setDupTouchToWallpaper(true); |
| 4389 | sp<FakeWindowHandle> secondWindow = |
| 4390 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4391 | ADISPLAY_ID_DEFAULT); |
| 4392 | secondWindow->setDupTouchToWallpaper(true); |
| 4393 | |
| 4394 | sp<FakeWindowHandle> wallpaper1 = |
| 4395 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 4396 | wallpaper1->setIsWallpaper(true); |
| 4397 | |
| 4398 | sp<FakeWindowHandle> wallpaper2 = |
| 4399 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 4400 | wallpaper2->setIsWallpaper(true); |
| 4401 | // Add the windows to the dispatcher |
| 4402 | mDispatcher->setInputWindows( |
| 4403 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 4404 | |
| 4405 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4406 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4407 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4408 | |
| 4409 | // Only the first window should get the down event |
| 4410 | firstWindow->consumeMotionDown(); |
| 4411 | secondWindow->assertNoEvents(); |
| 4412 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4413 | wallpaper2->assertNoEvents(); |
| 4414 | |
| 4415 | // Transfer touch focus to the second window |
| 4416 | TransferFunction f = GetParam(); |
| 4417 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4418 | ASSERT_TRUE(success); |
| 4419 | |
| 4420 | // The first window gets cancel and the second gets down |
| 4421 | firstWindow->consumeMotionCancel(); |
| 4422 | secondWindow->consumeMotionDown(); |
| 4423 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4424 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4425 | |
| 4426 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4427 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4428 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4429 | // The first window gets no events and the second gets up |
| 4430 | firstWindow->assertNoEvents(); |
| 4431 | secondWindow->consumeMotionUp(); |
| 4432 | wallpaper1->assertNoEvents(); |
| 4433 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4434 | } |
| 4435 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4436 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 4437 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 4438 | // for the case where there are multiple pointers split across several windows. |
| 4439 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 4440 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4441 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4442 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4443 | return dispatcher->transferTouch(destChannelToken, |
| 4444 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4445 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4446 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4447 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4448 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4449 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4450 | })); |
| 4451 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4452 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4453 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4454 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4455 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4456 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4457 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4458 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4459 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4460 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4461 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4462 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4463 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4464 | |
| 4465 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4466 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4467 | |
| 4468 | PointF pointInFirst = {300, 200}; |
| 4469 | PointF pointInSecond = {300, 600}; |
| 4470 | |
| 4471 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4472 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4473 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4474 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4475 | // Only the first window should get the down event |
| 4476 | firstWindow->consumeMotionDown(); |
| 4477 | secondWindow->assertNoEvents(); |
| 4478 | |
| 4479 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4480 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4481 | ADISPLAY_ID_DEFAULT, |
| 4482 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4483 | // The first window gets a move and the second a down |
| 4484 | firstWindow->consumeMotionMove(); |
| 4485 | secondWindow->consumeMotionDown(); |
| 4486 | |
| 4487 | // Transfer touch focus to the second window |
| 4488 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4489 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 4490 | firstWindow->consumeMotionCancel(); |
| 4491 | secondWindow->consumeMotionPointerDown(1); |
| 4492 | |
| 4493 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4494 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4495 | ADISPLAY_ID_DEFAULT, |
| 4496 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4497 | // The first window gets nothing and the second gets pointer up |
| 4498 | firstWindow->assertNoEvents(); |
| 4499 | secondWindow->consumeMotionPointerUp(1); |
| 4500 | |
| 4501 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4502 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4503 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4504 | // The first window gets nothing and the second gets up |
| 4505 | firstWindow->assertNoEvents(); |
| 4506 | secondWindow->consumeMotionUp(); |
| 4507 | } |
| 4508 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4509 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 4510 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 4511 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 4512 | // window should get nothing. |
| 4513 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 4514 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4515 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4516 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4517 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4518 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4519 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4520 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4521 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4522 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4523 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4524 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4525 | |
| 4526 | // Add the windows to the dispatcher |
| 4527 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4528 | |
| 4529 | PointF pointInFirst = {300, 200}; |
| 4530 | PointF pointInSecond = {300, 600}; |
| 4531 | |
| 4532 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4533 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4534 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4535 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4536 | // Only the first window should get the down event |
| 4537 | firstWindow->consumeMotionDown(); |
| 4538 | secondWindow->assertNoEvents(); |
| 4539 | |
| 4540 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4541 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4542 | ADISPLAY_ID_DEFAULT, |
| 4543 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4544 | // The first window gets a move and the second a down |
| 4545 | firstWindow->consumeMotionMove(); |
| 4546 | secondWindow->consumeMotionDown(); |
| 4547 | |
| 4548 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4549 | const bool transferred = |
| 4550 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4551 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 4552 | ASSERT_FALSE(transferred); |
| 4553 | firstWindow->assertNoEvents(); |
| 4554 | secondWindow->assertNoEvents(); |
| 4555 | |
| 4556 | // The rest of the dispatch should proceed as normal |
| 4557 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4558 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4559 | ADISPLAY_ID_DEFAULT, |
| 4560 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4561 | // The first window gets MOVE and the second gets pointer up |
| 4562 | firstWindow->consumeMotionMove(); |
| 4563 | secondWindow->consumeMotionUp(); |
| 4564 | |
| 4565 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4566 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4567 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4568 | // The first window gets nothing and the second gets up |
| 4569 | firstWindow->consumeMotionUp(); |
| 4570 | secondWindow->assertNoEvents(); |
| 4571 | } |
| 4572 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4573 | // This case will create two windows and one mirrored window on the default display and mirror |
| 4574 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 4575 | // the windows info of second display before default display. |
| 4576 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 4577 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4578 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4579 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4580 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4581 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4582 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4583 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4584 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4585 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4586 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4587 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4588 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4589 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4590 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4591 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4592 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4593 | |
| 4594 | // Update window info, let it find window handle of second display first. |
| 4595 | mDispatcher->setInputWindows( |
| 4596 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4597 | {ADISPLAY_ID_DEFAULT, |
| 4598 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4599 | |
| 4600 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4601 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4602 | {50, 50})) |
| 4603 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4604 | |
| 4605 | // Window should receive motion event. |
| 4606 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4607 | |
| 4608 | // Transfer touch focus |
| 4609 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 4610 | secondWindowInPrimary->getToken())); |
| 4611 | // The first window gets cancel. |
| 4612 | firstWindowInPrimary->consumeMotionCancel(); |
| 4613 | secondWindowInPrimary->consumeMotionDown(); |
| 4614 | |
| 4615 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4616 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4617 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4618 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4619 | firstWindowInPrimary->assertNoEvents(); |
| 4620 | secondWindowInPrimary->consumeMotionMove(); |
| 4621 | |
| 4622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4623 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4624 | {150, 50})) |
| 4625 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4626 | firstWindowInPrimary->assertNoEvents(); |
| 4627 | secondWindowInPrimary->consumeMotionUp(); |
| 4628 | } |
| 4629 | |
| 4630 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 4631 | // 'transferTouch' api. |
| 4632 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 4633 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4634 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4635 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4636 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4637 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4638 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4639 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4640 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4641 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4642 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4643 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4644 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4645 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4646 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4647 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4648 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4649 | |
| 4650 | // Update window info, let it find window handle of second display first. |
| 4651 | mDispatcher->setInputWindows( |
| 4652 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4653 | {ADISPLAY_ID_DEFAULT, |
| 4654 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4655 | |
| 4656 | // Touch on second display. |
| 4657 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4658 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 4659 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4660 | |
| 4661 | // Window should receive motion event. |
| 4662 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4663 | |
| 4664 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4665 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4666 | |
| 4667 | // The first window gets cancel. |
| 4668 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4669 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4670 | |
| 4671 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4672 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4673 | SECOND_DISPLAY_ID, {150, 50})) |
| 4674 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4675 | firstWindowInPrimary->assertNoEvents(); |
| 4676 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 4677 | |
| 4678 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4679 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 4680 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4681 | firstWindowInPrimary->assertNoEvents(); |
| 4682 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 4683 | } |
| 4684 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4685 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4686 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4687 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4688 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4689 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4690 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4691 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4692 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4693 | |
| 4694 | window->consumeFocusEvent(true); |
| 4695 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4696 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4697 | |
| 4698 | // Window should receive key down event. |
| 4699 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4700 | |
| 4701 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4702 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4703 | mFakePolicy->assertUserActivityPoked(); |
| 4704 | } |
| 4705 | |
| 4706 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 4707 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4708 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4709 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4710 | |
| 4711 | window->setDisableUserActivity(true); |
| 4712 | window->setFocusable(true); |
| 4713 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4714 | setFocusedWindow(window); |
| 4715 | |
| 4716 | window->consumeFocusEvent(true); |
| 4717 | |
| 4718 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4719 | |
| 4720 | // Window should receive key down event. |
| 4721 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4722 | |
| 4723 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4724 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4725 | mFakePolicy->assertUserActivityNotPoked(); |
| 4726 | } |
| 4727 | |
| 4728 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 4729 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4730 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4731 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4732 | |
| 4733 | window->setFocusable(true); |
| 4734 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4735 | setFocusedWindow(window); |
| 4736 | |
| 4737 | window->consumeFocusEvent(true); |
| 4738 | |
| 4739 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4740 | mDispatcher->waitForIdle(); |
| 4741 | |
| 4742 | // System key is not passed down |
| 4743 | window->assertNoEvents(); |
| 4744 | |
| 4745 | // Should have poked user activity |
| 4746 | mFakePolicy->assertUserActivityPoked(); |
| 4747 | } |
| 4748 | |
| 4749 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 4750 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4751 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4752 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4753 | |
| 4754 | window->setFocusable(true); |
| 4755 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4756 | setFocusedWindow(window); |
| 4757 | |
| 4758 | window->consumeFocusEvent(true); |
| 4759 | |
| 4760 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4761 | mDispatcher->waitForIdle(); |
| 4762 | |
| 4763 | // System key is not passed down |
| 4764 | window->assertNoEvents(); |
| 4765 | |
| 4766 | // Should have poked user activity |
| 4767 | mFakePolicy->assertUserActivityPoked(); |
| 4768 | } |
| 4769 | |
| 4770 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 4771 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4772 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4773 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4774 | |
| 4775 | window->setDisableUserActivity(true); |
| 4776 | window->setFocusable(true); |
| 4777 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4778 | setFocusedWindow(window); |
| 4779 | |
| 4780 | window->consumeFocusEvent(true); |
| 4781 | |
| 4782 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4783 | mDispatcher->waitForIdle(); |
| 4784 | |
| 4785 | // System key is not passed down |
| 4786 | window->assertNoEvents(); |
| 4787 | |
| 4788 | // Should have poked user activity |
| 4789 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4790 | } |
| 4791 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4792 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 4793 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4794 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4795 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4796 | |
| 4797 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4798 | |
| 4799 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4800 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4801 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 4802 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4803 | |
| 4804 | window->consumeMotionEvent( |
| 4805 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4806 | |
| 4807 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4808 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4809 | mFakePolicy->assertUserActivityPoked(); |
| 4810 | } |
| 4811 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4812 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4813 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4814 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4815 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4816 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4817 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4818 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4819 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4820 | mDispatcher->waitForIdle(); |
| 4821 | |
| 4822 | window->assertNoEvents(); |
| 4823 | } |
| 4824 | |
| 4825 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4826 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4827 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4828 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4829 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4830 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4831 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4832 | |
| 4833 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4834 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4835 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4836 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4837 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4838 | |
| 4839 | // Window should receive only the motion event |
| 4840 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4841 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4842 | } |
| 4843 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4844 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4845 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4846 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4847 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4848 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4849 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4850 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4851 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4852 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4853 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4854 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4855 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4856 | |
| 4857 | // Add the windows to the dispatcher |
| 4858 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4859 | |
| 4860 | PointF pointInFirst = {300, 200}; |
| 4861 | PointF pointInSecond = {300, 600}; |
| 4862 | |
| 4863 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4864 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4865 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4866 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4867 | // Only the first window should get the down event |
| 4868 | firstWindow->consumeMotionDown(); |
| 4869 | secondWindow->assertNoEvents(); |
| 4870 | |
| 4871 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4872 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4873 | ADISPLAY_ID_DEFAULT, |
| 4874 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4875 | // The first window gets a move and the second a down |
| 4876 | firstWindow->consumeMotionMove(); |
| 4877 | secondWindow->consumeMotionDown(); |
| 4878 | |
| 4879 | // Send pointer cancel to the second window |
| 4880 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4881 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4882 | {pointInFirst, pointInSecond}); |
| 4883 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4884 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4885 | // The first window gets move and the second gets cancel. |
| 4886 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4887 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4888 | |
| 4889 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4890 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4891 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4892 | // The first window gets up and the second gets nothing. |
| 4893 | firstWindow->consumeMotionUp(); |
| 4894 | secondWindow->assertNoEvents(); |
| 4895 | } |
| 4896 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4897 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 4898 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4899 | |
| 4900 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4901 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4902 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4903 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 4904 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 4905 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 4906 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4907 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4908 | window->assertNoEvents(); |
| 4909 | mDispatcher->waitForIdle(); |
| 4910 | } |
| 4911 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4912 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4913 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4914 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4915 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4916 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4917 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4918 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4921 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 4922 | |
| 4923 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4924 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 4925 | expectedFlags); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4926 | } |
| 4927 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4928 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 4929 | |
| 4930 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 4931 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4932 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4933 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4934 | expectedDisplayId, expectedFlags); |
| 4935 | } |
| 4936 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4937 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4938 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4939 | expectedDisplayId, expectedFlags); |
| 4940 | } |
| 4941 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4942 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4943 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4944 | expectedDisplayId, expectedFlags); |
| 4945 | } |
| 4946 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4947 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4948 | mInputReceiver->consumeMotionEvent( |
| 4949 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4950 | WithDisplayId(expectedDisplayId), |
| 4951 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4954 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 4955 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 4956 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4957 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4958 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4961 | MotionEvent* consumeMotion() { |
| 4962 | InputEvent* event = mInputReceiver->consume(); |
| 4963 | if (!event) { |
| 4964 | ADD_FAILURE() << "No event was produced"; |
| 4965 | return nullptr; |
| 4966 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4967 | if (event->getType() != InputEventType::MOTION) { |
| 4968 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4969 | return nullptr; |
| 4970 | } |
| 4971 | return static_cast<MotionEvent*>(event); |
| 4972 | } |
| 4973 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4974 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 4975 | |
| 4976 | private: |
| 4977 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4978 | }; |
| 4979 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4980 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 4981 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4982 | /** |
| 4983 | * Two entities that receive touch: A window, and a global monitor. |
| 4984 | * The touch goes to the window, and then the window disappears. |
| 4985 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 4986 | * for the monitor, as well. |
| 4987 | * 1. foregroundWindow |
| 4988 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 4989 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4990 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4991 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4992 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4993 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4994 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4995 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4996 | |
| 4997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4998 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4999 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5000 | {100, 200})) |
| 5001 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5002 | |
| 5003 | // Both the foreground window and the global monitor should receive the touch down |
| 5004 | window->consumeMotionDown(); |
| 5005 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5006 | |
| 5007 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5008 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5009 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5010 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5011 | |
| 5012 | window->consumeMotionMove(); |
| 5013 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5014 | |
| 5015 | // Now the foreground window goes away |
| 5016 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5017 | window->consumeMotionCancel(); |
| 5018 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 5019 | |
| 5020 | // If more events come in, there will be no more foreground window to send them to. This will |
| 5021 | // cause a cancel for the monitor, as well. |
| 5022 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5023 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5024 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 5025 | << "Injection should fail because the window was removed"; |
| 5026 | window->assertNoEvents(); |
| 5027 | // Global monitor now gets the cancel |
| 5028 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5029 | } |
| 5030 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5031 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5032 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5033 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5034 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5035 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5036 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5037 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5038 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5040 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5041 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5042 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5043 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5046 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 5047 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5048 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5049 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5050 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5051 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5052 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5053 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5055 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5056 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5057 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5058 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5059 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5060 | // Pilfer pointers from the monitor. |
| 5061 | // This should not do anything and the window should continue to receive events. |
| 5062 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5063 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5065 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5066 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5067 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5068 | |
| 5069 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5070 | window->consumeMotionMove(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, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5074 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5075 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5076 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5077 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5078 | window->setWindowOffset(20, 40); |
| 5079 | window->setWindowTransform(0, 1, -1, 0); |
| 5080 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5081 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5082 | |
| 5083 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5084 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5085 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5086 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5087 | MotionEvent* event = monitor.consumeMotion(); |
| 5088 | // Even though window has transform, gesture monitor must not. |
| 5089 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 5090 | } |
| 5091 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5092 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5093 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5094 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5095 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5096 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5097 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5098 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 5099 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5100 | } |
| 5101 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5102 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5103 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5104 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5105 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5106 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5107 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5108 | |
| 5109 | NotifyMotionArgs motionArgs = |
| 5110 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5111 | ADISPLAY_ID_DEFAULT); |
| 5112 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5113 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5114 | // Window should receive motion down event. |
| 5115 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5116 | |
| 5117 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5118 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5119 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5120 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5121 | motionArgs.pointerCoords[0].getX() - 10); |
| 5122 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5123 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5124 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5125 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5126 | } |
| 5127 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5128 | /** |
| 5129 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 5130 | * the device default right away. In the test scenario, we check both the default value, |
| 5131 | * and the action of enabling / disabling. |
| 5132 | */ |
| 5133 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5134 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5135 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5136 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5137 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5138 | |
| 5139 | // Set focused application. |
| 5140 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5141 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5142 | |
| 5143 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5144 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5145 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5146 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5147 | |
| 5148 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5149 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5150 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5151 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5152 | |
| 5153 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5154 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5155 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5156 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5157 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5158 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5159 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5160 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5161 | |
| 5162 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5163 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5164 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5165 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5166 | |
| 5167 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5168 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5169 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5170 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5171 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5172 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5173 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5174 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5175 | |
| 5176 | window->assertNoEvents(); |
| 5177 | } |
| 5178 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5179 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5180 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5181 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5182 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5183 | |
| 5184 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5185 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5186 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5187 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5188 | setFocusedWindow(window); |
| 5189 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5190 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5191 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5192 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5193 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5194 | |
| 5195 | InputEvent* event = window->consume(); |
| 5196 | ASSERT_NE(event, nullptr); |
| 5197 | |
| 5198 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5199 | ASSERT_NE(verified, nullptr); |
| 5200 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 5201 | |
| 5202 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 5203 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 5204 | ASSERT_EQ(keyArgs.source, verified->source); |
| 5205 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 5206 | |
| 5207 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 5208 | |
| 5209 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5210 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5211 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5212 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 5213 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 5214 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 5215 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 5216 | } |
| 5217 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5218 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5219 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5220 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5221 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5222 | |
| 5223 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5224 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5225 | ui::Transform transform; |
| 5226 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5227 | |
| 5228 | gui::DisplayInfo displayInfo; |
| 5229 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5230 | displayInfo.transform = transform; |
| 5231 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5232 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5233 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5234 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5235 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5236 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5237 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5238 | |
| 5239 | InputEvent* event = window->consume(); |
| 5240 | ASSERT_NE(event, nullptr); |
| 5241 | |
| 5242 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5243 | ASSERT_NE(verified, nullptr); |
| 5244 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 5245 | |
| 5246 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 5247 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 5248 | EXPECT_EQ(motionArgs.source, verified->source); |
| 5249 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 5250 | |
| 5251 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 5252 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5253 | const vec2 rawXY = |
| 5254 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 5255 | motionArgs.pointerCoords[0].getXYValue()); |
| 5256 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 5257 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5258 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5259 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5260 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5261 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 5262 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 5263 | } |
| 5264 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5265 | /** |
| 5266 | * Ensure that separate calls to sign the same data are generating the same key. |
| 5267 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 5268 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 5269 | * tests. |
| 5270 | */ |
| 5271 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 5272 | KeyEvent event = getTestKeyEvent(); |
| 5273 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5274 | |
| 5275 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 5276 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5277 | ASSERT_EQ(hmac1, hmac2); |
| 5278 | } |
| 5279 | |
| 5280 | /** |
| 5281 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 5282 | */ |
| 5283 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 5284 | KeyEvent event = getTestKeyEvent(); |
| 5285 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5286 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 5287 | |
| 5288 | verifiedEvent.deviceId += 1; |
| 5289 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5290 | |
| 5291 | verifiedEvent.source += 1; |
| 5292 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5293 | |
| 5294 | verifiedEvent.eventTimeNanos += 1; |
| 5295 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5296 | |
| 5297 | verifiedEvent.displayId += 1; |
| 5298 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5299 | |
| 5300 | verifiedEvent.action += 1; |
| 5301 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5302 | |
| 5303 | verifiedEvent.downTimeNanos += 1; |
| 5304 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5305 | |
| 5306 | verifiedEvent.flags += 1; |
| 5307 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5308 | |
| 5309 | verifiedEvent.keyCode += 1; |
| 5310 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5311 | |
| 5312 | verifiedEvent.scanCode += 1; |
| 5313 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5314 | |
| 5315 | verifiedEvent.metaState += 1; |
| 5316 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5317 | |
| 5318 | verifiedEvent.repeatCount += 1; |
| 5319 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5320 | } |
| 5321 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5322 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 5323 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5324 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5325 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5326 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5327 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5328 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5329 | |
| 5330 | // Top window is also focusable but is not granted focus. |
| 5331 | windowTop->setFocusable(true); |
| 5332 | windowSecond->setFocusable(true); |
| 5333 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5334 | setFocusedWindow(windowSecond); |
| 5335 | |
| 5336 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5337 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5338 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5339 | |
| 5340 | // Focused window should receive event. |
| 5341 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5342 | windowTop->assertNoEvents(); |
| 5343 | } |
| 5344 | |
| 5345 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5346 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5347 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5348 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5349 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5350 | |
| 5351 | window->setFocusable(true); |
| 5352 | // Release channel for window is no longer valid. |
| 5353 | window->releaseChannel(); |
| 5354 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5355 | setFocusedWindow(window); |
| 5356 | |
| 5357 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5358 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5359 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5360 | |
| 5361 | // window channel is invalid, so it should not receive any input event. |
| 5362 | window->assertNoEvents(); |
| 5363 | } |
| 5364 | |
| 5365 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5366 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5367 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5368 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5369 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5370 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5371 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5372 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5373 | setFocusedWindow(window); |
| 5374 | |
| 5375 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5376 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5377 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5378 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5379 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5380 | window->assertNoEvents(); |
| 5381 | } |
| 5382 | |
| 5383 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5384 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5385 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5386 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5387 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5388 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5389 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5390 | |
| 5391 | windowTop->setFocusable(true); |
| 5392 | windowSecond->setFocusable(true); |
| 5393 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5394 | setFocusedWindow(windowTop); |
| 5395 | windowTop->consumeFocusEvent(true); |
| 5396 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5397 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
| 5398 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5399 | windowSecond->consumeFocusEvent(true); |
| 5400 | windowTop->consumeFocusEvent(false); |
| 5401 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5402 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5403 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5404 | |
| 5405 | // Focused window should receive event. |
| 5406 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5407 | } |
| 5408 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5409 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5410 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5411 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5412 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5413 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5414 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5415 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5416 | |
| 5417 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5418 | windowSecond->setFocusable(false); |
| 5419 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5420 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5421 | setFocusedWindow(windowTop); |
| 5422 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5423 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5425 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5426 | |
| 5427 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5428 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5429 | windowSecond->assertNoEvents(); |
| 5430 | } |
| 5431 | |
| 5432 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5433 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5434 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5435 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5436 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5437 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5438 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5439 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5440 | |
| 5441 | window->setFocusable(true); |
| 5442 | previousFocusedWindow->setFocusable(true); |
| 5443 | window->setVisible(false); |
| 5444 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5445 | setFocusedWindow(previousFocusedWindow); |
| 5446 | previousFocusedWindow->consumeFocusEvent(true); |
| 5447 | |
| 5448 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5449 | setFocusedWindow(window); |
| 5450 | previousFocusedWindow->consumeFocusEvent(false); |
| 5451 | |
| 5452 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5453 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5454 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5455 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5456 | |
| 5457 | // Window does not get focus event or key down. |
| 5458 | window->assertNoEvents(); |
| 5459 | |
| 5460 | // Window becomes visible. |
| 5461 | window->setVisible(true); |
| 5462 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5463 | |
| 5464 | // Window receives focus event. |
| 5465 | window->consumeFocusEvent(true); |
| 5466 | // Focused window receives key down. |
| 5467 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5468 | } |
| 5469 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5470 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5471 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5472 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5473 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5474 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5475 | |
| 5476 | // window is granted focus. |
| 5477 | window->setFocusable(true); |
| 5478 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5479 | setFocusedWindow(window); |
| 5480 | window->consumeFocusEvent(true); |
| 5481 | |
| 5482 | // When a display is removed window loses focus. |
| 5483 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5484 | window->consumeFocusEvent(false); |
| 5485 | } |
| 5486 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5487 | /** |
| 5488 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5489 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5490 | * of the 'slipperyEnterWindow'. |
| 5491 | * |
| 5492 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5493 | * a way so that the touched location is no longer covered by the top window. |
| 5494 | * |
| 5495 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5496 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5497 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5498 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5499 | * with ACTION_DOWN). |
| 5500 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5501 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5502 | * |
| 5503 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5504 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5505 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5506 | * |
| 5507 | * In this test, we ensure that the event received by the bottom window has |
| 5508 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5509 | */ |
| 5510 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5511 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5512 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5513 | |
| 5514 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5515 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5516 | |
| 5517 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5518 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5519 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5520 | // Make sure this one overlaps the bottom window |
| 5521 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5522 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5523 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5524 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5525 | |
| 5526 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5527 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5528 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5529 | |
| 5530 | mDispatcher->setInputWindows( |
| 5531 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5532 | |
| 5533 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5534 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5535 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5536 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5537 | slipperyExitWindow->consumeMotionDown(); |
| 5538 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5539 | mDispatcher->setInputWindows( |
| 5540 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5541 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5542 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 5543 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5544 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5545 | |
| 5546 | slipperyExitWindow->consumeMotionCancel(); |
| 5547 | |
| 5548 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5549 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5550 | } |
| 5551 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 5552 | /** |
| 5553 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 5554 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 5555 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 5556 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 5557 | * reproduces a crash. |
| 5558 | */ |
| 5559 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 5560 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5561 | |
| 5562 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 5563 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5564 | leftSlipperyWindow->setSlippery(true); |
| 5565 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5566 | |
| 5567 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 5568 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5569 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 5570 | rightDropTouchesWindow->setDropInput(true); |
| 5571 | |
| 5572 | mDispatcher->setInputWindows( |
| 5573 | {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}}); |
| 5574 | |
| 5575 | // Start touch in the left window |
| 5576 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5577 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5578 | .build()); |
| 5579 | leftSlipperyWindow->consumeMotionDown(); |
| 5580 | |
| 5581 | // And move it into the right window |
| 5582 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5583 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5584 | .build()); |
| 5585 | |
| 5586 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 5587 | // The left window continues to receive the gesture. |
| 5588 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5589 | rightDropTouchesWindow->assertNoEvents(); |
| 5590 | } |
| 5591 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5592 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5593 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5594 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5595 | |
| 5596 | sp<FakeWindowHandle> leftWindow = |
| 5597 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5598 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5599 | leftWindow->setOwnerInfo(1, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5600 | |
| 5601 | sp<FakeWindowHandle> rightSpy = |
| 5602 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 5603 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5604 | rightSpy->setOwnerInfo(2, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5605 | rightSpy->setSpy(true); |
| 5606 | rightSpy->setTrustedOverlay(true); |
| 5607 | |
| 5608 | sp<FakeWindowHandle> rightWindow = |
| 5609 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5610 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5611 | rightWindow->setOwnerInfo(3, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5612 | |
| 5613 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}}); |
| 5614 | |
| 5615 | // Touch in the left window |
| 5616 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5617 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5618 | .build()); |
| 5619 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 5620 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5621 | ASSERT_NO_FATAL_FAILURE( |
| 5622 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5623 | |
| 5624 | // Touch another finger over the right windows |
| 5625 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5626 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5627 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5628 | .build()); |
| 5629 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 5630 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 5631 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 5632 | mDispatcher->waitForIdle(); |
| 5633 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5634 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 5635 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5636 | |
| 5637 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 5638 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 5639 | // is part of the UP action, we do not treat this as device interaction. |
| 5640 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5641 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5642 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5643 | .build()); |
| 5644 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 5645 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 5646 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 5647 | mDispatcher->waitForIdle(); |
| 5648 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5649 | |
| 5650 | // Move remaining finger |
| 5651 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5652 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5653 | .build()); |
| 5654 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 5655 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 5656 | mDispatcher->waitForIdle(); |
| 5657 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5658 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5659 | |
| 5660 | // Release all fingers |
| 5661 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5662 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5663 | .build()); |
| 5664 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 5665 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 5666 | mDispatcher->waitForIdle(); |
| 5667 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5668 | } |
| 5669 | |
| 5670 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 5671 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5672 | |
| 5673 | sp<FakeWindowHandle> window = |
| 5674 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5675 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5676 | window->setOwnerInfo(1, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5677 | |
| 5678 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5679 | setFocusedWindow(window); |
| 5680 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 5681 | |
| 5682 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 5683 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 5684 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 5685 | ASSERT_NO_FATAL_FAILURE( |
| 5686 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5687 | |
| 5688 | // The UP actions are not treated as device interaction. |
| 5689 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 5690 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 5691 | mDispatcher->waitForIdle(); |
| 5692 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5693 | } |
| 5694 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5695 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5696 | protected: |
| 5697 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5698 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5699 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5700 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5701 | sp<FakeWindowHandle> mWindow; |
| 5702 | |
| 5703 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5704 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5705 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5706 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 5707 | mDispatcher->requestRefreshConfiguration(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5708 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 5709 | ASSERT_EQ(OK, mDispatcher->start()); |
| 5710 | |
| 5711 | setUpWindow(); |
| 5712 | } |
| 5713 | |
| 5714 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5715 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5716 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5717 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5718 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5719 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5720 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5721 | mWindow->consumeFocusEvent(true); |
| 5722 | } |
| 5723 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5724 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5725 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5726 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5727 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5728 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5729 | |
| 5730 | // Window should receive key down event. |
| 5731 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5732 | } |
| 5733 | |
| 5734 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 5735 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 5736 | InputEvent* repeatEvent = mWindow->consume(); |
| 5737 | ASSERT_NE(nullptr, repeatEvent); |
| 5738 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5739 | ASSERT_EQ(InputEventType::KEY, repeatEvent->getType()); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5740 | |
| 5741 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 5742 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 5743 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 5744 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 5745 | } |
| 5746 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5747 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5748 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5749 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5750 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5751 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5752 | |
| 5753 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5754 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5755 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5756 | } |
| 5757 | }; |
| 5758 | |
| 5759 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5760 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5761 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5762 | expectKeyRepeatOnce(repeatCount); |
| 5763 | } |
| 5764 | } |
| 5765 | |
| 5766 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5767 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5768 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5769 | expectKeyRepeatOnce(repeatCount); |
| 5770 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5771 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5772 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5773 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5774 | expectKeyRepeatOnce(repeatCount); |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5779 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5780 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5781 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5782 | mWindow->assertNoEvents(); |
| 5783 | } |
| 5784 | |
| 5785 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5786 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5787 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5788 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5789 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5790 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5791 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5792 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5793 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 5794 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5795 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5796 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5797 | mWindow->assertNoEvents(); |
| 5798 | } |
| 5799 | |
| 5800 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5801 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5802 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5803 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5804 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5805 | // 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] | 5806 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5807 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5808 | mWindow->assertNoEvents(); |
| 5809 | } |
| 5810 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5811 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 5812 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5813 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5814 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5815 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 5816 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 5817 | mWindow->assertNoEvents(); |
| 5818 | } |
| 5819 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5820 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5821 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5822 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5823 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5824 | InputEvent* repeatEvent = mWindow->consume(); |
| 5825 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5826 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 5827 | IdGenerator::getSource(repeatEvent->getId())); |
| 5828 | } |
| 5829 | } |
| 5830 | |
| 5831 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5832 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5833 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5834 | |
| 5835 | std::unordered_set<int32_t> idSet; |
| 5836 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5837 | InputEvent* repeatEvent = mWindow->consume(); |
| 5838 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5839 | int32_t id = repeatEvent->getId(); |
| 5840 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 5841 | idSet.insert(id); |
| 5842 | } |
| 5843 | } |
| 5844 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5845 | /* Test InputDispatcher for MultiDisplay */ |
| 5846 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 5847 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5848 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5849 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5850 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5851 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5852 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5853 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5854 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5855 | // Set focus window for primary display, but focused display would be second one. |
| 5856 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5857 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5858 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5859 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5860 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5861 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5862 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5863 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5864 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5865 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5866 | // Set focus display to second one. |
| 5867 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 5868 | // Set focus window for second display. |
| 5869 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5870 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5871 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5872 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5873 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5874 | } |
| 5875 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5876 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5877 | InputDispatcherTest::TearDown(); |
| 5878 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5879 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5880 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5881 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5882 | windowInSecondary.clear(); |
| 5883 | } |
| 5884 | |
| 5885 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5886 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5887 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5888 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5889 | sp<FakeWindowHandle> windowInSecondary; |
| 5890 | }; |
| 5891 | |
| 5892 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 5893 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5895 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5896 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5897 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5898 | windowInSecondary->assertNoEvents(); |
| 5899 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5900 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5902 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5903 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5904 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5905 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5906 | } |
| 5907 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5908 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5909 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5911 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5912 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5913 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5914 | windowInSecondary->assertNoEvents(); |
| 5915 | |
| 5916 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5918 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5919 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5920 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5921 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5922 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5923 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5924 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5925 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5926 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5927 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5928 | |
| 5929 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5930 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5931 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5932 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5933 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5934 | windowInSecondary->assertNoEvents(); |
| 5935 | } |
| 5936 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5937 | // Test per-display input monitors for motion event. |
| 5938 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5939 | FakeMonitorReceiver monitorInPrimary = |
| 5940 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5941 | FakeMonitorReceiver monitorInSecondary = |
| 5942 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5943 | |
| 5944 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5945 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5946 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5947 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5948 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5949 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5950 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5951 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5952 | |
| 5953 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5954 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5955 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5956 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5957 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5958 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5959 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5960 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5961 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 5962 | // Lift up the touch from the second display |
| 5963 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5964 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5965 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5966 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5967 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 5968 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5969 | // Test inject a non-pointer motion event. |
| 5970 | // If specific a display, it will dispatch to the focused window of particular display, |
| 5971 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5972 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5973 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 5974 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5975 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5976 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5977 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5978 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5979 | } |
| 5980 | |
| 5981 | // Test per-display input monitors for key event. |
| 5982 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5983 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5984 | FakeMonitorReceiver monitorInPrimary = |
| 5985 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5986 | FakeMonitorReceiver monitorInSecondary = |
| 5987 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5988 | |
| 5989 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5990 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5991 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5992 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5993 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5994 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5995 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5996 | } |
| 5997 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5998 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 5999 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6000 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6001 | secondWindowInPrimary->setFocusable(true); |
| 6002 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 6003 | setFocusedWindow(secondWindowInPrimary); |
| 6004 | windowInPrimary->consumeFocusEvent(false); |
| 6005 | secondWindowInPrimary->consumeFocusEvent(true); |
| 6006 | |
| 6007 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6008 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 6009 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6010 | windowInPrimary->assertNoEvents(); |
| 6011 | windowInSecondary->assertNoEvents(); |
| 6012 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6013 | } |
| 6014 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6015 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 6016 | FakeMonitorReceiver monitorInPrimary = |
| 6017 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6018 | FakeMonitorReceiver monitorInSecondary = |
| 6019 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6020 | |
| 6021 | // Test touch down on primary display. |
| 6022 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6023 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6024 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6025 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6026 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6027 | |
| 6028 | // Test touch down on second display. |
| 6029 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6030 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 6031 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6032 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 6033 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 6034 | |
| 6035 | // Trigger cancel touch. |
| 6036 | mDispatcher->cancelCurrentTouch(); |
| 6037 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6038 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6039 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6040 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6041 | |
| 6042 | // Test inject a move motion event, no window/monitor should receive the event. |
| 6043 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6044 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6045 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6046 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 6047 | windowInPrimary->assertNoEvents(); |
| 6048 | monitorInPrimary.assertNoEvents(); |
| 6049 | |
| 6050 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6051 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6052 | SECOND_DISPLAY_ID, {110, 200})) |
| 6053 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 6054 | windowInSecondary->assertNoEvents(); |
| 6055 | monitorInSecondary.assertNoEvents(); |
| 6056 | } |
| 6057 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6058 | class InputFilterTest : public InputDispatcherTest { |
| 6059 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6060 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 6061 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6062 | NotifyMotionArgs motionArgs; |
| 6063 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6064 | motionArgs = |
| 6065 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6066 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6067 | motionArgs = |
| 6068 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6069 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6070 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6071 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6072 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 6073 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6074 | } else { |
| 6075 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 6076 | } |
| 6077 | } |
| 6078 | |
| 6079 | void testNotifyKey(bool expectToBeFiltered) { |
| 6080 | NotifyKeyArgs keyArgs; |
| 6081 | |
| 6082 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6083 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6084 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6085 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6086 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6087 | |
| 6088 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 6089 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6090 | } else { |
| 6091 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 6092 | } |
| 6093 | } |
| 6094 | }; |
| 6095 | |
| 6096 | // Test InputFilter for MotionEvent |
| 6097 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 6098 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 6099 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 6100 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 6101 | |
| 6102 | // Enable InputFilter |
| 6103 | mDispatcher->setInputFilterEnabled(true); |
| 6104 | // Test touch on both primary and second display, and check if both events are filtered. |
| 6105 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 6106 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 6107 | |
| 6108 | // Disable InputFilter |
| 6109 | mDispatcher->setInputFilterEnabled(false); |
| 6110 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 6111 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 6112 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 6113 | } |
| 6114 | |
| 6115 | // Test InputFilter for KeyEvent |
| 6116 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 6117 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 6118 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 6119 | |
| 6120 | // Enable InputFilter |
| 6121 | mDispatcher->setInputFilterEnabled(true); |
| 6122 | // Send a key event, and check if it is filtered. |
| 6123 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 6124 | |
| 6125 | // Disable InputFilter |
| 6126 | mDispatcher->setInputFilterEnabled(false); |
| 6127 | // Send a key event, and check if it isn't filtered. |
| 6128 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 6129 | } |
| 6130 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6131 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 6132 | // logical display coordinate space. |
| 6133 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 6134 | ui::Transform firstDisplayTransform; |
| 6135 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6136 | ui::Transform secondDisplayTransform; |
| 6137 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 6138 | |
| 6139 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 6140 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 6141 | displayInfos[0].transform = firstDisplayTransform; |
| 6142 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 6143 | displayInfos[1].transform = secondDisplayTransform; |
| 6144 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6145 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6146 | |
| 6147 | // Enable InputFilter |
| 6148 | mDispatcher->setInputFilterEnabled(true); |
| 6149 | |
| 6150 | // Ensure the correct transforms are used for the displays. |
| 6151 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 6152 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 6153 | } |
| 6154 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6155 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 6156 | protected: |
| 6157 | virtual void SetUp() override { |
| 6158 | InputDispatcherTest::SetUp(); |
| 6159 | |
| 6160 | /** |
| 6161 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 6162 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 6163 | * on. |
| 6164 | */ |
| 6165 | mDispatcher->setInputFilterEnabled(true); |
| 6166 | |
| 6167 | std::shared_ptr<InputApplicationHandle> application = |
| 6168 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6169 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 6170 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6171 | |
| 6172 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6173 | mWindow->setFocusable(true); |
| 6174 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6175 | setFocusedWindow(mWindow); |
| 6176 | mWindow->consumeFocusEvent(true); |
| 6177 | } |
| 6178 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6179 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 6180 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6181 | KeyEvent event; |
| 6182 | |
| 6183 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6184 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 6185 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6186 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6187 | const int32_t additionalPolicyFlags = |
| 6188 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 6189 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6190 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6191 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 6192 | policyFlags | additionalPolicyFlags)); |
| 6193 | |
| 6194 | InputEvent* received = mWindow->consume(); |
| 6195 | ASSERT_NE(nullptr, received); |
| 6196 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6197 | ASSERT_EQ(received->getType(), InputEventType::KEY); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6198 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 6199 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 6200 | } |
| 6201 | |
| 6202 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 6203 | int32_t flags) { |
| 6204 | MotionEvent event; |
| 6205 | PointerProperties pointerProperties[1]; |
| 6206 | PointerCoords pointerCoords[1]; |
| 6207 | pointerProperties[0].clear(); |
| 6208 | pointerProperties[0].id = 0; |
| 6209 | pointerCoords[0].clear(); |
| 6210 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 6211 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 6212 | |
| 6213 | ui::Transform identityTransform; |
| 6214 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6215 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 6216 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 6217 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 6218 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 6219 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 6220 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6221 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 6222 | |
| 6223 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 6224 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6225 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6226 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 6227 | policyFlags | additionalPolicyFlags)); |
| 6228 | |
| 6229 | InputEvent* received = mWindow->consume(); |
| 6230 | ASSERT_NE(nullptr, received); |
| 6231 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6232 | ASSERT_EQ(received->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6233 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 6234 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | private: |
| 6238 | sp<FakeWindowHandle> mWindow; |
| 6239 | }; |
| 6240 | |
| 6241 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6242 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 6243 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 6244 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6245 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 6246 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6249 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6250 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6251 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6252 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 6253 | } |
| 6254 | |
| 6255 | TEST_F(InputFilterInjectionPolicyTest, |
| 6256 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 6257 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6258 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6259 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6260 | } |
| 6261 | |
| 6262 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6263 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 6264 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6265 | } |
| 6266 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6267 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6268 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6269 | InputDispatcherTest::SetUp(); |
| 6270 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6271 | std::shared_ptr<FakeApplicationHandle> application = |
| 6272 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6273 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6274 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6275 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6276 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6277 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6278 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6279 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6280 | |
| 6281 | // Set focused application. |
| 6282 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6283 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6284 | |
| 6285 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6286 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6287 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6288 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6289 | } |
| 6290 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6291 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6292 | InputDispatcherTest::TearDown(); |
| 6293 | |
| 6294 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6295 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6296 | } |
| 6297 | |
| 6298 | protected: |
| 6299 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6300 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6301 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6302 | }; |
| 6303 | |
| 6304 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6305 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 6306 | // the onPointerDownOutsideFocus callback. |
| 6307 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6308 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6309 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6310 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6311 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6312 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6313 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6314 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6315 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 6316 | } |
| 6317 | |
| 6318 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 6319 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 6320 | // onPointerDownOutsideFocus callback. |
| 6321 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6322 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6323 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6324 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6325 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6326 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6327 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6328 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6329 | } |
| 6330 | |
| 6331 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 6332 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 6333 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6334 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6335 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6336 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6337 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6338 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6339 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6340 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6341 | } |
| 6342 | |
| 6343 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6344 | // DOWN on the window that already has focus. Ensure no window received the |
| 6345 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6346 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6347 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6348 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6349 | FOCUSED_WINDOW_TOUCH_POINT)) |
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 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6357 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 6358 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 6359 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 6360 | const MotionEvent event = |
| 6361 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6362 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 6363 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6364 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 6365 | .build(); |
| 6366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 6367 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6368 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6369 | |
| 6370 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6371 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 6372 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 6373 | mUnfocusedWindow->assertNoEvents(); |
| 6374 | } |
| 6375 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6376 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 6377 | // windows that point to the same client token. |
| 6378 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 6379 | virtual void SetUp() override { |
| 6380 | InputDispatcherTest::SetUp(); |
| 6381 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6382 | std::shared_ptr<FakeApplicationHandle> application = |
| 6383 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6384 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 6385 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6386 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 6387 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6388 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 6389 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6390 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 6391 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6392 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6393 | } |
| 6394 | |
| 6395 | protected: |
| 6396 | sp<FakeWindowHandle> mWindow1; |
| 6397 | sp<FakeWindowHandle> mWindow2; |
| 6398 | |
| 6399 | // 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] | 6400 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 6401 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 6402 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6403 | } |
| 6404 | |
| 6405 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 6406 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6407 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6408 | InputEvent* event = window->consume(); |
| 6409 | |
| 6410 | ASSERT_NE(nullptr, event) << name.c_str() |
| 6411 | << ": consumer should have returned non-NULL event."; |
| 6412 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6413 | ASSERT_EQ(InputEventType::MOTION, event->getType()) |
| 6414 | << name.c_str() << ": expected MotionEvent, got " << *event; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6415 | |
| 6416 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6417 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 6418 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6419 | |
| 6420 | for (size_t i = 0; i < points.size(); i++) { |
| 6421 | float expectedX = points[i].x; |
| 6422 | float expectedY = points[i].y; |
| 6423 | |
| 6424 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 6425 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 6426 | << ", got " << motionEvent.getX(i); |
| 6427 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 6428 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 6429 | << ", got " << motionEvent.getY(i); |
| 6430 | } |
| 6431 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6432 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6433 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6434 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6435 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 6436 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6437 | |
| 6438 | // Always consume from window1 since it's the window that has the InputReceiver |
| 6439 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 6440 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6441 | }; |
| 6442 | |
| 6443 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 6444 | // Touch Window 1 |
| 6445 | PointF touchedPoint = {10, 10}; |
| 6446 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6447 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6448 | |
| 6449 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6450 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6451 | |
| 6452 | // Touch Window 2 |
| 6453 | touchedPoint = {150, 150}; |
| 6454 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6455 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6456 | } |
| 6457 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6458 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 6459 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6460 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6461 | |
| 6462 | // Touch Window 1 |
| 6463 | PointF touchedPoint = {10, 10}; |
| 6464 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6465 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6466 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6467 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6468 | |
| 6469 | // Touch Window 2 |
| 6470 | touchedPoint = {150, 150}; |
| 6471 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6472 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 6473 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6474 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6475 | // Update the transform so rotation is set |
| 6476 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6477 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 6478 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6479 | } |
| 6480 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6481 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6482 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6483 | |
| 6484 | // Touch Window 1 |
| 6485 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6486 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6487 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6488 | |
| 6489 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6490 | touchedPoints.push_back(PointF{150, 150}); |
| 6491 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6492 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6493 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6494 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6495 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6496 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6497 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6498 | // Update the transform so rotation is set for Window 2 |
| 6499 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6500 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6501 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6502 | } |
| 6503 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6504 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6505 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6506 | |
| 6507 | // Touch Window 1 |
| 6508 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6509 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6510 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6511 | |
| 6512 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6513 | touchedPoints.push_back(PointF{150, 150}); |
| 6514 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6515 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6516 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6517 | |
| 6518 | // Move both windows |
| 6519 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6520 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6521 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6522 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6523 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6524 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6525 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6526 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6527 | expectedPoints.pop_back(); |
| 6528 | |
| 6529 | // Touch Window 2 |
| 6530 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6531 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6532 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6533 | |
| 6534 | // Move both windows |
| 6535 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6536 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6537 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6538 | |
| 6539 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
| 6542 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 6543 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 6544 | |
| 6545 | // Touch Window 1 |
| 6546 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6547 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6548 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6549 | |
| 6550 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6551 | touchedPoints.push_back(PointF{150, 150}); |
| 6552 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6553 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6554 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6555 | |
| 6556 | // Move both windows |
| 6557 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6558 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6559 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6560 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6561 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6562 | } |
| 6563 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 6564 | /** |
| 6565 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 6566 | * same input channel. |
| 6567 | */ |
| 6568 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 6569 | mWindow1->setSlippery(true); |
| 6570 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6571 | |
| 6572 | // Touch down in window 1 |
| 6573 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6574 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6575 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 6576 | |
| 6577 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 6578 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 6579 | // getting generated. |
| 6580 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6581 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6582 | |
| 6583 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 6584 | } |
| 6585 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 6586 | /** |
| 6587 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 6588 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 6589 | * that the pointer is hovering over may have a different transform. |
| 6590 | */ |
| 6591 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
| 6592 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6593 | |
| 6594 | // Start hover in window 1 |
| 6595 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN, |
| 6596 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6597 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 6598 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
| 6599 | |
| 6600 | // Move hover to window 2. |
| 6601 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6602 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6603 | |
| 6604 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 6605 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 6606 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 6607 | } |
| 6608 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6609 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 6610 | virtual void SetUp() override { |
| 6611 | InputDispatcherTest::SetUp(); |
| 6612 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6613 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6614 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6615 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 6616 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6617 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6618 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6619 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6620 | |
| 6621 | // Set focused application. |
| 6622 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 6623 | |
| 6624 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6625 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6626 | mWindow->consumeFocusEvent(true); |
| 6627 | } |
| 6628 | |
| 6629 | virtual void TearDown() override { |
| 6630 | InputDispatcherTest::TearDown(); |
| 6631 | mWindow.clear(); |
| 6632 | } |
| 6633 | |
| 6634 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6635 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6636 | sp<FakeWindowHandle> mWindow; |
| 6637 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 6638 | |
| 6639 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6640 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6641 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6642 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6644 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6645 | WINDOW_LOCATION)); |
| 6646 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6647 | |
| 6648 | sp<FakeWindowHandle> addSpyWindow() { |
| 6649 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6650 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6651 | spy->setTrustedOverlay(true); |
| 6652 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6653 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6654 | spy->setDispatchingTimeout(30ms); |
| 6655 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 6656 | return spy; |
| 6657 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6658 | }; |
| 6659 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6660 | // Send a tap and respond, which should not cause an ANR. |
| 6661 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 6662 | tapOnWindow(); |
| 6663 | mWindow->consumeMotionDown(); |
| 6664 | mWindow->consumeMotionUp(); |
| 6665 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6666 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6667 | } |
| 6668 | |
| 6669 | // Send a regular key and respond, which should not cause an ANR. |
| 6670 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6671 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6672 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6673 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6674 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6675 | } |
| 6676 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6677 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 6678 | mWindow->setFocusable(false); |
| 6679 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6680 | mWindow->consumeFocusEvent(false); |
| 6681 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6682 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6683 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6684 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6685 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6686 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6687 | // Key will not go to window because we have no focused window. |
| 6688 | // The 'no focused window' ANR timer should start instead. |
| 6689 | |
| 6690 | // Now, the focused application goes away. |
| 6691 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 6692 | // The key should get dropped and there should be no ANR. |
| 6693 | |
| 6694 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6695 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6696 | } |
| 6697 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6698 | // 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] | 6699 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 6700 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6701 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6702 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6703 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6704 | WINDOW_LOCATION)); |
| 6705 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6706 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 6707 | ASSERT_TRUE(sequenceNum); |
| 6708 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6709 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6710 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6711 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6712 | mWindow->consumeMotionEvent( |
| 6713 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6714 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6715 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6716 | } |
| 6717 | |
| 6718 | // Send a key to the app and have the app not respond right away. |
| 6719 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 6720 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6722 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 6723 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6724 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6725 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6726 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6727 | } |
| 6728 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6729 | // We have a focused application, but no focused window |
| 6730 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6731 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6732 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6733 | mWindow->consumeFocusEvent(false); |
| 6734 | |
| 6735 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6736 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6737 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6738 | WINDOW_LOCATION)); |
| 6739 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 6740 | mDispatcher->waitForIdle(); |
| 6741 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6742 | |
| 6743 | // Once a focused event arrives, we get an ANR for this application |
| 6744 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6745 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6746 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6747 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6748 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6749 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6750 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6751 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6752 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6753 | } |
| 6754 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6755 | /** |
| 6756 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 6757 | * there will not be an ANR. |
| 6758 | */ |
| 6759 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 6760 | mWindow->setFocusable(false); |
| 6761 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6762 | mWindow->consumeFocusEvent(false); |
| 6763 | |
| 6764 | KeyEvent event; |
| 6765 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 6766 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 6767 | |
| 6768 | // Define a valid key down event that is stale (too old). |
| 6769 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 6770 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6771 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6772 | |
| 6773 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 6774 | |
| 6775 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6776 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6777 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 6778 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 6779 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 6780 | << "Injection should fail because the event is stale"; |
| 6781 | |
| 6782 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6783 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6784 | mWindow->assertNoEvents(); |
| 6785 | } |
| 6786 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6787 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6788 | // Make sure that we don't notify policy twice about the same ANR. |
| 6789 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6790 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6791 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6792 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6793 | |
| 6794 | // Once a focused event arrives, we get an ANR for this application |
| 6795 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6796 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6797 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6798 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6799 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6800 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6801 | const std::chrono::duration appTimeout = |
| 6802 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6803 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6804 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6805 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6806 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 6807 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6808 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6809 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 6810 | // '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] | 6811 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6812 | } |
| 6813 | |
| 6814 | // We have a focused application, but no focused window |
| 6815 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
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); |
| 6819 | |
| 6820 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6821 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6822 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6823 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6824 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6825 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6826 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6827 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6828 | |
| 6829 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6830 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6831 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6832 | mWindow->assertNoEvents(); |
| 6833 | } |
| 6834 | |
| 6835 | /** |
| 6836 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 6837 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 6838 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 6839 | * the ANR mechanism should still work. |
| 6840 | * |
| 6841 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 6842 | * DOWN event, while not responding on the second one. |
| 6843 | */ |
| 6844 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 6845 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6846 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6847 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6848 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6849 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6850 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6851 | |
| 6852 | // Now send ACTION_UP, with identical timestamp |
| 6853 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6854 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6855 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6856 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6857 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6858 | |
| 6859 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 6860 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6861 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6862 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6863 | } |
| 6864 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6865 | // A spy window can receive an ANR |
| 6866 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 6867 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 6868 | |
| 6869 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6870 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6871 | WINDOW_LOCATION)); |
| 6872 | mWindow->consumeMotionDown(); |
| 6873 | |
| 6874 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 6875 | ASSERT_TRUE(sequenceNum); |
| 6876 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6877 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6878 | |
| 6879 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6880 | spy->consumeMotionEvent( |
| 6881 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6882 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6883 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6884 | } |
| 6885 | |
| 6886 | // 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] | 6887 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6888 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 6889 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6890 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6892 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6893 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6895 | |
| 6896 | // Stuck on the ACTION_UP |
| 6897 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6898 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6899 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6900 | // 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] | 6901 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6902 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6903 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6904 | |
| 6905 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6906 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6907 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6908 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6909 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6910 | } |
| 6911 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6912 | // 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] | 6913 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6914 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 6915 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6916 | |
| 6917 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6918 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6919 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6920 | |
| 6921 | mWindow->consumeMotionDown(); |
| 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->consumeMotionUp(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(); |
| 6936 | } |
| 6937 | |
| 6938 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 6939 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 6940 | |
| 6941 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6942 | |
| 6943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6944 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6945 | WINDOW_LOCATION)); |
| 6946 | |
| 6947 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6948 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 6949 | ASSERT_TRUE(consumeSeq); |
| 6950 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6951 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6952 | |
| 6953 | monitor.finishEvent(*consumeSeq); |
| 6954 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6955 | |
| 6956 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6957 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6958 | } |
| 6959 | |
| 6960 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 6961 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 6962 | // get an ANR again. |
| 6963 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 6964 | // 2. consume all pending events (= queue becomes healthy again) |
| 6965 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 6966 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 6967 | tapOnWindow(); |
| 6968 | |
| 6969 | mWindow->consumeMotionDown(); |
| 6970 | // Block on ACTION_UP |
| 6971 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6972 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6973 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 6974 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6975 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6976 | mWindow->assertNoEvents(); |
| 6977 | |
| 6978 | tapOnWindow(); |
| 6979 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6980 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6981 | mWindow->consumeMotionUp(); |
| 6982 | |
| 6983 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6984 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6985 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6986 | mWindow->assertNoEvents(); |
| 6987 | } |
| 6988 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6989 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 6990 | // it. |
| 6991 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6992 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6993 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6994 | WINDOW_LOCATION)); |
| 6995 | |
| 6996 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6997 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6998 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6999 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 7000 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7001 | // 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] | 7002 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 7003 | mWindow->consumeMotionEvent( |
| 7004 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7005 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7006 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7007 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7008 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7009 | } |
| 7010 | |
| 7011 | /** |
| 7012 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7013 | * not to to the focused window until the motion is processed. |
| 7014 | * |
| 7015 | * Warning!!! |
| 7016 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7017 | * and the injection timeout that we specify when injecting the key. |
| 7018 | * We must have the injection timeout (10ms) be smaller than |
| 7019 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7020 | * |
| 7021 | * If that value changes, this test should also change. |
| 7022 | */ |
| 7023 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 7024 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 7025 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7026 | |
| 7027 | tapOnWindow(); |
| 7028 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 7029 | ASSERT_TRUE(downSequenceNum); |
| 7030 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 7031 | ASSERT_TRUE(upSequenceNum); |
| 7032 | // Don't finish the events yet, and send a key |
| 7033 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 7034 | // window even if motions are still being processed. But because the injection timeout is short, |
| 7035 | // we will receive INJECTION_TIMED_OUT as the result. |
| 7036 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7037 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7038 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7039 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 7040 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7041 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7042 | // and the key remains pending, waiting for the touch events to be processed |
| 7043 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 7044 | ASSERT_FALSE(keySequenceNum); |
| 7045 | |
| 7046 | std::this_thread::sleep_for(500ms); |
| 7047 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 7048 | // to the focused window, even though we have not yet finished the motion event |
| 7049 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7050 | mWindow->finishEvent(*downSequenceNum); |
| 7051 | mWindow->finishEvent(*upSequenceNum); |
| 7052 | } |
| 7053 | |
| 7054 | /** |
| 7055 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7056 | * not go to the focused window until the motion is processed. |
| 7057 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 7058 | * focused window right away. |
| 7059 | */ |
| 7060 | TEST_F(InputDispatcherSingleWindowAnr, |
| 7061 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 7062 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 7063 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7064 | |
| 7065 | tapOnWindow(); |
| 7066 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 7067 | ASSERT_TRUE(downSequenceNum); |
| 7068 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 7069 | ASSERT_TRUE(upSequenceNum); |
| 7070 | // Don't finish the events yet, and send a key |
| 7071 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7073 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7074 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7075 | // At this point, key is still pending, and should not be sent to the application yet. |
| 7076 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 7077 | ASSERT_FALSE(keySequenceNum); |
| 7078 | |
| 7079 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 7080 | tapOnWindow(); |
| 7081 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 7082 | // the other events yet. We can finish events in any order. |
| 7083 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 7084 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 7085 | mWindow->consumeMotionDown(); |
| 7086 | mWindow->consumeMotionUp(); |
| 7087 | mWindow->assertNoEvents(); |
| 7088 | } |
| 7089 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 7090 | /** |
| 7091 | * Send an event to the app and have the app not respond right away. |
| 7092 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 7093 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 7094 | * At some point, the window becomes responsive again. |
| 7095 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 7096 | */ |
| 7097 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 7098 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7099 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 7100 | .build()); |
| 7101 | |
| 7102 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 7103 | ASSERT_TRUE(sequenceNum); |
| 7104 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 7105 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 7106 | |
| 7107 | mWindow->finishEvent(*sequenceNum); |
| 7108 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 7109 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7110 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 7111 | |
| 7112 | // Now that the window is responsive, let's continue the gesture. |
| 7113 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7114 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7115 | .build()); |
| 7116 | |
| 7117 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7118 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7119 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 7120 | .build()); |
| 7121 | |
| 7122 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7123 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7124 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 7125 | .build()); |
| 7126 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7127 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7128 | .build()); |
| 7129 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 7130 | mWindow->assertNoEvents(); |
| 7131 | |
| 7132 | // Start another one. |
| 7133 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7134 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 7135 | .build()); |
| 7136 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7137 | } |
| 7138 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7139 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 7140 | virtual void SetUp() override { |
| 7141 | InputDispatcherTest::SetUp(); |
| 7142 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7143 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7144 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7145 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 7146 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7147 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7148 | // 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] | 7149 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7150 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7151 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 7152 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 7153 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7154 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7155 | |
| 7156 | // Set focused application. |
| 7157 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7158 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7159 | |
| 7160 | // Expect one focus window exist in display. |
| 7161 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7162 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7163 | mFocusedWindow->consumeFocusEvent(true); |
| 7164 | } |
| 7165 | |
| 7166 | virtual void TearDown() override { |
| 7167 | InputDispatcherTest::TearDown(); |
| 7168 | |
| 7169 | mUnfocusedWindow.clear(); |
| 7170 | mFocusedWindow.clear(); |
| 7171 | } |
| 7172 | |
| 7173 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7174 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7175 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 7176 | sp<FakeWindowHandle> mFocusedWindow; |
| 7177 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 7178 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 7179 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 7180 | |
| 7181 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 7182 | |
| 7183 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 7184 | |
| 7185 | private: |
| 7186 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7188 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7189 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7190 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7191 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7192 | location)); |
| 7193 | } |
| 7194 | }; |
| 7195 | |
| 7196 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 7197 | // should be ANR'd first. |
| 7198 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7199 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7200 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7201 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7202 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7203 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7204 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7205 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7206 | // We consumed all events, so no ANR |
| 7207 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7208 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7209 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7210 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7211 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7212 | FOCUSED_WINDOW_LOCATION)); |
| 7213 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7214 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7215 | |
| 7216 | const std::chrono::duration timeout = |
| 7217 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7218 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7219 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 7220 | // sequence to make it consistent |
| 7221 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7222 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7223 | mFocusedWindow->consumeMotionDown(); |
| 7224 | // This cancel is generated because the connection was unresponsive |
| 7225 | mFocusedWindow->consumeMotionCancel(); |
| 7226 | mFocusedWindow->assertNoEvents(); |
| 7227 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7228 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7229 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7230 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7231 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7232 | } |
| 7233 | |
| 7234 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 7235 | // it doesn't matter which order they should have ANR. |
| 7236 | // But we should receive ANR for both. |
| 7237 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 7238 | // Set the timeout for unfocused window to match the focused window |
| 7239 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 7240 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7241 | |
| 7242 | tapOnFocusedWindow(); |
| 7243 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7244 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 7245 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 7246 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7247 | |
| 7248 | // We don't know which window will ANR first. But both of them should happen eventually. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7249 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 7250 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 7251 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 7252 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7253 | |
| 7254 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7255 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7256 | |
| 7257 | mFocusedWindow->consumeMotionDown(); |
| 7258 | mFocusedWindow->consumeMotionUp(); |
| 7259 | mUnfocusedWindow->consumeMotionOutside(); |
| 7260 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7261 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 7262 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 7263 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7264 | |
| 7265 | // Both applications should be marked as responsive, in any order |
| 7266 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 7267 | mFocusedWindow->getToken() == responsiveToken2); |
| 7268 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 7269 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 7270 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7271 | } |
| 7272 | |
| 7273 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 7274 | // We should also log an error to account for the dropped event (not tested here). |
| 7275 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 7276 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 7277 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7278 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7279 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7280 | // Receive the events, but don't respond |
| 7281 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 7282 | ASSERT_TRUE(downEventSequenceNum); |
| 7283 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 7284 | ASSERT_TRUE(upEventSequenceNum); |
| 7285 | const std::chrono::duration timeout = |
| 7286 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7287 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7288 | |
| 7289 | // Tap once again |
| 7290 | // 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] | 7291 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7292 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7293 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7294 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7295 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7296 | FOCUSED_WINDOW_LOCATION)); |
| 7297 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 7298 | // valid touch target |
| 7299 | mUnfocusedWindow->assertNoEvents(); |
| 7300 | |
| 7301 | // Consume the first tap |
| 7302 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 7303 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 7304 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7305 | // The second tap did not go to the focused window |
| 7306 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7307 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7308 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7309 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7310 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7311 | } |
| 7312 | |
| 7313 | // If you tap outside of all windows, there will not be ANR |
| 7314 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7315 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7316 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7317 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 7318 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7319 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7320 | } |
| 7321 | |
| 7322 | // Since the focused window is paused, tapping on it should not produce any events |
| 7323 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 7324 | mFocusedWindow->setPaused(true); |
| 7325 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7326 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7327 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7328 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7329 | FOCUSED_WINDOW_LOCATION)); |
| 7330 | |
| 7331 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 7332 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7333 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 7334 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7335 | |
| 7336 | mFocusedWindow->assertNoEvents(); |
| 7337 | mUnfocusedWindow->assertNoEvents(); |
| 7338 | } |
| 7339 | |
| 7340 | /** |
| 7341 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7342 | * not to to the focused window until the motion is processed. |
| 7343 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 7344 | * |
| 7345 | * Warning!!! |
| 7346 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7347 | * and the injection timeout that we specify when injecting the key. |
| 7348 | * We must have the injection timeout (10ms) be smaller than |
| 7349 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7350 | * |
| 7351 | * If that value changes, this test should also change. |
| 7352 | */ |
| 7353 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 7354 | // Set a long ANR timeout to prevent it from triggering |
| 7355 | mFocusedWindow->setDispatchingTimeout(2s); |
| 7356 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7357 | |
| 7358 | tapOnUnfocusedWindow(); |
| 7359 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7360 | ASSERT_TRUE(downSequenceNum); |
| 7361 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7362 | ASSERT_TRUE(upSequenceNum); |
| 7363 | // Don't finish the events yet, and send a key |
| 7364 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 7365 | // window even if motions are still being processed. |
| 7366 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7367 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7368 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7369 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7370 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7371 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7372 | // and the key remains pending, waiting for the touch events to be processed |
| 7373 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 7374 | ASSERT_FALSE(keySequenceNum); |
| 7375 | |
| 7376 | // 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] | 7377 | mFocusedWindow->setFocusable(false); |
| 7378 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7379 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7380 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7381 | |
| 7382 | // Focus events should precede the key events |
| 7383 | mUnfocusedWindow->consumeFocusEvent(true); |
| 7384 | mFocusedWindow->consumeFocusEvent(false); |
| 7385 | |
| 7386 | // Finish the tap events, which should unblock dispatcher |
| 7387 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 7388 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 7389 | |
| 7390 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 7391 | // can finally go to the newly focused "mUnfocusedWindow". |
| 7392 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7393 | mFocusedWindow->assertNoEvents(); |
| 7394 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7395 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7396 | } |
| 7397 | |
| 7398 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 7399 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 7400 | // The other window should not be affected by that. |
| 7401 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 7402 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7403 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7404 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7405 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7406 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7407 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7408 | |
| 7409 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7410 | mDispatcher->notifyMotion( |
| 7411 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7412 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7413 | |
| 7414 | const std::chrono::duration timeout = |
| 7415 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7416 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7417 | |
| 7418 | mUnfocusedWindow->consumeMotionDown(); |
| 7419 | mFocusedWindow->consumeMotionDown(); |
| 7420 | // Focused window may or may not receive ACTION_MOVE |
| 7421 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 7422 | InputEvent* event; |
| 7423 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 7424 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 7425 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 7426 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7427 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7428 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 7429 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 7430 | mFocusedWindow->consumeMotionCancel(); |
| 7431 | } else { |
| 7432 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 7433 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7434 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7435 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7436 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7437 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7438 | mUnfocusedWindow->assertNoEvents(); |
| 7439 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7440 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7441 | } |
| 7442 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7443 | /** |
| 7444 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 7445 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 7446 | * |
| 7447 | * If the user touches another application during this time, the key should be dropped. |
| 7448 | * Next, if a new focused window comes in, without toggling the focused application, |
| 7449 | * then no ANR should occur. |
| 7450 | * |
| 7451 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 7452 | * but in some cases the policy may not update the focused application. |
| 7453 | */ |
| 7454 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 7455 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 7456 | std::make_shared<FakeApplicationHandle>(); |
| 7457 | focusedApplication->setDispatchingTimeout(60ms); |
| 7458 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 7459 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 7460 | mFocusedWindow->setFocusable(false); |
| 7461 | |
| 7462 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7463 | mFocusedWindow->consumeFocusEvent(false); |
| 7464 | |
| 7465 | // Send a key. The ANR timer should start because there is no focused window. |
| 7466 | // 'focusedApplication' will get blamed if this timer completes. |
| 7467 | // 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] | 7468 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7469 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7470 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 7471 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7473 | |
| 7474 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 7475 | // then the injected touches won't cause the focused event to get dropped. |
| 7476 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 7477 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 7478 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 7479 | // For this test, it means that the key would get delivered to the window once it becomes |
| 7480 | // focused. |
| 7481 | std::this_thread::sleep_for(10ms); |
| 7482 | |
| 7483 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7484 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7485 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7486 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7487 | |
| 7488 | // We do not consume the motion right away, because that would require dispatcher to first |
| 7489 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 7490 | // Set the focused window first. |
| 7491 | mFocusedWindow->setFocusable(true); |
| 7492 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7493 | setFocusedWindow(mFocusedWindow); |
| 7494 | mFocusedWindow->consumeFocusEvent(true); |
| 7495 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 7496 | // to another application. This could be a bug / behaviour in the policy. |
| 7497 | |
| 7498 | mUnfocusedWindow->consumeMotionDown(); |
| 7499 | |
| 7500 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7501 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 7502 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 7503 | } |
| 7504 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7505 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 7506 | // that has feature NO_INPUT_CHANNEL. |
| 7507 | // Layout: |
| 7508 | // Top (closest to user) |
| 7509 | // mNoInputWindow (above all windows) |
| 7510 | // mBottomWindow |
| 7511 | // Bottom (furthest from user) |
| 7512 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 7513 | virtual void SetUp() override { |
| 7514 | InputDispatcherTest::SetUp(); |
| 7515 | |
| 7516 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7517 | mNoInputWindow = |
| 7518 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7519 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7520 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7521 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7522 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7523 | // It's perfectly valid for this window to not have an associated input channel |
| 7524 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7525 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 7526 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7527 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7528 | |
| 7529 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7530 | } |
| 7531 | |
| 7532 | protected: |
| 7533 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 7534 | sp<FakeWindowHandle> mNoInputWindow; |
| 7535 | sp<FakeWindowHandle> mBottomWindow; |
| 7536 | }; |
| 7537 | |
| 7538 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 7539 | PointF touchedPoint = {10, 10}; |
| 7540 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7541 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7542 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7543 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7544 | |
| 7545 | mNoInputWindow->assertNoEvents(); |
| 7546 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 7547 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 7548 | // and therefore should prevent mBottomWindow from receiving touches |
| 7549 | mBottomWindow->assertNoEvents(); |
| 7550 | } |
| 7551 | |
| 7552 | /** |
| 7553 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 7554 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 7555 | */ |
| 7556 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 7557 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7558 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7559 | "Window with input channel and NO_INPUT_CHANNEL", |
| 7560 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7561 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7562 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7563 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7564 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7565 | |
| 7566 | PointF touchedPoint = {10, 10}; |
| 7567 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7568 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7569 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7570 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7571 | |
| 7572 | mNoInputWindow->assertNoEvents(); |
| 7573 | mBottomWindow->assertNoEvents(); |
| 7574 | } |
| 7575 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7576 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7577 | protected: |
| 7578 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7579 | sp<FakeWindowHandle> mWindow; |
| 7580 | sp<FakeWindowHandle> mMirror; |
| 7581 | |
| 7582 | virtual void SetUp() override { |
| 7583 | InputDispatcherTest::SetUp(); |
| 7584 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7585 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7586 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7587 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7588 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7589 | mWindow->setFocusable(true); |
| 7590 | mMirror->setFocusable(true); |
| 7591 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7592 | } |
| 7593 | }; |
| 7594 | |
| 7595 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7596 | // Request focus on a mirrored window |
| 7597 | setFocusedWindow(mMirror); |
| 7598 | |
| 7599 | // window gets focused |
| 7600 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7601 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7602 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7603 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7604 | } |
| 7605 | |
| 7606 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7607 | // focusable. |
| 7608 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7609 | setFocusedWindow(mMirror); |
| 7610 | |
| 7611 | // window gets focused |
| 7612 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7613 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7614 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7615 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7617 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7618 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7619 | |
| 7620 | mMirror->setFocusable(false); |
| 7621 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7622 | |
| 7623 | // window loses focus since one of the windows associated with the token in not focusable |
| 7624 | mWindow->consumeFocusEvent(false); |
| 7625 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7626 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7627 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7628 | mWindow->assertNoEvents(); |
| 7629 | } |
| 7630 | |
| 7631 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7632 | // invisible. |
| 7633 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 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->setVisible(false); |
| 7646 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7647 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7648 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7649 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7650 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7652 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7653 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7654 | |
| 7655 | mWindow->setVisible(false); |
| 7656 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7657 | |
| 7658 | // window loses focus only after all windows associated with the token become invisible. |
| 7659 | mWindow->consumeFocusEvent(false); |
| 7660 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7661 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7662 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7663 | mWindow->assertNoEvents(); |
| 7664 | } |
| 7665 | |
| 7666 | // A focused & mirrored window remains focused until both windows are removed. |
| 7667 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7668 | setFocusedWindow(mMirror); |
| 7669 | |
| 7670 | // window gets focused |
| 7671 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7672 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7673 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7674 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7675 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7676 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7677 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7678 | |
| 7679 | // single window is removed but the window token remains focused |
| 7680 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7681 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7682 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7683 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7684 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7685 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7686 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7687 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7688 | |
| 7689 | // Both windows are removed |
| 7690 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7691 | mWindow->consumeFocusEvent(false); |
| 7692 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7693 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7694 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7695 | mWindow->assertNoEvents(); |
| 7696 | } |
| 7697 | |
| 7698 | // Focus request can be pending until one window becomes visible. |
| 7699 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7700 | // Request focus on an invisible mirror. |
| 7701 | mWindow->setVisible(false); |
| 7702 | mMirror->setVisible(false); |
| 7703 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7704 | setFocusedWindow(mMirror); |
| 7705 | |
| 7706 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7708 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7709 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7710 | |
| 7711 | mMirror->setVisible(true); |
| 7712 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7713 | |
| 7714 | // window gets focused |
| 7715 | mWindow->consumeFocusEvent(true); |
| 7716 | // window gets the pending key event |
| 7717 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7718 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7719 | |
| 7720 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7721 | protected: |
| 7722 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7723 | sp<FakeWindowHandle> mWindow; |
| 7724 | sp<FakeWindowHandle> mSecondWindow; |
| 7725 | |
| 7726 | void SetUp() override { |
| 7727 | InputDispatcherTest::SetUp(); |
| 7728 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7729 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7730 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7731 | mSecondWindow = |
| 7732 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7733 | mSecondWindow->setFocusable(true); |
| 7734 | |
| 7735 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7736 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7737 | |
| 7738 | setFocusedWindow(mWindow); |
| 7739 | mWindow->consumeFocusEvent(true); |
| 7740 | } |
| 7741 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7742 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7743 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7744 | } |
| 7745 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7746 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7747 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7748 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7749 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7750 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7751 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7752 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7753 | } |
| 7754 | }; |
| 7755 | |
| 7756 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7757 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7758 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7759 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7760 | mSecondWindow->assertNoEvents(); |
| 7761 | |
| 7762 | // Ensure that capture can be enabled from the focus window. |
| 7763 | requestAndVerifyPointerCapture(mWindow, true); |
| 7764 | |
| 7765 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7766 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7767 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7768 | |
| 7769 | // Ensure that capture can be disabled from the window with capture. |
| 7770 | requestAndVerifyPointerCapture(mWindow, false); |
| 7771 | } |
| 7772 | |
| 7773 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7774 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7775 | |
| 7776 | setFocusedWindow(mSecondWindow); |
| 7777 | |
| 7778 | // Ensure that the capture disabled event was sent first. |
| 7779 | mWindow->consumeCaptureEvent(false); |
| 7780 | mWindow->consumeFocusEvent(false); |
| 7781 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7782 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7783 | |
| 7784 | // 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] | 7785 | notifyPointerCaptureChanged({}); |
| 7786 | notifyPointerCaptureChanged(request); |
| 7787 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7788 | mWindow->assertNoEvents(); |
| 7789 | mSecondWindow->assertNoEvents(); |
| 7790 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7791 | } |
| 7792 | |
| 7793 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7794 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7795 | |
| 7796 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7797 | notifyPointerCaptureChanged({}); |
| 7798 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7799 | |
| 7800 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7801 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7802 | mWindow->consumeCaptureEvent(false); |
| 7803 | mWindow->assertNoEvents(); |
| 7804 | } |
| 7805 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7806 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7807 | requestAndVerifyPointerCapture(mWindow, true); |
| 7808 | |
| 7809 | // The first window loses focus. |
| 7810 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7811 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7812 | mWindow->consumeCaptureEvent(false); |
| 7813 | |
| 7814 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7815 | // arrives. |
| 7816 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7817 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7818 | |
| 7819 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7820 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7821 | |
| 7822 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7823 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7824 | |
| 7825 | mSecondWindow->consumeFocusEvent(true); |
| 7826 | mSecondWindow->consumeCaptureEvent(true); |
| 7827 | } |
| 7828 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7829 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7830 | // App repeatedly enables and disables capture. |
| 7831 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7832 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7833 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7834 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7835 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7836 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7837 | |
| 7838 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7839 | // first request is now stale, this should do nothing. |
| 7840 | notifyPointerCaptureChanged(firstRequest); |
| 7841 | mWindow->assertNoEvents(); |
| 7842 | |
| 7843 | // InputReader notifies that the second request was enabled. |
| 7844 | notifyPointerCaptureChanged(secondRequest); |
| 7845 | mWindow->consumeCaptureEvent(true); |
| 7846 | } |
| 7847 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7848 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7849 | requestAndVerifyPointerCapture(mWindow, true); |
| 7850 | |
| 7851 | // App toggles pointer capture off and on. |
| 7852 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7853 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7854 | |
| 7855 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7856 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7857 | |
| 7858 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7859 | // preceding "disable" request. |
| 7860 | notifyPointerCaptureChanged(enableRequest); |
| 7861 | |
| 7862 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7863 | // any notifications. |
| 7864 | mWindow->assertNoEvents(); |
| 7865 | } |
| 7866 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7867 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7868 | protected: |
| 7869 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7870 | |
| 7871 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7872 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7873 | |
| 7874 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7875 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7876 | |
| 7877 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7878 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7879 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7880 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7881 | MAXIMUM_OBSCURING_OPACITY); |
| 7882 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 7883 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 7884 | static constexpr gui::Uid APP_B_UID{10002}; |
| 7885 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7886 | |
| 7887 | sp<FakeWindowHandle> mTouchWindow; |
| 7888 | |
| 7889 | virtual void SetUp() override { |
| 7890 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7891 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7892 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7893 | } |
| 7894 | |
| 7895 | virtual void TearDown() override { |
| 7896 | InputDispatcherTest::TearDown(); |
| 7897 | mTouchWindow.clear(); |
| 7898 | } |
| 7899 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 7900 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7901 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7902 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7903 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7904 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7905 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7906 | return window; |
| 7907 | } |
| 7908 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 7909 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7910 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7911 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7912 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7913 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 7914 | window->setOwnerInfo(1777 + (uid.val() % 10000), uid); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7915 | return window; |
| 7916 | } |
| 7917 | |
| 7918 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7919 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7920 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7921 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7922 | } |
| 7923 | }; |
| 7924 | |
| 7925 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7926 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7927 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7928 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7929 | |
| 7930 | touch(); |
| 7931 | |
| 7932 | mTouchWindow->assertNoEvents(); |
| 7933 | } |
| 7934 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7935 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7936 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7937 | const sp<FakeWindowHandle>& w = |
| 7938 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7939 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7940 | |
| 7941 | touch(); |
| 7942 | |
| 7943 | mTouchWindow->assertNoEvents(); |
| 7944 | } |
| 7945 | |
| 7946 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7947 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7948 | const sp<FakeWindowHandle>& w = |
| 7949 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7950 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7951 | |
| 7952 | touch(); |
| 7953 | |
| 7954 | w->assertNoEvents(); |
| 7955 | } |
| 7956 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7957 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7958 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7959 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7960 | |
| 7961 | touch(); |
| 7962 | |
| 7963 | mTouchWindow->consumeAnyMotionDown(); |
| 7964 | } |
| 7965 | |
| 7966 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7967 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7968 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7969 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7970 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7971 | |
| 7972 | touch({PointF{100, 100}}); |
| 7973 | |
| 7974 | mTouchWindow->consumeAnyMotionDown(); |
| 7975 | } |
| 7976 | |
| 7977 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7978 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7979 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7980 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7981 | |
| 7982 | touch(); |
| 7983 | |
| 7984 | mTouchWindow->consumeAnyMotionDown(); |
| 7985 | } |
| 7986 | |
| 7987 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 7988 | const sp<FakeWindowHandle>& w = |
| 7989 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7990 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7991 | |
| 7992 | touch(); |
| 7993 | |
| 7994 | mTouchWindow->consumeAnyMotionDown(); |
| 7995 | } |
| 7996 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7997 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 7998 | const sp<FakeWindowHandle>& w = |
| 7999 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 8000 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8001 | |
| 8002 | touch(); |
| 8003 | |
| 8004 | w->assertNoEvents(); |
| 8005 | } |
| 8006 | |
| 8007 | /** |
| 8008 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 8009 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 8010 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 8011 | */ |
| 8012 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8013 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 8014 | const sp<FakeWindowHandle>& w = |
| 8015 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8016 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8017 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8018 | |
| 8019 | touch(); |
| 8020 | |
| 8021 | w->consumeMotionOutside(); |
| 8022 | } |
| 8023 | |
| 8024 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 8025 | const sp<FakeWindowHandle>& w = |
| 8026 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8027 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8028 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8029 | |
| 8030 | touch(); |
| 8031 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8032 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8033 | } |
| 8034 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8035 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8036 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8037 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8038 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8039 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8040 | |
| 8041 | touch(); |
| 8042 | |
| 8043 | mTouchWindow->consumeAnyMotionDown(); |
| 8044 | } |
| 8045 | |
| 8046 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 8047 | const sp<FakeWindowHandle>& w = |
| 8048 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8049 | MAXIMUM_OBSCURING_OPACITY); |
| 8050 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8051 | |
| 8052 | touch(); |
| 8053 | |
| 8054 | mTouchWindow->consumeAnyMotionDown(); |
| 8055 | } |
| 8056 | |
| 8057 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8058 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8059 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8060 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8061 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8062 | |
| 8063 | touch(); |
| 8064 | |
| 8065 | mTouchWindow->assertNoEvents(); |
| 8066 | } |
| 8067 | |
| 8068 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 8069 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 8070 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8071 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8072 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8073 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8074 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8075 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8076 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8077 | |
| 8078 | touch(); |
| 8079 | |
| 8080 | mTouchWindow->assertNoEvents(); |
| 8081 | } |
| 8082 | |
| 8083 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 8084 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 8085 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8086 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8087 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8088 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8089 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8090 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8091 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8092 | |
| 8093 | touch(); |
| 8094 | |
| 8095 | mTouchWindow->consumeAnyMotionDown(); |
| 8096 | } |
| 8097 | |
| 8098 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8099 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 8100 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8101 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8102 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8103 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8104 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8105 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8106 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 8107 | |
| 8108 | touch(); |
| 8109 | |
| 8110 | mTouchWindow->consumeAnyMotionDown(); |
| 8111 | } |
| 8112 | |
| 8113 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 8114 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8115 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8116 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8117 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8118 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8119 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8120 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8121 | |
| 8122 | touch(); |
| 8123 | |
| 8124 | mTouchWindow->assertNoEvents(); |
| 8125 | } |
| 8126 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8127 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8128 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 8129 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8130 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8131 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8132 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8133 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8134 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8135 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 8136 | |
| 8137 | touch(); |
| 8138 | |
| 8139 | mTouchWindow->assertNoEvents(); |
| 8140 | } |
| 8141 | |
| 8142 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8143 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 8144 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8145 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8146 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8147 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8148 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8149 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8150 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 8151 | |
| 8152 | touch(); |
| 8153 | |
| 8154 | mTouchWindow->consumeAnyMotionDown(); |
| 8155 | } |
| 8156 | |
| 8157 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 8158 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8159 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8160 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8161 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8162 | |
| 8163 | touch(); |
| 8164 | |
| 8165 | mTouchWindow->consumeAnyMotionDown(); |
| 8166 | } |
| 8167 | |
| 8168 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 8169 | const sp<FakeWindowHandle>& w = |
| 8170 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 8171 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8172 | |
| 8173 | touch(); |
| 8174 | |
| 8175 | mTouchWindow->consumeAnyMotionDown(); |
| 8176 | } |
| 8177 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8178 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8179 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 8180 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 8181 | const sp<FakeWindowHandle>& w = |
| 8182 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 8183 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8184 | |
| 8185 | touch(); |
| 8186 | |
| 8187 | mTouchWindow->assertNoEvents(); |
| 8188 | } |
| 8189 | |
| 8190 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 8191 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 8192 | const sp<FakeWindowHandle>& w = |
| 8193 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 8194 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8195 | |
| 8196 | touch(); |
| 8197 | |
| 8198 | mTouchWindow->consumeAnyMotionDown(); |
| 8199 | } |
| 8200 | |
| 8201 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8202 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 8203 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 8204 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8205 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8206 | OPACITY_ABOVE_THRESHOLD); |
| 8207 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8208 | |
| 8209 | touch(); |
| 8210 | |
| 8211 | mTouchWindow->consumeAnyMotionDown(); |
| 8212 | } |
| 8213 | |
| 8214 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8215 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 8216 | const sp<FakeWindowHandle>& w1 = |
| 8217 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 8218 | OPACITY_BELOW_THRESHOLD); |
| 8219 | const sp<FakeWindowHandle>& w2 = |
| 8220 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8221 | OPACITY_BELOW_THRESHOLD); |
| 8222 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8223 | |
| 8224 | touch(); |
| 8225 | |
| 8226 | mTouchWindow->assertNoEvents(); |
| 8227 | } |
| 8228 | |
| 8229 | /** |
| 8230 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 8231 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 8232 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 8233 | */ |
| 8234 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8235 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 8236 | const sp<FakeWindowHandle>& wB = |
| 8237 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 8238 | OPACITY_BELOW_THRESHOLD); |
| 8239 | const sp<FakeWindowHandle>& wC = |
| 8240 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8241 | OPACITY_BELOW_THRESHOLD); |
| 8242 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 8243 | |
| 8244 | touch(); |
| 8245 | |
| 8246 | mTouchWindow->assertNoEvents(); |
| 8247 | } |
| 8248 | |
| 8249 | /** |
| 8250 | * This test is testing that a window from a different UID but with same application token doesn't |
| 8251 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 8252 | */ |
| 8253 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8254 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 8255 | const sp<FakeWindowHandle>& w = |
| 8256 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 8257 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8258 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8259 | |
| 8260 | touch(); |
| 8261 | |
| 8262 | mTouchWindow->consumeAnyMotionDown(); |
| 8263 | } |
| 8264 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8265 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 8266 | protected: |
| 8267 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8268 | sp<FakeWindowHandle> mWindow; |
| 8269 | sp<FakeWindowHandle> mSecondWindow; |
| 8270 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8271 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8272 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 8273 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8274 | |
| 8275 | void SetUp() override { |
| 8276 | InputDispatcherTest::SetUp(); |
| 8277 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8278 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8279 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8280 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8281 | mSecondWindow = |
| 8282 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8283 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8284 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8285 | mSpyWindow = |
| 8286 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8287 | mSpyWindow->setSpy(true); |
| 8288 | mSpyWindow->setTrustedOverlay(true); |
| 8289 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 8290 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8291 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8292 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8293 | } |
| 8294 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8295 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 8296 | switch (fromSource) { |
| 8297 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 8298 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8299 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8300 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8301 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8302 | break; |
| 8303 | case AINPUT_SOURCE_STYLUS: |
| 8304 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8305 | injectMotionEvent( |
| 8306 | mDispatcher, |
| 8307 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8308 | AINPUT_SOURCE_STYLUS) |
| 8309 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8310 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8311 | .x(50) |
| 8312 | .y(50)) |
| 8313 | .build())); |
| 8314 | break; |
| 8315 | case AINPUT_SOURCE_MOUSE: |
| 8316 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8317 | injectMotionEvent( |
| 8318 | mDispatcher, |
| 8319 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8320 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8321 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8322 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8323 | .x(50) |
| 8324 | .y(50)) |
| 8325 | .build())); |
| 8326 | break; |
| 8327 | default: |
| 8328 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 8329 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8330 | |
| 8331 | // Window should receive motion event. |
| 8332 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8333 | // Spy window should also receive motion event |
| 8334 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8335 | } |
| 8336 | |
| 8337 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 8338 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 8339 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8340 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8341 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8342 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8343 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8344 | |
| 8345 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8346 | mDragWindow = |
| 8347 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8348 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8349 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8350 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8351 | |
| 8352 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8353 | bool transferred = |
| 8354 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8355 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8356 | if (transferred) { |
| 8357 | mWindow->consumeMotionCancel(); |
| 8358 | mDragWindow->consumeMotionDown(); |
| 8359 | } |
| 8360 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8361 | } |
| 8362 | }; |
| 8363 | |
| 8364 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8365 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8366 | |
| 8367 | // Move on window. |
| 8368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8369 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8370 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8371 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8372 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8373 | mWindow->consumeDragEvent(false, 50, 50); |
| 8374 | mSecondWindow->assertNoEvents(); |
| 8375 | |
| 8376 | // Move to another window. |
| 8377 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8378 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8379 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8380 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8381 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8382 | mWindow->consumeDragEvent(true, 150, 50); |
| 8383 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8384 | |
| 8385 | // Move back to original window. |
| 8386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8387 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8388 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8389 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8390 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8391 | mWindow->consumeDragEvent(false, 50, 50); |
| 8392 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 8393 | |
| 8394 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8395 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8396 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8397 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8398 | mWindow->assertNoEvents(); |
| 8399 | mSecondWindow->assertNoEvents(); |
| 8400 | } |
| 8401 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8402 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8403 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8404 | |
| 8405 | // No cancel event after drag start |
| 8406 | mSpyWindow->assertNoEvents(); |
| 8407 | |
| 8408 | const MotionEvent secondFingerDownEvent = |
| 8409 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8410 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8411 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8412 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8413 | .build(); |
| 8414 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8415 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8416 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8417 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8418 | |
| 8419 | // Receives cancel for first pointer after next pointer down |
| 8420 | mSpyWindow->consumeMotionCancel(); |
| 8421 | mSpyWindow->consumeMotionDown(); |
| 8422 | |
| 8423 | mSpyWindow->assertNoEvents(); |
| 8424 | } |
| 8425 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8426 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8427 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8428 | |
| 8429 | // Move on window. |
| 8430 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8431 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8432 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8433 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8434 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8435 | mWindow->consumeDragEvent(false, 50, 50); |
| 8436 | mSecondWindow->assertNoEvents(); |
| 8437 | |
| 8438 | // Move to another window. |
| 8439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8440 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8441 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8442 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8443 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8444 | mWindow->consumeDragEvent(true, 150, 50); |
| 8445 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8446 | |
| 8447 | // drop to another window. |
| 8448 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8449 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8450 | {150, 50})) |
| 8451 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8452 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8453 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8454 | mWindow->assertNoEvents(); |
| 8455 | mSecondWindow->assertNoEvents(); |
| 8456 | } |
| 8457 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8458 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8459 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8460 | |
| 8461 | // Move on window and keep button pressed. |
| 8462 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8463 | injectMotionEvent(mDispatcher, |
| 8464 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8465 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8466 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8467 | .build())) |
| 8468 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8469 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8470 | mWindow->consumeDragEvent(false, 50, 50); |
| 8471 | mSecondWindow->assertNoEvents(); |
| 8472 | |
| 8473 | // Move to another window and release button, expect to drop item. |
| 8474 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8475 | injectMotionEvent(mDispatcher, |
| 8476 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8477 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8478 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8479 | .build())) |
| 8480 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8481 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8482 | mWindow->assertNoEvents(); |
| 8483 | mSecondWindow->assertNoEvents(); |
| 8484 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8485 | |
| 8486 | // nothing to the window. |
| 8487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8488 | injectMotionEvent(mDispatcher, |
| 8489 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 8490 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8491 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).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->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8495 | mWindow->assertNoEvents(); |
| 8496 | mSecondWindow->assertNoEvents(); |
| 8497 | } |
| 8498 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8499 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8500 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8501 | |
| 8502 | // Set second window invisible. |
| 8503 | mSecondWindow->setVisible(false); |
| 8504 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 8505 | |
| 8506 | // Move on window. |
| 8507 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8508 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8509 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8510 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8511 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8512 | mWindow->consumeDragEvent(false, 50, 50); |
| 8513 | mSecondWindow->assertNoEvents(); |
| 8514 | |
| 8515 | // Move to another window. |
| 8516 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8517 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8518 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8519 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8520 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8521 | mWindow->consumeDragEvent(true, 150, 50); |
| 8522 | mSecondWindow->assertNoEvents(); |
| 8523 | |
| 8524 | // drop to another window. |
| 8525 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8526 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8527 | {150, 50})) |
| 8528 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8529 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8530 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 8531 | mWindow->assertNoEvents(); |
| 8532 | mSecondWindow->assertNoEvents(); |
| 8533 | } |
| 8534 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8535 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8536 | // Ensure window could track pointerIds if it didn't support split touch. |
| 8537 | mWindow->setPreventSplitting(true); |
| 8538 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8539 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8540 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8541 | {50, 50})) |
| 8542 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8543 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8544 | |
| 8545 | const MotionEvent secondFingerDownEvent = |
| 8546 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8547 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8548 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8549 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8550 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8551 | .build(); |
| 8552 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8553 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8554 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8555 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8556 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8557 | |
| 8558 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8559 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8560 | } |
| 8561 | |
| 8562 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 8563 | // First down on second window. |
| 8564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8565 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8566 | {150, 50})) |
| 8567 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8568 | |
| 8569 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8570 | |
| 8571 | // Second down on first window. |
| 8572 | const MotionEvent secondFingerDownEvent = |
| 8573 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8574 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8575 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8576 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8577 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8578 | .build(); |
| 8579 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8580 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8581 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8582 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8583 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8584 | |
| 8585 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8586 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8587 | |
| 8588 | // Move on window. |
| 8589 | const MotionEvent secondFingerMoveEvent = |
| 8590 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8591 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8592 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8593 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8594 | .build(); |
| 8595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8596 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8597 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8598 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8599 | mWindow->consumeDragEvent(false, 50, 50); |
| 8600 | mSecondWindow->consumeMotionMove(); |
| 8601 | |
| 8602 | // Release the drag pointer should perform drop. |
| 8603 | const MotionEvent secondFingerUpEvent = |
| 8604 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8605 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8606 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8607 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8608 | .build(); |
| 8609 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8610 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8611 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8612 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8613 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 8614 | mWindow->assertNoEvents(); |
| 8615 | mSecondWindow->consumeMotionMove(); |
| 8616 | } |
| 8617 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8618 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8619 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8620 | |
| 8621 | // Update window of second display. |
| 8622 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8623 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8624 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8625 | |
| 8626 | // Let second display has a touch state. |
| 8627 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8628 | injectMotionEvent(mDispatcher, |
| 8629 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8630 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8631 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8632 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8633 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8634 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8635 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8636 | // Update window again. |
| 8637 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8638 | |
| 8639 | // Move on window. |
| 8640 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8641 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8642 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8643 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8644 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8645 | mWindow->consumeDragEvent(false, 50, 50); |
| 8646 | mSecondWindow->assertNoEvents(); |
| 8647 | |
| 8648 | // Move to another window. |
| 8649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8650 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8651 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8652 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8653 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8654 | mWindow->consumeDragEvent(true, 150, 50); |
| 8655 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8656 | |
| 8657 | // drop to another window. |
| 8658 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8659 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8660 | {150, 50})) |
| 8661 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8662 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8663 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8664 | mWindow->assertNoEvents(); |
| 8665 | mSecondWindow->assertNoEvents(); |
| 8666 | } |
| 8667 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8668 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8669 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8670 | // Move on window. |
| 8671 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8672 | injectMotionEvent(mDispatcher, |
| 8673 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8674 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8675 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8676 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8677 | .x(50) |
| 8678 | .y(50)) |
| 8679 | .build())) |
| 8680 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8681 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8682 | mWindow->consumeDragEvent(false, 50, 50); |
| 8683 | mSecondWindow->assertNoEvents(); |
| 8684 | |
| 8685 | // Move to another window. |
| 8686 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8687 | injectMotionEvent(mDispatcher, |
| 8688 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8689 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8690 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8691 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8692 | .x(150) |
| 8693 | .y(50)) |
| 8694 | .build())) |
| 8695 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8696 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8697 | mWindow->consumeDragEvent(true, 150, 50); |
| 8698 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8699 | |
| 8700 | // drop to another window. |
| 8701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8702 | injectMotionEvent(mDispatcher, |
| 8703 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8704 | .buttonState(0) |
| 8705 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8706 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8707 | .x(150) |
| 8708 | .y(50)) |
| 8709 | .build())) |
| 8710 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8711 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8712 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8713 | mWindow->assertNoEvents(); |
| 8714 | mSecondWindow->assertNoEvents(); |
| 8715 | } |
| 8716 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8717 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8718 | |
| 8719 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8720 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8721 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8722 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8723 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8724 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8725 | window->setFocusable(true); |
| 8726 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8727 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8728 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8729 | |
| 8730 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8731 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8732 | window->assertNoEvents(); |
| 8733 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8734 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8735 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8736 | window->assertNoEvents(); |
| 8737 | |
| 8738 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8739 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8740 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8741 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8742 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8743 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8744 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8745 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8746 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8747 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8748 | window->assertNoEvents(); |
| 8749 | } |
| 8750 | |
| 8751 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8752 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8753 | std::make_shared<FakeApplicationHandle>(); |
| 8754 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8755 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8756 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8757 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 8758 | obscuringWindow->setOwnerInfo(111, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8759 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8760 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8761 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8762 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8763 | window->setDropInputIfObscured(true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 8764 | window->setOwnerInfo(222, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8765 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8766 | window->setFocusable(true); |
| 8767 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8768 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8769 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8770 | |
| 8771 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8772 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8773 | window->assertNoEvents(); |
| 8774 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8775 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8776 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8777 | window->assertNoEvents(); |
| 8778 | |
| 8779 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8780 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8781 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8782 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8783 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8784 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8785 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8786 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8787 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8788 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8789 | window->assertNoEvents(); |
| 8790 | } |
| 8791 | |
| 8792 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8793 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8794 | std::make_shared<FakeApplicationHandle>(); |
| 8795 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8796 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8797 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8798 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 8799 | obscuringWindow->setOwnerInfo(111, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8800 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8801 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8802 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8803 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8804 | window->setDropInputIfObscured(true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 8805 | window->setOwnerInfo(222, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8806 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8807 | window->setFocusable(true); |
| 8808 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8809 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8810 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8811 | |
| 8812 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8813 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8814 | window->assertNoEvents(); |
| 8815 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8816 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8817 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8818 | window->assertNoEvents(); |
| 8819 | |
| 8820 | // When the window is no longer obscured because it went on top, it should get input |
| 8821 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8822 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8823 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8824 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8825 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8826 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8827 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8828 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8829 | window->assertNoEvents(); |
| 8830 | } |
| 8831 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8832 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8833 | protected: |
| 8834 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8835 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8836 | sp<FakeWindowHandle> mWindow; |
| 8837 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8838 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8839 | |
| 8840 | void SetUp() override { |
| 8841 | InputDispatcherTest::SetUp(); |
| 8842 | |
| 8843 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8844 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8845 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8846 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8847 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8848 | mSecondWindow = |
| 8849 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8850 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8851 | mThirdWindow = |
| 8852 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8853 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8854 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8855 | |
| 8856 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8857 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8858 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8859 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8860 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8861 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8862 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8863 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8864 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8865 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8866 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8867 | mThirdWindow->assertNoEvents(); |
| 8868 | } |
| 8869 | |
| 8870 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8871 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8872 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8873 | SECOND_DISPLAY_ID)) { |
| 8874 | mWindow->assertNoEvents(); |
| 8875 | mSecondWindow->assertNoEvents(); |
| 8876 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8877 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8878 | } |
| 8879 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 8880 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8881 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8882 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8883 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8884 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8885 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8886 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8887 | } |
| 8888 | }; |
| 8889 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8890 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8891 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8892 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8893 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8894 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8895 | } |
| 8896 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8897 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8898 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8899 | int32_t ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 8900 | gui::Uid ownerUid = windowInfo.ownerUid; |
| 8901 | mWindow->setOwnerInfo(/*pid=*/-1, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8902 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8903 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8904 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8905 | mWindow->assertNoEvents(); |
| 8906 | mSecondWindow->assertNoEvents(); |
| 8907 | } |
| 8908 | |
| 8909 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8910 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8911 | int32_t ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 8912 | gui::Uid ownerUid = windowInfo.ownerUid; |
| 8913 | mWindow->setOwnerInfo(/*pid=*/-1, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8914 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8915 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8916 | } |
| 8917 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8918 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8919 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8920 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8921 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8922 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8923 | mWindow->assertNoEvents(); |
| 8924 | mSecondWindow->assertNoEvents(); |
| 8925 | } |
| 8926 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8927 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8928 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8929 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8930 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8931 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8932 | mWindow->assertNoEvents(); |
| 8933 | mSecondWindow->assertNoEvents(); |
| 8934 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8935 | } |
| 8936 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8937 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8938 | // Interact with the window first. |
| 8939 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8940 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8941 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8942 | |
| 8943 | // Then remove focus. |
| 8944 | mWindow->setFocusable(false); |
| 8945 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8946 | |
| 8947 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8948 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8949 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8950 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8951 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8952 | } |
| 8953 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8954 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8955 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8956 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8957 | std::shared_ptr<FakeApplicationHandle> application = |
| 8958 | std::make_shared<FakeApplicationHandle>(); |
| 8959 | std::string name = "Fake Spy "; |
| 8960 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8961 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8962 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8963 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8964 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8965 | return spy; |
| 8966 | } |
| 8967 | |
| 8968 | sp<FakeWindowHandle> createForeground() { |
| 8969 | std::shared_ptr<FakeApplicationHandle> application = |
| 8970 | std::make_shared<FakeApplicationHandle>(); |
| 8971 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8972 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 8973 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8974 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8975 | return window; |
| 8976 | } |
| 8977 | |
| 8978 | private: |
| 8979 | int mSpyCount{0}; |
| 8980 | }; |
| 8981 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8982 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8983 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8984 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 8985 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8986 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 8987 | ScopedSilentDeath _silentDeath; |
| 8988 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8989 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8990 | spy->setTrustedOverlay(false); |
| 8991 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 8992 | ".* not a trusted overlay"); |
| 8993 | } |
| 8994 | |
| 8995 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8996 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 8997 | */ |
| 8998 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8999 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9000 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 9001 | |
| 9002 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9003 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9004 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9005 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9006 | } |
| 9007 | |
| 9008 | /** |
| 9009 | * Verify the order in which different input windows receive events. The touched foreground window |
| 9010 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 9011 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 9012 | * receive events before ones belows it. |
| 9013 | * |
| 9014 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 9015 | * spy1, spy2, window, spy3. |
| 9016 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 9017 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 9018 | * window. |
| 9019 | */ |
| 9020 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 9021 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9022 | auto spy1 = createSpy(); |
| 9023 | auto spy2 = createSpy(); |
| 9024 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9025 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 9026 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 9027 | const size_t numChannels = channels.size(); |
| 9028 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 9029 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9030 | if (!epollFd.ok()) { |
| 9031 | FAIL() << "Failed to create epoll fd"; |
| 9032 | } |
| 9033 | |
| 9034 | for (size_t i = 0; i < numChannels; i++) { |
| 9035 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 9036 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 9037 | FAIL() << "Failed to add fd to epoll"; |
| 9038 | } |
| 9039 | } |
| 9040 | |
| 9041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9042 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9043 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9044 | |
| 9045 | std::vector<size_t> eventOrder; |
| 9046 | std::vector<struct epoll_event> events(numChannels); |
| 9047 | for (;;) { |
| 9048 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 9049 | (100ms).count()); |
| 9050 | if (nFds < 0) { |
| 9051 | FAIL() << "Failed to call epoll_wait"; |
| 9052 | } |
| 9053 | if (nFds == 0) { |
| 9054 | break; // epoll_wait timed out |
| 9055 | } |
| 9056 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 9057 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 9058 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9059 | channels[i]->consumeMotionDown(); |
| 9060 | } |
| 9061 | } |
| 9062 | |
| 9063 | // Verify the order in which the events were received. |
| 9064 | EXPECT_EQ(3u, eventOrder.size()); |
| 9065 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 9066 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 9067 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 9068 | } |
| 9069 | |
| 9070 | /** |
| 9071 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 9072 | */ |
| 9073 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 9074 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9075 | auto spy = createSpy(); |
| 9076 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9077 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9078 | |
| 9079 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9080 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9081 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9082 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9083 | spy->assertNoEvents(); |
| 9084 | } |
| 9085 | |
| 9086 | /** |
| 9087 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 9088 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 9089 | * to the window. |
| 9090 | */ |
| 9091 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 9092 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9093 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9094 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 9095 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9096 | |
| 9097 | // Inject an event outside the spy window's touchable region. |
| 9098 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9099 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9100 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9101 | window->consumeMotionDown(); |
| 9102 | spy->assertNoEvents(); |
| 9103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9104 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9105 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9106 | window->consumeMotionUp(); |
| 9107 | spy->assertNoEvents(); |
| 9108 | |
| 9109 | // Inject an event inside the spy window's touchable region. |
| 9110 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9111 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9112 | {5, 10})) |
| 9113 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9114 | window->consumeMotionDown(); |
| 9115 | spy->consumeMotionDown(); |
| 9116 | } |
| 9117 | |
| 9118 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9119 | * 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] | 9120 | * 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] | 9121 | */ |
| 9122 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 9123 | auto window = createForeground(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9124 | window->setOwnerInfo(12, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9125 | auto spy = createSpy(); |
| 9126 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9127 | spy->setOwnerInfo(56, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9128 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 9129 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9130 | |
| 9131 | // Inject an event outside the spy window's frame and touchable region. |
| 9132 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9133 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9134 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9135 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9136 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9137 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9138 | } |
| 9139 | |
| 9140 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9141 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 9142 | * pointers that are down within its bounds. |
| 9143 | */ |
| 9144 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 9145 | auto windowLeft = createForeground(); |
| 9146 | windowLeft->setFrame({0, 0, 100, 200}); |
| 9147 | auto windowRight = createForeground(); |
| 9148 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9149 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9150 | spy->setFrame({0, 0, 200, 200}); |
| 9151 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 9152 | |
| 9153 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9154 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9155 | {50, 50})) |
| 9156 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9157 | windowLeft->consumeMotionDown(); |
| 9158 | spy->consumeMotionDown(); |
| 9159 | |
| 9160 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9161 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9162 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9163 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9164 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9165 | .build(); |
| 9166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9167 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9168 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9169 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9170 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9171 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9172 | } |
| 9173 | |
| 9174 | /** |
| 9175 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 9176 | * the spy should receive the second pointer with ACTION_DOWN. |
| 9177 | */ |
| 9178 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 9179 | auto window = createForeground(); |
| 9180 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9181 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9182 | spyRight->setFrame({100, 0, 200, 200}); |
| 9183 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 9184 | |
| 9185 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9186 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9187 | {50, 50})) |
| 9188 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9189 | window->consumeMotionDown(); |
| 9190 | spyRight->assertNoEvents(); |
| 9191 | |
| 9192 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9193 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9194 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9195 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9196 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9197 | .build(); |
| 9198 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9199 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9200 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9201 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9202 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9203 | spyRight->consumeMotionDown(); |
| 9204 | } |
| 9205 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9206 | /** |
| 9207 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 9208 | * windows should be allowed to control split touch. |
| 9209 | */ |
| 9210 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9211 | // 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] | 9212 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9213 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9214 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9215 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9216 | auto window = createForeground(); |
| 9217 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9218 | |
| 9219 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9220 | |
| 9221 | // First finger down, no window touched. |
| 9222 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9223 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9224 | {100, 200})) |
| 9225 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9226 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9227 | window->assertNoEvents(); |
| 9228 | |
| 9229 | // Second finger down on window, the window should receive touch down. |
| 9230 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9231 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9232 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9233 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9234 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9235 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9236 | .build(); |
| 9237 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9238 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9239 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9240 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9241 | |
| 9242 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9243 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9244 | } |
| 9245 | |
| 9246 | /** |
| 9247 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 9248 | * do not receive key events. |
| 9249 | */ |
| 9250 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9251 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9252 | spy->setFocusable(false); |
| 9253 | |
| 9254 | auto window = createForeground(); |
| 9255 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9256 | setFocusedWindow(window); |
| 9257 | window->consumeFocusEvent(true); |
| 9258 | |
| 9259 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 9260 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9261 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9262 | |
| 9263 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 9264 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9265 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9266 | |
| 9267 | spy->assertNoEvents(); |
| 9268 | } |
| 9269 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9270 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 9271 | |
| 9272 | /** |
| 9273 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 9274 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 9275 | */ |
| 9276 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 9277 | auto window = createForeground(); |
| 9278 | auto spy1 = createSpy(); |
| 9279 | auto spy2 = createSpy(); |
| 9280 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 9281 | |
| 9282 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9283 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9284 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9285 | window->consumeMotionDown(); |
| 9286 | spy1->consumeMotionDown(); |
| 9287 | spy2->consumeMotionDown(); |
| 9288 | |
| 9289 | // Pilfer pointers from the second spy window. |
| 9290 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 9291 | spy2->assertNoEvents(); |
| 9292 | spy1->consumeMotionCancel(); |
| 9293 | window->consumeMotionCancel(); |
| 9294 | |
| 9295 | // The rest of the gesture should only be sent to the second spy window. |
| 9296 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9297 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 9298 | ADISPLAY_ID_DEFAULT)) |
| 9299 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9300 | spy2->consumeMotionMove(); |
| 9301 | spy1->assertNoEvents(); |
| 9302 | window->assertNoEvents(); |
| 9303 | } |
| 9304 | |
| 9305 | /** |
| 9306 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 9307 | * in the middle of the gesture. |
| 9308 | */ |
| 9309 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 9310 | auto window = createForeground(); |
| 9311 | auto spy = createSpy(); |
| 9312 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9313 | |
| 9314 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9315 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9316 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9317 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9318 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9319 | |
| 9320 | window->releaseChannel(); |
| 9321 | |
| 9322 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9323 | |
| 9324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9325 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9326 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9327 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9328 | } |
| 9329 | |
| 9330 | /** |
| 9331 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 9332 | * the spy, but not to any other windows. |
| 9333 | */ |
| 9334 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 9335 | auto spy = createSpy(); |
| 9336 | auto window = createForeground(); |
| 9337 | |
| 9338 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9339 | |
| 9340 | // First finger down on the window and the spy. |
| 9341 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9342 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9343 | {100, 200})) |
| 9344 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9345 | spy->consumeMotionDown(); |
| 9346 | window->consumeMotionDown(); |
| 9347 | |
| 9348 | // Spy window pilfers the pointers. |
| 9349 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9350 | window->consumeMotionCancel(); |
| 9351 | |
| 9352 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 9353 | const MotionEvent secondFingerDownEvent = |
| 9354 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9355 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9356 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9357 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9358 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9359 | .build(); |
| 9360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9361 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9362 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9363 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9364 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9365 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9366 | |
| 9367 | // Third finger goes down outside all windows, so injection should fail. |
| 9368 | const MotionEvent thirdFingerDownEvent = |
| 9369 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9370 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9371 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9372 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9373 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 9374 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9375 | .build(); |
| 9376 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 9377 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9378 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9379 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9380 | |
| 9381 | spy->assertNoEvents(); |
| 9382 | window->assertNoEvents(); |
| 9383 | } |
| 9384 | |
| 9385 | /** |
| 9386 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 9387 | */ |
| 9388 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 9389 | auto spy = createSpy(); |
| 9390 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9391 | auto window = createForeground(); |
| 9392 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9393 | |
| 9394 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9395 | |
| 9396 | // First finger down on the window only |
| 9397 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9398 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9399 | {150, 150})) |
| 9400 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9401 | window->consumeMotionDown(); |
| 9402 | |
| 9403 | // Second finger down on the spy and window |
| 9404 | const MotionEvent secondFingerDownEvent = |
| 9405 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9406 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9407 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9408 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9409 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9410 | .build(); |
| 9411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9412 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9413 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9414 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9415 | spy->consumeMotionDown(); |
| 9416 | window->consumeMotionPointerDown(1); |
| 9417 | |
| 9418 | // Third finger down on the spy and window |
| 9419 | const MotionEvent thirdFingerDownEvent = |
| 9420 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9421 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9422 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9423 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9424 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 9425 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9426 | .build(); |
| 9427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9428 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9429 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9430 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9431 | spy->consumeMotionPointerDown(1); |
| 9432 | window->consumeMotionPointerDown(2); |
| 9433 | |
| 9434 | // Spy window pilfers the pointers. |
| 9435 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9436 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9437 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9438 | |
| 9439 | spy->assertNoEvents(); |
| 9440 | window->assertNoEvents(); |
| 9441 | } |
| 9442 | |
| 9443 | /** |
| 9444 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 9445 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 9446 | * window, then that window should receive ACTION_CANCEL. |
| 9447 | */ |
| 9448 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 9449 | auto spy = createSpy(); |
| 9450 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9451 | auto window = createForeground(); |
| 9452 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9453 | |
| 9454 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9455 | |
| 9456 | // First finger down on both spy and window |
| 9457 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9458 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9459 | {10, 10})) |
| 9460 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9461 | window->consumeMotionDown(); |
| 9462 | spy->consumeMotionDown(); |
| 9463 | |
| 9464 | // Second finger down on the spy and window |
| 9465 | const MotionEvent secondFingerDownEvent = |
| 9466 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9467 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9468 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9469 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9470 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9471 | .build(); |
| 9472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9473 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9474 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9475 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9476 | spy->consumeMotionPointerDown(1); |
| 9477 | window->consumeMotionPointerDown(1); |
| 9478 | |
| 9479 | // Spy window pilfers the pointers. |
| 9480 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9481 | window->consumeMotionCancel(); |
| 9482 | |
| 9483 | spy->assertNoEvents(); |
| 9484 | window->assertNoEvents(); |
| 9485 | } |
| 9486 | |
| 9487 | /** |
| 9488 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 9489 | * be sent to other windows |
| 9490 | */ |
| 9491 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 9492 | auto spy = createSpy(); |
| 9493 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9494 | auto window = createForeground(); |
| 9495 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9496 | |
| 9497 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9498 | |
| 9499 | // First finger down on both window and spy |
| 9500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9501 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9502 | {10, 10})) |
| 9503 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9504 | window->consumeMotionDown(); |
| 9505 | spy->consumeMotionDown(); |
| 9506 | |
| 9507 | // Spy window pilfers the pointers. |
| 9508 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9509 | window->consumeMotionCancel(); |
| 9510 | |
| 9511 | // Second finger down on the window only |
| 9512 | const MotionEvent secondFingerDownEvent = |
| 9513 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9514 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9515 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9516 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9517 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9518 | .build(); |
| 9519 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9520 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9521 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9522 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9523 | window->consumeMotionDown(); |
| 9524 | window->assertNoEvents(); |
| 9525 | |
| 9526 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 9527 | spy->consumeMotionMove(); |
| 9528 | spy->assertNoEvents(); |
| 9529 | } |
| 9530 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9531 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 9532 | public: |
| 9533 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 9534 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9535 | std::make_shared<FakeApplicationHandle>(); |
| 9536 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9537 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 9538 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9539 | overlay->setFocusable(false); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9540 | overlay->setOwnerInfo(111, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9541 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9542 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9543 | overlay->setTrustedOverlay(true); |
| 9544 | |
| 9545 | std::shared_ptr<FakeApplicationHandle> application = |
| 9546 | std::make_shared<FakeApplicationHandle>(); |
| 9547 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9548 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9549 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9550 | window->setFocusable(true); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9551 | window->setOwnerInfo(222, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9552 | |
| 9553 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9554 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9555 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9556 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9557 | return {std::move(overlay), std::move(window)}; |
| 9558 | } |
| 9559 | |
| 9560 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9561 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9562 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9563 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9564 | } |
| 9565 | |
| 9566 | void sendStylusEvent(int32_t action) { |
| 9567 | NotifyMotionArgs motionArgs = |
| 9568 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9569 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9570 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9571 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9572 | } |
| 9573 | }; |
| 9574 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9575 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9576 | |
| 9577 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 9578 | ScopedSilentDeath _silentDeath; |
| 9579 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9580 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9581 | overlay->setTrustedOverlay(false); |
| 9582 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9583 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9584 | ".* not a trusted overlay"); |
| 9585 | } |
| 9586 | |
| 9587 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9588 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9589 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9590 | |
| 9591 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9592 | overlay->consumeMotionDown(); |
| 9593 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9594 | overlay->consumeMotionUp(); |
| 9595 | |
| 9596 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9597 | window->consumeMotionDown(); |
| 9598 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9599 | window->consumeMotionUp(); |
| 9600 | |
| 9601 | overlay->assertNoEvents(); |
| 9602 | window->assertNoEvents(); |
| 9603 | } |
| 9604 | |
| 9605 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9606 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9607 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9608 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9609 | |
| 9610 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9611 | overlay->consumeMotionDown(); |
| 9612 | window->consumeMotionDown(); |
| 9613 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9614 | overlay->consumeMotionUp(); |
| 9615 | window->consumeMotionUp(); |
| 9616 | |
| 9617 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9618 | window->consumeMotionDown(); |
| 9619 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9620 | window->consumeMotionUp(); |
| 9621 | |
| 9622 | overlay->assertNoEvents(); |
| 9623 | window->assertNoEvents(); |
| 9624 | } |
| 9625 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9626 | /** |
| 9627 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9628 | * The scenario is as follows: |
| 9629 | * - The stylus interceptor overlay is configured as a spy window. |
| 9630 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9631 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9632 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9633 | */ |
| 9634 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9635 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9636 | overlay->setSpy(true); |
| 9637 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9638 | |
| 9639 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9640 | overlay->consumeMotionDown(); |
| 9641 | window->consumeMotionDown(); |
| 9642 | |
| 9643 | // The interceptor pilfers the pointers. |
| 9644 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9645 | window->consumeMotionCancel(); |
| 9646 | |
| 9647 | // The interceptor configures itself so that it is no longer a spy. |
| 9648 | overlay->setSpy(false); |
| 9649 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9650 | |
| 9651 | // It continues to receive the rest of the stylus gesture. |
| 9652 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9653 | overlay->consumeMotionMove(); |
| 9654 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9655 | overlay->consumeMotionUp(); |
| 9656 | |
| 9657 | window->assertNoEvents(); |
| 9658 | } |
| 9659 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9660 | struct User { |
| 9661 | int32_t mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9662 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9663 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9664 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9665 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9666 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9667 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9668 | |
| 9669 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9670 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9671 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9672 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9673 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9674 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9675 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9676 | } |
| 9677 | |
| 9678 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9679 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9680 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9681 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9682 | mPolicyFlags); |
| 9683 | } |
| 9684 | |
| 9685 | sp<FakeWindowHandle> createWindow() const { |
| 9686 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9687 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9688 | sp<FakeWindowHandle> window = |
| 9689 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9690 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9691 | window->setOwnerInfo(mPid, mUid); |
| 9692 | return window; |
| 9693 | } |
| 9694 | }; |
| 9695 | |
| 9696 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9697 | |
| 9698 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9699 | auto owner = User(mDispatcher, 10, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9700 | auto window = owner.createWindow(); |
| 9701 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9702 | |
| 9703 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9704 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9705 | window->consumeMotionDown(); |
| 9706 | |
| 9707 | setFocusedWindow(window); |
| 9708 | window->consumeFocusEvent(true); |
| 9709 | |
| 9710 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9711 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9712 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9713 | } |
| 9714 | |
| 9715 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9716 | auto owner = User(mDispatcher, 10, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9717 | auto window = owner.createWindow(); |
| 9718 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9719 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9720 | auto rando = User(mDispatcher, 20, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9721 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9722 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9723 | |
| 9724 | setFocusedWindow(window); |
| 9725 | window->consumeFocusEvent(true); |
| 9726 | |
| 9727 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9728 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9729 | window->assertNoEvents(); |
| 9730 | } |
| 9731 | |
| 9732 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9733 | auto owner = User(mDispatcher, 10, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9734 | auto window = owner.createWindow(); |
| 9735 | auto spy = owner.createWindow(); |
| 9736 | spy->setSpy(true); |
| 9737 | spy->setTrustedOverlay(true); |
| 9738 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9739 | |
| 9740 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9741 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9742 | spy->consumeMotionDown(); |
| 9743 | window->consumeMotionDown(); |
| 9744 | } |
| 9745 | |
| 9746 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9747 | auto owner = User(mDispatcher, 10, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9748 | auto window = owner.createWindow(); |
| 9749 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9750 | auto rando = User(mDispatcher, 20, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9751 | auto randosSpy = rando.createWindow(); |
| 9752 | randosSpy->setSpy(true); |
| 9753 | randosSpy->setTrustedOverlay(true); |
| 9754 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9755 | |
| 9756 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9757 | // not receive the event. |
| 9758 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9759 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9760 | randosSpy->assertNoEvents(); |
| 9761 | window->consumeMotionDown(); |
| 9762 | } |
| 9763 | |
| 9764 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9765 | auto owner = User(mDispatcher, 10, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9766 | auto window = owner.createWindow(); |
| 9767 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9768 | auto rando = User(mDispatcher, 20, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9769 | auto randosSpy = rando.createWindow(); |
| 9770 | randosSpy->setSpy(true); |
| 9771 | randosSpy->setTrustedOverlay(true); |
| 9772 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9773 | |
| 9774 | // A user that has injection permission can inject into any window. |
| 9775 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9776 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9777 | ADISPLAY_ID_DEFAULT)); |
| 9778 | randosSpy->consumeMotionDown(); |
| 9779 | window->consumeMotionDown(); |
| 9780 | |
| 9781 | setFocusedWindow(randosSpy); |
| 9782 | randosSpy->consumeFocusEvent(true); |
| 9783 | |
| 9784 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9785 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9786 | window->assertNoEvents(); |
| 9787 | } |
| 9788 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9789 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9790 | auto owner = User(mDispatcher, 10, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9791 | auto window = owner.createWindow(); |
| 9792 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame^] | 9793 | auto rando = User(mDispatcher, 20, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9794 | auto randosWindow = rando.createWindow(); |
| 9795 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9796 | randosWindow->setWatchOutsideTouch(true); |
| 9797 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9798 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9799 | // 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] | 9800 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9801 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9802 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9803 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9804 | } |
| 9805 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9806 | } // namespace android::inputdispatcher |