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 | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 98 | static constexpr gui::Pid WINDOW_PID{999}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 99 | static constexpr gui::Uid WINDOW_UID{1001}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 100 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 101 | // The default pid and uid for the windows created on the secondary display by the test. |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 102 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 103 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 104 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 105 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 106 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 107 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 108 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 109 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 110 | static constexpr int expectedWallpaperFlags = |
| 111 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 112 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 113 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 114 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 115 | struct PointF { |
| 116 | float x; |
| 117 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 118 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 119 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 121 | /** |
| 122 | * Return a DOWN key event with KEYCODE_A. |
| 123 | */ |
| 124 | static KeyEvent getTestKeyEvent() { |
| 125 | KeyEvent event; |
| 126 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 127 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 128 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 129 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 130 | return event; |
| 131 | } |
| 132 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 133 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 134 | ASSERT_EQ(expectedAction, receivedAction) |
| 135 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 136 | << MotionEvent::actionToString(receivedAction); |
| 137 | } |
| 138 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 139 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 140 | bool matches = action == arg.getAction(); |
| 141 | if (!matches) { |
| 142 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 143 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 144 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 145 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 146 | if (!matches) { |
| 147 | *result_listener << "; "; |
| 148 | } |
| 149 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 150 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 151 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 152 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 153 | if (!matches) { |
| 154 | *result_listener << "; "; |
| 155 | } |
| 156 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 157 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 158 | } |
| 159 | return matches; |
| 160 | } |
| 161 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 162 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 163 | return arg.getDownTime() == downTime; |
| 164 | } |
| 165 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 166 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { |
| 167 | return arg.getDisplayId() == displayId; |
| 168 | } |
| 169 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 170 | MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") { |
| 171 | return arg.getDeviceId() == deviceId; |
| 172 | } |
| 173 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 174 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 175 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 176 | << inputEventSourceToString(arg.getSource()); |
| 177 | return arg.getSource() == source; |
| 178 | } |
| 179 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 180 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { |
| 181 | return arg.getFlags() == flags; |
| 182 | } |
| 183 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 184 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 185 | if (arg.getPointerCount() != 1) { |
| 186 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 187 | return false; |
| 188 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 189 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 192 | MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") { |
| 193 | return arg.getPointerCount() == pointerCount; |
| 194 | } |
| 195 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 196 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 197 | // Build a map for the received pointers, by pointer id |
| 198 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 199 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 200 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 201 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 202 | } |
| 203 | return pointers == actualPointers; |
| 204 | } |
| 205 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 206 | // --- FakeInputDispatcherPolicy --- |
| 207 | |
| 208 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 209 | struct AnrResult { |
| 210 | sp<IBinder> token{}; |
| 211 | gui::Pid pid{gui::Pid::INVALID}; |
| 212 | }; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 213 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 215 | FakeInputDispatcherPolicy() = default; |
| 216 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 217 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 218 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 219 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 220 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 221 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 222 | |
| 223 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 224 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 225 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 226 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 229 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 230 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 231 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 232 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 233 | |
| 234 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 235 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 236 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 237 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 238 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 239 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 240 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 241 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 244 | void assertFilterInputEventWasNotCalled() { |
| 245 | std::scoped_lock lock(mLock); |
| 246 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 247 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 248 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 249 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 250 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 251 | ASSERT_TRUE(mConfigurationChangedTime) |
| 252 | << "Timed out waiting for configuration changed call"; |
| 253 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 254 | mConfigurationChangedTime = std::nullopt; |
| 255 | } |
| 256 | |
| 257 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 258 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 259 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 260 | // We do not check id because it is not exposed to the policy |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 261 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 262 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 263 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 264 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 265 | mLastNotifySwitch = std::nullopt; |
| 266 | } |
| 267 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 268 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 269 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 270 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 271 | mOnPointerDownToken.clear(); |
| 272 | } |
| 273 | |
| 274 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 275 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 276 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 277 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 280 | // This function must be called soon after the expected ANR timer starts, |
| 281 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 282 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 283 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 284 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 285 | std::unique_lock lock(mLock); |
| 286 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 287 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 288 | ASSERT_NO_FATAL_FAILURE( |
| 289 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 290 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 293 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 294 | const sp<WindowInfoHandle>& window) { |
| 295 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 296 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 297 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 300 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 301 | const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 302 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 303 | std::unique_lock lock(mLock); |
| 304 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 305 | AnrResult result; |
| 306 | ASSERT_NO_FATAL_FAILURE(result = |
| 307 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 308 | ASSERT_EQ(expectedToken, result.token); |
| 309 | ASSERT_EQ(expectedPid, result.pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 312 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 313 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 314 | std::unique_lock lock(mLock); |
| 315 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 316 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 317 | const auto& [token, _] = result; |
| 318 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 321 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 322 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 323 | std::unique_lock lock(mLock); |
| 324 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 325 | AnrResult result; |
| 326 | ASSERT_NO_FATAL_FAILURE( |
| 327 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 328 | ASSERT_EQ(expectedToken, result.token); |
| 329 | ASSERT_EQ(expectedPid, result.pid); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 333 | sp<IBinder> getResponsiveWindowToken() { |
| 334 | std::unique_lock lock(mLock); |
| 335 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 336 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 337 | const auto& [token, _] = result; |
| 338 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void assertNotifyAnrWasNotCalled() { |
| 342 | std::scoped_lock lock(mLock); |
| 343 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 344 | ASSERT_TRUE(mAnrWindows.empty()); |
| 345 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 346 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 347 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 350 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 351 | std::unique_lock lock(mLock); |
| 352 | base::ScopedLockAssertion assumeLocked(mLock); |
| 353 | |
| 354 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 355 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 356 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 357 | enabled; |
| 358 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 359 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 360 | << ") to be called."; |
| 361 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 362 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 363 | auto request = *mPointerCaptureRequest; |
| 364 | mPointerCaptureRequest.reset(); |
| 365 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void assertSetPointerCaptureNotCalled() { |
| 369 | std::unique_lock lock(mLock); |
| 370 | base::ScopedLockAssertion assumeLocked(mLock); |
| 371 | |
| 372 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 373 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 374 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 375 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 376 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 377 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 378 | } |
| 379 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 380 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 381 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 382 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 383 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 384 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 385 | } |
| 386 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 387 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 388 | std::unique_lock lock(mLock); |
| 389 | base::ScopedLockAssertion assumeLocked(mLock); |
| 390 | std::optional<sp<IBinder>> receivedToken = |
| 391 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 392 | mNotifyInputChannelBroken); |
| 393 | ASSERT_TRUE(receivedToken.has_value()); |
| 394 | ASSERT_EQ(token, *receivedToken); |
| 395 | } |
| 396 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 397 | /** |
| 398 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 399 | */ |
| 400 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 401 | mInterceptKeyTimeout = timeout; |
| 402 | } |
| 403 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 404 | void assertUserActivityPoked() { |
| 405 | std::scoped_lock lock(mLock); |
| 406 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; |
| 407 | } |
| 408 | |
| 409 | void assertUserActivityNotPoked() { |
| 410 | std::scoped_lock lock(mLock); |
| 411 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; |
| 412 | } |
| 413 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 414 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 415 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 416 | } |
| 417 | |
| 418 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 419 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 420 | } |
| 421 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 422 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 423 | std::mutex mLock; |
| 424 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 425 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 426 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 427 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 428 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 429 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 430 | |
| 431 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 432 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 433 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 434 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 435 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 436 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 437 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 438 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 439 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 440 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 441 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 442 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 443 | bool mPokedUserActivity GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 444 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 445 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 446 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 447 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 448 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 449 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 450 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 451 | // first entry from the container and erase it. |
| 452 | template <class T> |
| 453 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 454 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 455 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 456 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 457 | // before checking if ANR was called. |
| 458 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 459 | // to provide it some time to act. 100ms seems reasonable. |
| 460 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 461 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 462 | std::optional<T> token = |
| 463 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 464 | if (!token.has_value()) { |
| 465 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 466 | return {}; |
| 467 | } |
| 468 | |
| 469 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 470 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 471 | // the dispatcher started counting before this function was called |
| 472 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 473 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 474 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 475 | << "ms, but waited " |
| 476 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 477 | << "ms instead"; |
| 478 | } |
| 479 | return *token; |
| 480 | } |
| 481 | |
| 482 | template <class T> |
| 483 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 484 | std::queue<T>& storage, |
| 485 | std::unique_lock<std::mutex>& lock, |
| 486 | std::condition_variable& condition) |
| 487 | REQUIRES(mLock) { |
| 488 | condition.wait_for(lock, timeout, |
| 489 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 490 | if (storage.empty()) { |
| 491 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 492 | return std::nullopt; |
| 493 | } |
| 494 | T item = storage.front(); |
| 495 | storage.pop(); |
| 496 | return std::make_optional(item); |
| 497 | } |
| 498 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 499 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 500 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 501 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 504 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 505 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 506 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 507 | ASSERT_TRUE(pid.has_value()); |
| 508 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 509 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 510 | } |
| 511 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 512 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 513 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 514 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 515 | ASSERT_TRUE(pid.has_value()); |
| 516 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 517 | mNotifyAnr.notify_all(); |
| 518 | } |
| 519 | |
| 520 | void notifyNoFocusedWindowAnr( |
| 521 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 522 | std::scoped_lock lock(mLock); |
| 523 | mAnrApplications.push(applicationHandle); |
| 524 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 527 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 528 | std::scoped_lock lock(mLock); |
| 529 | mBrokenInputChannels.push(connectionToken); |
| 530 | mNotifyInputChannelBroken.notify_all(); |
| 531 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 532 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 533 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 534 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 535 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 536 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 537 | const std::vector<float>& values) override {} |
| 538 | |
| 539 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 540 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 541 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 542 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 543 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 544 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 545 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 546 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 547 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 548 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 549 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 553 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 554 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 555 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 556 | break; |
| 557 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 558 | default: { |
| 559 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 560 | break; |
| 561 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 562 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 563 | return true; |
| 564 | } |
| 565 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 566 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 567 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 568 | // Clear intercept state when we handled the event. |
| 569 | mInterceptKeyTimeout = 0ms; |
| 570 | } |
| 571 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 572 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 573 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 575 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 576 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 577 | // Clear intercept state so we could dispatch the event in next wake. |
| 578 | mInterceptKeyTimeout = 0ms; |
| 579 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 582 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&, |
| 583 | uint32_t) override { |
| 584 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 587 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 588 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 589 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 590 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 591 | * essentially a passthrough for notifySwitch. |
| 592 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 593 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 596 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { |
| 597 | std::scoped_lock lock(mLock); |
| 598 | mPokedUserActivity = true; |
| 599 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 600 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 601 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 602 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 603 | mOnPointerDownToken = newToken; |
| 604 | } |
| 605 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 606 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 607 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 608 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 609 | mPointerCaptureChangedCondition.notify_all(); |
| 610 | } |
| 611 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 612 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 613 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 614 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 615 | mDropTargetWindowToken = token; |
| 616 | } |
| 617 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 618 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 619 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 620 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 621 | } |
| 622 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 623 | void assertFilterInputEventWasCalledInternal( |
| 624 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 625 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 626 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 627 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 628 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 629 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 630 | }; |
| 631 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 632 | // --- InputDispatcherTest --- |
| 633 | |
| 634 | class InputDispatcherTest : public testing::Test { |
| 635 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 636 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 637 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 638 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 639 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 640 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
| 641 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 642 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 643 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 644 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 647 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 648 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 649 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 650 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 651 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 652 | |
| 653 | /** |
| 654 | * Used for debugging when writing the test |
| 655 | */ |
| 656 | void dumpDispatcherState() { |
| 657 | std::string dump; |
| 658 | mDispatcher->dump(dump); |
| 659 | std::stringstream ss(dump); |
| 660 | std::string to; |
| 661 | |
| 662 | while (std::getline(ss, to, '\n')) { |
| 663 | ALOGE("%s", to.c_str()); |
| 664 | } |
| 665 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 666 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 667 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 668 | FocusRequest request; |
| 669 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 670 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 671 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 672 | request.displayId = window->getInfo()->displayId; |
| 673 | mDispatcher->setFocusedWindow(request); |
| 674 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | }; |
| 676 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 677 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 678 | KeyEvent event; |
| 679 | |
| 680 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 681 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 682 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 683 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 684 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 685 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 686 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 687 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 688 | << "Should reject key events with undefined action."; |
| 689 | |
| 690 | // 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] | 691 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 692 | 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] | 693 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 694 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 695 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 696 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 697 | << "Should reject key events with ACTION_MULTIPLE."; |
| 698 | } |
| 699 | |
| 700 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 701 | MotionEvent event; |
| 702 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 703 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 704 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 705 | pointerProperties[i].clear(); |
| 706 | pointerProperties[i].id = i; |
| 707 | pointerCoords[i].clear(); |
| 708 | } |
| 709 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 710 | // Some constants commonly used below |
| 711 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 712 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 713 | constexpr int32_t metaState = AMETA_NONE; |
| 714 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 715 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 716 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 717 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 718 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 719 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 720 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 721 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 722 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 723 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 724 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 725 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 726 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 727 | << "Should reject motion events with undefined action."; |
| 728 | |
| 729 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 730 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 731 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 732 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 733 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 734 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 735 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 736 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 737 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 738 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 739 | << "Should reject motion events with pointer down index too large."; |
| 740 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 741 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 742 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 743 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 744 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 745 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 746 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 747 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 748 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 749 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 750 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 751 | << "Should reject motion events with pointer down index too small."; |
| 752 | |
| 753 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 754 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 755 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 756 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 757 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 758 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 759 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 760 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 761 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 762 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 763 | << "Should reject motion events with pointer up index too large."; |
| 764 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 765 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 766 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 767 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 768 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 769 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 770 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 771 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 772 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 773 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 774 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 775 | << "Should reject motion events with pointer up index too small."; |
| 776 | |
| 777 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 778 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 779 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 780 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 781 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 782 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 783 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 784 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 785 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 786 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 787 | << "Should reject motion events with 0 pointers."; |
| 788 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 789 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 790 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 791 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 792 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 793 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 794 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 795 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 796 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 797 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 798 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 799 | |
| 800 | // Rejects motion events with invalid pointer ids. |
| 801 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 802 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 803 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 804 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 805 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 806 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 807 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 808 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 809 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 810 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 811 | << "Should reject motion events with pointer ids less than 0."; |
| 812 | |
| 813 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 814 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 815 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 816 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 817 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 818 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 819 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 820 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 821 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 822 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 823 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 824 | |
| 825 | // Rejects motion events with duplicate pointer ids. |
| 826 | pointerProperties[0].id = 1; |
| 827 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 828 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 829 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 830 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 831 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 832 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 833 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 834 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 835 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 836 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 837 | << "Should reject motion events with duplicate pointer ids."; |
| 838 | } |
| 839 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 840 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 841 | |
| 842 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 843 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 844 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 845 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 846 | |
| 847 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 848 | } |
| 849 | |
| 850 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 851 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 852 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 853 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 854 | |
| 855 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 856 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 857 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 858 | } |
| 859 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 860 | namespace { |
| 861 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 862 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 863 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 864 | // Default input dispatching timeout if there is no focused application or paused window |
| 865 | // from which to determine an appropriate dispatching timeout. |
| 866 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 867 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 868 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 869 | |
| 870 | class FakeApplicationHandle : public InputApplicationHandle { |
| 871 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 872 | FakeApplicationHandle() { |
| 873 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 874 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 875 | mInfo.dispatchingTimeoutMillis = |
| 876 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 877 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 878 | virtual ~FakeApplicationHandle() {} |
| 879 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 880 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 881 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 882 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 883 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 884 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 885 | }; |
| 886 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 887 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 888 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 889 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 890 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 891 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 894 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 895 | InputEvent* event; |
| 896 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 897 | if (!consumeSeq) { |
| 898 | return nullptr; |
| 899 | } |
| 900 | finishEvent(*consumeSeq); |
| 901 | return event; |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * Receive an event without acknowledging it. |
| 906 | * Return the sequence number that could later be used to send finished signal. |
| 907 | */ |
| 908 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 909 | uint32_t consumeSeq; |
| 910 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 911 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 912 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 913 | status_t status = WOULD_BLOCK; |
| 914 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 915 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 916 | &event); |
| 917 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 918 | if (elapsed > 100ms) { |
| 919 | break; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | if (status == WOULD_BLOCK) { |
| 924 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 925 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | if (status != OK) { |
| 929 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 930 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 931 | } |
| 932 | if (event == nullptr) { |
| 933 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 934 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 935 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 936 | if (outEvent != nullptr) { |
| 937 | *outEvent = event; |
| 938 | } |
| 939 | return consumeSeq; |
| 940 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 941 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 942 | /** |
| 943 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 944 | */ |
| 945 | void finishEvent(uint32_t consumeSeq) { |
| 946 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 947 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 948 | } |
| 949 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 950 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 951 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 952 | ASSERT_EQ(OK, status); |
| 953 | } |
| 954 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 955 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 956 | std::optional<int32_t> expectedDisplayId, |
| 957 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 958 | InputEvent* event = consume(); |
| 959 | |
| 960 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 961 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 962 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 963 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 964 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 965 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 966 | if (expectedDisplayId.has_value()) { |
| 967 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 968 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 969 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 970 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 971 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 972 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 973 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 974 | if (expectedFlags.has_value()) { |
| 975 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 976 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 977 | break; |
| 978 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 979 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 980 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 981 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 982 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 983 | if (expectedFlags.has_value()) { |
| 984 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 985 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 986 | break; |
| 987 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 988 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 989 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 990 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 991 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 992 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 993 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 994 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 995 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 996 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 997 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 998 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 999 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1000 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1003 | MotionEvent* consumeMotion() { |
| 1004 | InputEvent* event = consume(); |
| 1005 | |
| 1006 | if (event == nullptr) { |
| 1007 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 1008 | return nullptr; |
| 1009 | } |
| 1010 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1011 | if (event->getType() != InputEventType::MOTION) { |
| 1012 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1013 | return nullptr; |
| 1014 | } |
| 1015 | return static_cast<MotionEvent*>(event); |
| 1016 | } |
| 1017 | |
| 1018 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1019 | MotionEvent* motionEvent = consumeMotion(); |
| 1020 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1021 | ASSERT_THAT(*motionEvent, matcher); |
| 1022 | } |
| 1023 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1024 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 1025 | InputEvent* event = consume(); |
| 1026 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1027 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1028 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1029 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1030 | |
| 1031 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1032 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1033 | |
| 1034 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1035 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1038 | void consumeCaptureEvent(bool hasCapture) { |
| 1039 | const InputEvent* event = consume(); |
| 1040 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1041 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1042 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1043 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1044 | |
| 1045 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1046 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1047 | |
| 1048 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1049 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1050 | } |
| 1051 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1052 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1053 | const InputEvent* event = consume(); |
| 1054 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1055 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1056 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1057 | |
| 1058 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1059 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1060 | |
| 1061 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1062 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1063 | EXPECT_EQ(x, dragEvent.getX()); |
| 1064 | EXPECT_EQ(y, dragEvent.getY()); |
| 1065 | } |
| 1066 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1067 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1068 | const InputEvent* event = consume(); |
| 1069 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1070 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1071 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1072 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1073 | |
| 1074 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1075 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1076 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1077 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1078 | } |
| 1079 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1080 | void assertNoEvents() { |
| 1081 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1082 | if (event == nullptr) { |
| 1083 | return; |
| 1084 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1085 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1086 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1087 | ADD_FAILURE() << "Received key event " |
| 1088 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1089 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1090 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1091 | ADD_FAILURE() << "Received motion event " |
| 1092 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1093 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1094 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1095 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1096 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1097 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1098 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1099 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1100 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1101 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1102 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1103 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1104 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1105 | } |
| 1106 | FAIL() << mName.c_str() |
| 1107 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1111 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1112 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1113 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1114 | protected: |
| 1115 | std::unique_ptr<InputConsumer> mConsumer; |
| 1116 | PreallocatedInputEventFactory mEventFactory; |
| 1117 | |
| 1118 | std::string mName; |
| 1119 | }; |
| 1120 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1121 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1122 | public: |
| 1123 | static const int32_t WIDTH = 600; |
| 1124 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1125 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1126 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1127 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1128 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1129 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1130 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1131 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1132 | dispatcher->createInputChannel(name); |
| 1133 | token = (*channel)->getConnectionToken(); |
| 1134 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | inputApplicationHandle->updateInfo(); |
| 1138 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1139 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1140 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1141 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1142 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1143 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1144 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1145 | mInfo.frameLeft = 0; |
| 1146 | mInfo.frameTop = 0; |
| 1147 | mInfo.frameRight = WIDTH; |
| 1148 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1149 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1150 | mInfo.globalScaleFactor = 1.0; |
| 1151 | mInfo.touchableRegion.clear(); |
| 1152 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1153 | mInfo.ownerPid = WINDOW_PID; |
| 1154 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1155 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1156 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1159 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1160 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1161 | handle->mInfo = mInfo; |
| 1162 | handle->mInfo.displayId = displayId; |
| 1163 | handle->mInfo.id = sId++; |
| 1164 | handle->mInputReceiver = mInputReceiver; |
| 1165 | return handle; |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * This is different from clone, because clone will make a "mirror" window - a window with the |
| 1170 | * same token, but a different ID. The original window and the clone window are allowed to be |
| 1171 | * sent to the dispatcher at the same time - they can coexist inside the dispatcher. |
| 1172 | * This function will create a different object of WindowInfoHandle, but with the same |
| 1173 | * properties as the original object - including the ID. |
| 1174 | * You can use either the old or the new object to consume the events. |
| 1175 | * IMPORTANT: The duplicated object is supposed to replace the original object, and not appear |
| 1176 | * at the same time inside dispatcher. |
| 1177 | */ |
| 1178 | sp<FakeWindowHandle> duplicate() { |
| 1179 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mName); |
| 1180 | handle->mInfo = mInfo; |
| 1181 | handle->mInputReceiver = mInputReceiver; |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1182 | return handle; |
| 1183 | } |
| 1184 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1185 | void setTouchable(bool touchable) { |
| 1186 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1187 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1188 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1189 | void setFocusable(bool focusable) { |
| 1190 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1191 | } |
| 1192 | |
| 1193 | void setVisible(bool visible) { |
| 1194 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1195 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1196 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1197 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1198 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1201 | void setPaused(bool paused) { |
| 1202 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1203 | } |
| 1204 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1205 | void setPreventSplitting(bool preventSplitting) { |
| 1206 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | void setSlippery(bool slippery) { |
| 1210 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1211 | } |
| 1212 | |
| 1213 | void setWatchOutsideTouch(bool watchOutside) { |
| 1214 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1215 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1216 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1217 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1218 | |
| 1219 | void setInterceptsStylus(bool interceptsStylus) { |
| 1220 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1221 | } |
| 1222 | |
| 1223 | void setDropInput(bool dropInput) { |
| 1224 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1225 | } |
| 1226 | |
| 1227 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1228 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1229 | } |
| 1230 | |
| 1231 | void setNoInputChannel(bool noInputChannel) { |
| 1232 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1233 | } |
| 1234 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1235 | void setDisableUserActivity(bool disableUserActivity) { |
| 1236 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1237 | } |
| 1238 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1239 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1240 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1241 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1242 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1243 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1244 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1245 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1246 | mInfo.frameLeft = frame.left; |
| 1247 | mInfo.frameTop = frame.top; |
| 1248 | mInfo.frameRight = frame.right; |
| 1249 | mInfo.frameBottom = frame.bottom; |
| 1250 | mInfo.touchableRegion.clear(); |
| 1251 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1252 | |
| 1253 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1254 | ui::Transform translate; |
| 1255 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1256 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1259 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1260 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1261 | void setIsWallpaper(bool isWallpaper) { |
| 1262 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1263 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1264 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1265 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1266 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1267 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1268 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1269 | void setTrustedOverlay(bool trustedOverlay) { |
| 1270 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1271 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1272 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1273 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1274 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1275 | } |
| 1276 | |
| 1277 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1278 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1279 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1280 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1281 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1282 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1285 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1286 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1289 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1290 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1291 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1292 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1296 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1297 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1298 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1302 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1303 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1304 | } |
| 1305 | |
| 1306 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1307 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1308 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1309 | expectedFlags); |
| 1310 | } |
| 1311 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1312 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1313 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1314 | int32_t expectedFlags = 0) { |
| 1315 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1316 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1317 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1321 | int32_t expectedFlags = 0) { |
| 1322 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1323 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1324 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1328 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1329 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1330 | expectedFlags); |
| 1331 | } |
| 1332 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1333 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1334 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1335 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1336 | expectedFlags); |
| 1337 | } |
| 1338 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1339 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1340 | int32_t expectedFlags = 0) { |
| 1341 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1342 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1343 | ASSERT_EQ(InputEventType::MOTION, event->getType()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1344 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1345 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1346 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1347 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1348 | } |
| 1349 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1350 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1351 | ASSERT_NE(mInputReceiver, nullptr) |
| 1352 | << "Cannot consume events from a window with no receiver"; |
| 1353 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1354 | } |
| 1355 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1356 | void consumeCaptureEvent(bool hasCapture) { |
| 1357 | ASSERT_NE(mInputReceiver, nullptr) |
| 1358 | << "Cannot consume events from a window with no receiver"; |
| 1359 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1360 | } |
| 1361 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1362 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1363 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1364 | 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] | 1365 | ASSERT_THAT(*motionEvent, matcher); |
| 1366 | } |
| 1367 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1368 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1369 | std::optional<int32_t> expectedDisplayId, |
| 1370 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1371 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1372 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1373 | expectedFlags); |
| 1374 | } |
| 1375 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1376 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1377 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1378 | } |
| 1379 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1380 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1381 | ASSERT_NE(mInputReceiver, nullptr) |
| 1382 | << "Cannot consume events from a window with no receiver"; |
| 1383 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1384 | } |
| 1385 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1386 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1387 | if (mInputReceiver == nullptr) { |
| 1388 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1389 | return std::nullopt; |
| 1390 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1391 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | void finishEvent(uint32_t sequenceNum) { |
| 1395 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1396 | mInputReceiver->finishEvent(sequenceNum); |
| 1397 | } |
| 1398 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1399 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1400 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1401 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1402 | } |
| 1403 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1404 | InputEvent* consume() { |
| 1405 | if (mInputReceiver == nullptr) { |
| 1406 | return nullptr; |
| 1407 | } |
| 1408 | return mInputReceiver->consume(); |
| 1409 | } |
| 1410 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1411 | MotionEvent* consumeMotion() { |
| 1412 | InputEvent* event = consume(); |
| 1413 | if (event == nullptr) { |
| 1414 | ADD_FAILURE() << "Consume failed : no event"; |
| 1415 | return nullptr; |
| 1416 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1417 | if (event->getType() != InputEventType::MOTION) { |
| 1418 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1419 | return nullptr; |
| 1420 | } |
| 1421 | return static_cast<MotionEvent*>(event); |
| 1422 | } |
| 1423 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1424 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1425 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1426 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1427 | return; // Can't receive events if the window does not have input channel |
| 1428 | } |
| 1429 | ASSERT_NE(nullptr, mInputReceiver) |
| 1430 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1431 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1432 | } |
| 1433 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1434 | sp<IBinder> getToken() { return mInfo.token; } |
| 1435 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1436 | const std::string& getName() { return mName; } |
| 1437 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 1438 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1439 | mInfo.ownerPid = ownerPid; |
| 1440 | mInfo.ownerUid = ownerUid; |
| 1441 | } |
| 1442 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 1443 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1444 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1445 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1446 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1447 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1448 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1449 | private: |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1450 | FakeWindowHandle(std::string name) : mName(name){}; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1451 | const std::string mName; |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1452 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1453 | 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] | 1454 | friend class sp<FakeWindowHandle>; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1455 | }; |
| 1456 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1457 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1458 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1459 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1460 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1461 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1462 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1463 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1464 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1465 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1466 | KeyEvent event; |
| 1467 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1468 | |
| 1469 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1470 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1471 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1472 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1473 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1474 | if (!allowKeyRepeat) { |
| 1475 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1476 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1477 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1478 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1481 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1482 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1483 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1486 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1487 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1488 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1489 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1490 | const std::unique_ptr<InputDispatcher>& dispatcher, 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, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1492 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1493 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1496 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1497 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1498 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1501 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1502 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1503 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1504 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1505 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1506 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1507 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1510 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1511 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1512 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1513 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1514 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1515 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1516 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1517 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1518 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1519 | MotionEventBuilder motionBuilder = |
| 1520 | MotionEventBuilder(action, source) |
| 1521 | .displayId(displayId) |
| 1522 | .eventTime(eventTime) |
| 1523 | .rawXCursorPosition(cursorPosition.x) |
| 1524 | .rawYCursorPosition(cursorPosition.y) |
| 1525 | .pointer( |
| 1526 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1527 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1528 | motionBuilder.downTime(eventTime); |
| 1529 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1530 | |
| 1531 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1532 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1533 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1536 | static InputEventInjectionResult injectMotionDown( |
| 1537 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1538 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1539 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1542 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1543 | int32_t source, int32_t displayId, |
| 1544 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1545 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1548 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1549 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1550 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1551 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1552 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1553 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1554 | |
| 1555 | return args; |
| 1556 | } |
| 1557 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1558 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1559 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1560 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1561 | // Define a valid key event. |
| 1562 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1563 | displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON, |
| 1564 | currentTime); |
| 1565 | |
| 1566 | return args; |
| 1567 | } |
| 1568 | |
| 1569 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1570 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1571 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1572 | // Define a valid key event. |
| 1573 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1574 | displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT, |
| 1575 | AMETA_NONE, currentTime); |
| 1576 | |
| 1577 | return args; |
| 1578 | } |
| 1579 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1580 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1581 | int32_t displayId, |
| 1582 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1583 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1584 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1585 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1586 | } |
| 1587 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1588 | PointerProperties pointerProperties[pointerCount]; |
| 1589 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1590 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1591 | for (size_t i = 0; i < pointerCount; i++) { |
| 1592 | pointerProperties[i].clear(); |
| 1593 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1594 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1595 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1596 | pointerCoords[i].clear(); |
| 1597 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1598 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1599 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1600 | |
| 1601 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1602 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1603 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1604 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1605 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1606 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1607 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1608 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1609 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1610 | |
| 1611 | return args; |
| 1612 | } |
| 1613 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1614 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1615 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1616 | } |
| 1617 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1618 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1619 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1620 | } |
| 1621 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1622 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1623 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1624 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1627 | } // namespace |
| 1628 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1629 | /** |
| 1630 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1631 | * broken channel. |
| 1632 | */ |
| 1633 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1634 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1635 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1636 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1637 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1638 | |
| 1639 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1640 | |
| 1641 | // Window closes its channel, but the window remains. |
| 1642 | window->destroyReceiver(); |
| 1643 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1644 | } |
| 1645 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1646 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1647 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1648 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1649 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1650 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1651 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1653 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1654 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1655 | |
| 1656 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1657 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1658 | } |
| 1659 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1660 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1661 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1662 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1663 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1664 | |
| 1665 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1666 | // Inject a MotionEvent to an unknown display. |
| 1667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1668 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1670 | |
| 1671 | // Window should receive motion event. |
| 1672 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1673 | } |
| 1674 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1675 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1676 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1677 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1678 | * called twice. |
| 1679 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1680 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1681 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1682 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1683 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1684 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1685 | |
| 1686 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1687 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1688 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1689 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1690 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1691 | |
| 1692 | // Window should receive motion event. |
| 1693 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1694 | } |
| 1695 | |
| 1696 | /** |
| 1697 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1698 | */ |
| 1699 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1700 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1701 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1702 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1703 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1704 | |
| 1705 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1706 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1708 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1709 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1710 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1711 | |
| 1712 | // Window should receive motion event. |
| 1713 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1714 | } |
| 1715 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1716 | // The foreground window should receive the first touch down event. |
| 1717 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1718 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1719 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1720 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1721 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1722 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1723 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1724 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1725 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1726 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1727 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1728 | |
| 1729 | // 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] | 1730 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1731 | windowSecond->assertNoEvents(); |
| 1732 | } |
| 1733 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1734 | /** |
| 1735 | * Two windows: A top window, and a wallpaper behind the window. |
| 1736 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1737 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1738 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1739 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1740 | */ |
| 1741 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1742 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1743 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1744 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1745 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1746 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1747 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1748 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1749 | |
| 1750 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1751 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1752 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1753 | {100, 200})) |
| 1754 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1755 | |
| 1756 | // Both foreground window and its wallpaper should receive the touch down |
| 1757 | foregroundWindow->consumeMotionDown(); |
| 1758 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1759 | |
| 1760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1761 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1762 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1763 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1764 | |
| 1765 | foregroundWindow->consumeMotionMove(); |
| 1766 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1767 | |
| 1768 | // Now the foreground window goes away, but the wallpaper stays |
| 1769 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1770 | foregroundWindow->consumeMotionCancel(); |
| 1771 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1772 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1773 | } |
| 1774 | |
| 1775 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1776 | * Two fingers down on the window, and lift off the first finger. |
| 1777 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1778 | * contains a single pointer. |
| 1779 | */ |
| 1780 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1781 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1782 | sp<FakeWindowHandle> window = |
| 1783 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1784 | |
| 1785 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1786 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1787 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1788 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1789 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1790 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1791 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1792 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1793 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1794 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1795 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1796 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1797 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1798 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1799 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1800 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1801 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1802 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1803 | |
| 1804 | // Remove the window. The gesture should be canceled |
| 1805 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 1806 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1807 | window->consumeMotionEvent( |
| 1808 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1809 | } |
| 1810 | |
| 1811 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1812 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1813 | * with the following differences: |
| 1814 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1815 | * clean up the connection. |
| 1816 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1817 | * Ensure that there's no crash in the dispatcher. |
| 1818 | */ |
| 1819 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1820 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1821 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1822 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1823 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1824 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1825 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1826 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1827 | |
| 1828 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1829 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1830 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1831 | {100, 200})) |
| 1832 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1833 | |
| 1834 | // Both foreground window and its wallpaper should receive the touch down |
| 1835 | foregroundWindow->consumeMotionDown(); |
| 1836 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1837 | |
| 1838 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1839 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1840 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1841 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1842 | |
| 1843 | foregroundWindow->consumeMotionMove(); |
| 1844 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1845 | |
| 1846 | // Wallpaper closes its channel, but the window remains. |
| 1847 | wallpaperWindow->destroyReceiver(); |
| 1848 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1849 | |
| 1850 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1851 | // is no longer valid. |
| 1852 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1853 | foregroundWindow->consumeMotionCancel(); |
| 1854 | } |
| 1855 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1856 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1857 | public ::testing::WithParamInterface<bool> {}; |
| 1858 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1859 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1860 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1861 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1862 | * The top window gets a multitouch gesture. |
| 1863 | * Ensure that wallpaper gets the same gesture. |
| 1864 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1865 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1866 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1867 | sp<FakeWindowHandle> foregroundWindow = |
| 1868 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1869 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1870 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1871 | |
| 1872 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1873 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1874 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1875 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1876 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1877 | |
| 1878 | // Touch down on top window |
| 1879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1880 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1881 | {100, 100})) |
| 1882 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1883 | |
| 1884 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1885 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1886 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1887 | |
| 1888 | // Second finger down on the top window |
| 1889 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1890 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1891 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1892 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1893 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1894 | .build(); |
| 1895 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1896 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1897 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1898 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1899 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1900 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1901 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1902 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1903 | |
| 1904 | const MotionEvent secondFingerUpEvent = |
| 1905 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1906 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1907 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1908 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1909 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1910 | .build(); |
| 1911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1912 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1913 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1914 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1915 | foregroundWindow->consumeMotionPointerUp(0); |
| 1916 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1917 | |
| 1918 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1919 | injectMotionEvent(mDispatcher, |
| 1920 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1921 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1922 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1923 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1924 | .pointer(PointerBuilder(/* id */ 1, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1925 | ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1926 | .x(100) |
| 1927 | .y(100)) |
| 1928 | .build(), |
| 1929 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1930 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1931 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1932 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /** |
| 1936 | * Two windows: a window on the left and window on the right. |
| 1937 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1938 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1939 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1940 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1941 | * ACTION_POINTER_DOWN(1). |
| 1942 | */ |
| 1943 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1944 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1945 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1947 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1948 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1949 | |
| 1950 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1951 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1952 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1953 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1954 | |
| 1955 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1956 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1957 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1958 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1959 | |
| 1960 | mDispatcher->setInputWindows( |
| 1961 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1962 | |
| 1963 | // Touch down on left window |
| 1964 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1965 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1966 | {100, 100})) |
| 1967 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1968 | |
| 1969 | // Both foreground window and its wallpaper should receive the touch down |
| 1970 | leftWindow->consumeMotionDown(); |
| 1971 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1972 | |
| 1973 | // Second finger down on the right window |
| 1974 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1975 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1976 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1977 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1978 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1979 | .build(); |
| 1980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1981 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1982 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1983 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1984 | |
| 1985 | leftWindow->consumeMotionMove(); |
| 1986 | // Since the touch is split, right window gets ACTION_DOWN |
| 1987 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1988 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1989 | expectedWallpaperFlags); |
| 1990 | |
| 1991 | // Now, leftWindow, which received the first finger, disappears. |
| 1992 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1993 | leftWindow->consumeMotionCancel(); |
| 1994 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1995 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1996 | |
| 1997 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1998 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1999 | const MotionEvent secondFingerMoveEvent = |
| 2000 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2001 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2002 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2003 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2004 | .build(); |
| 2005 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2006 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 2007 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2008 | rightWindow->consumeMotionMove(); |
| 2009 | |
| 2010 | leftWindow->assertNoEvents(); |
| 2011 | rightWindow->assertNoEvents(); |
| 2012 | wallpaperWindow->assertNoEvents(); |
| 2013 | } |
| 2014 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2015 | /** |
| 2016 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2017 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2018 | * The right window should receive ACTION_DOWN. |
| 2019 | */ |
| 2020 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2021 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2022 | sp<FakeWindowHandle> leftWindow = |
| 2023 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2024 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2025 | leftWindow->setDupTouchToWallpaper(true); |
| 2026 | leftWindow->setSlippery(true); |
| 2027 | |
| 2028 | sp<FakeWindowHandle> rightWindow = |
| 2029 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2030 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2031 | |
| 2032 | sp<FakeWindowHandle> wallpaperWindow = |
| 2033 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2034 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2035 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2036 | mDispatcher->setInputWindows( |
| 2037 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2038 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2039 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2041 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2042 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2043 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2044 | |
| 2045 | // Both foreground window and its wallpaper should receive the touch down |
| 2046 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2047 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2048 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2049 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2050 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2051 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2052 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2053 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2054 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2055 | leftWindow->consumeMotionCancel(); |
| 2056 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2057 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2060 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2061 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2062 | * interactive, it might stop sending this flag. |
| 2063 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2064 | * to have a consistent input stream. |
| 2065 | * |
| 2066 | * Test procedure: |
| 2067 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2068 | * DOWN (new gesture). |
| 2069 | * |
| 2070 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2071 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2072 | * |
| 2073 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2074 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2075 | */ |
| 2076 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2077 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2078 | sp<FakeWindowHandle> spyWindow = |
| 2079 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2080 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2081 | spyWindow->setTrustedOverlay(true); |
| 2082 | spyWindow->setSpy(true); |
| 2083 | |
| 2084 | sp<FakeWindowHandle> window = |
| 2085 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2086 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2087 | |
| 2088 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2089 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2090 | |
| 2091 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2092 | mDispatcher->notifyMotion( |
| 2093 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2094 | .deviceId(touchDeviceId) |
| 2095 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2096 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2097 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2098 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2099 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2100 | .deviceId(touchDeviceId) |
| 2101 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2102 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2103 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2104 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2105 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2106 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2107 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2108 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2109 | |
| 2110 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2111 | mDispatcher->notifyMotion( |
| 2112 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2113 | .deviceId(touchDeviceId) |
| 2114 | .policyFlags(0) |
| 2115 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2116 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2117 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2118 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2119 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2120 | |
| 2121 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2122 | // 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] | 2123 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2124 | |
| 2125 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2126 | mDispatcher->notifyMotion( |
| 2127 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2128 | .deviceId(touchDeviceId) |
| 2129 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2130 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2131 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2132 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2133 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2134 | |
| 2135 | // No more events |
| 2136 | spyWindow->assertNoEvents(); |
| 2137 | window->assertNoEvents(); |
| 2138 | } |
| 2139 | |
| 2140 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2141 | * Two windows: a window on the left and a window on the right. |
| 2142 | * Mouse is hovered from the right window into the left window. |
| 2143 | * Next, we tap on the left window, where the cursor was last seen. |
| 2144 | * The second tap is done onto the right window. |
| 2145 | * The mouse and tap are from two different devices. |
| 2146 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2147 | * explicitly helps during debugging. |
| 2148 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2149 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2150 | */ |
| 2151 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2152 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2153 | sp<FakeWindowHandle> leftWindow = |
| 2154 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2155 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2156 | |
| 2157 | sp<FakeWindowHandle> rightWindow = |
| 2158 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2159 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2160 | |
| 2161 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2162 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2163 | // stale. |
| 2164 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2165 | const int32_t mouseDeviceId = 6; |
| 2166 | const int32_t touchDeviceId = 4; |
| 2167 | // Move the cursor from right |
| 2168 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2169 | injectMotionEvent(mDispatcher, |
| 2170 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2171 | AINPUT_SOURCE_MOUSE) |
| 2172 | .deviceId(mouseDeviceId) |
| 2173 | .downTime(baseTime + 10) |
| 2174 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2175 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2176 | .x(300) |
| 2177 | .y(100)) |
| 2178 | .build())); |
| 2179 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2180 | |
| 2181 | // .. to the left window |
| 2182 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2183 | injectMotionEvent(mDispatcher, |
| 2184 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2185 | AINPUT_SOURCE_MOUSE) |
| 2186 | .deviceId(mouseDeviceId) |
| 2187 | .downTime(baseTime + 10) |
| 2188 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2189 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2190 | .x(110) |
| 2191 | .y(100)) |
| 2192 | .build())); |
| 2193 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2194 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2195 | // Now tap the left window |
| 2196 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2197 | injectMotionEvent(mDispatcher, |
| 2198 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2199 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2200 | .deviceId(touchDeviceId) |
| 2201 | .downTime(baseTime + 40) |
| 2202 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2203 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2204 | .x(100) |
| 2205 | .y(100)) |
| 2206 | .build())); |
| 2207 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2208 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2209 | |
| 2210 | // release tap |
| 2211 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2212 | injectMotionEvent(mDispatcher, |
| 2213 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2214 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2215 | .deviceId(touchDeviceId) |
| 2216 | .downTime(baseTime + 40) |
| 2217 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2218 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2219 | .x(100) |
| 2220 | .y(100)) |
| 2221 | .build())); |
| 2222 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2223 | |
| 2224 | // Tap the window on the right |
| 2225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2226 | injectMotionEvent(mDispatcher, |
| 2227 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2228 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2229 | .deviceId(touchDeviceId) |
| 2230 | .downTime(baseTime + 60) |
| 2231 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2232 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2233 | .x(300) |
| 2234 | .y(100)) |
| 2235 | .build())); |
| 2236 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2237 | |
| 2238 | // release tap |
| 2239 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2240 | injectMotionEvent(mDispatcher, |
| 2241 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2242 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2243 | .deviceId(touchDeviceId) |
| 2244 | .downTime(baseTime + 60) |
| 2245 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2246 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2247 | .x(300) |
| 2248 | .y(100)) |
| 2249 | .build())); |
| 2250 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2251 | |
| 2252 | // No more events |
| 2253 | leftWindow->assertNoEvents(); |
| 2254 | rightWindow->assertNoEvents(); |
| 2255 | } |
| 2256 | |
| 2257 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2258 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2259 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2260 | * While the top window is present, the hovering is stopped. |
| 2261 | * Later, hovering gets resumed again. |
| 2262 | * Ensure that new hover gesture is handled correctly. |
| 2263 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2264 | * to the window that's currently being hovered over. |
| 2265 | */ |
| 2266 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2267 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2268 | sp<FakeWindowHandle> window = |
| 2269 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2270 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2271 | |
| 2272 | // Only a single window is present at first |
| 2273 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2274 | |
| 2275 | // Start hovering in the window |
| 2276 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2277 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2278 | .build()); |
| 2279 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2280 | |
| 2281 | // Now, an obscuring window appears! |
| 2282 | sp<FakeWindowHandle> obscuringWindow = |
| 2283 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2284 | ADISPLAY_ID_DEFAULT, |
| 2285 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2286 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2287 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2288 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2289 | obscuringWindow->setNoInputChannel(true); |
| 2290 | obscuringWindow->setFocusable(false); |
| 2291 | obscuringWindow->setAlpha(1.0); |
| 2292 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2293 | |
| 2294 | // While this new obscuring window is present, the hovering is stopped |
| 2295 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2296 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2297 | .build()); |
| 2298 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2299 | |
| 2300 | // Now the obscuring window goes away. |
| 2301 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2302 | |
| 2303 | // And a new hover gesture starts. |
| 2304 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2305 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2306 | .build()); |
| 2307 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2308 | } |
| 2309 | |
| 2310 | /** |
| 2311 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2312 | * the obscuring window. |
| 2313 | */ |
| 2314 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2315 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2316 | sp<FakeWindowHandle> window = |
| 2317 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2318 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2319 | |
| 2320 | // Only a single window is present at first |
| 2321 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2322 | |
| 2323 | // Start hovering in the window |
| 2324 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2325 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2326 | .build()); |
| 2327 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2328 | |
| 2329 | // Now, an obscuring window appears! |
| 2330 | sp<FakeWindowHandle> obscuringWindow = |
| 2331 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2332 | ADISPLAY_ID_DEFAULT, |
| 2333 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2334 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2335 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2336 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2337 | obscuringWindow->setNoInputChannel(true); |
| 2338 | obscuringWindow->setFocusable(false); |
| 2339 | obscuringWindow->setAlpha(1.0); |
| 2340 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2341 | |
| 2342 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2343 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2344 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2345 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2346 | .build()); |
| 2347 | obscuringWindow->assertNoEvents(); |
| 2348 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2349 | |
| 2350 | // Now the obscuring window goes away. |
| 2351 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2352 | |
| 2353 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2354 | // so it should generate a HOVER_ENTER |
| 2355 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2356 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2357 | .build()); |
| 2358 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2359 | |
| 2360 | // Now the MOVE should be getting dispatched normally |
| 2361 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2362 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2363 | .build()); |
| 2364 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2365 | } |
| 2366 | |
| 2367 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2368 | * Two windows: a window on the left and a window on the right. |
| 2369 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2370 | * down. Then, on the left window, also place second touch pointer down. |
| 2371 | * This test tries to reproduce a crash. |
| 2372 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2373 | */ |
| 2374 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { |
| 2375 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2376 | sp<FakeWindowHandle> leftWindow = |
| 2377 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2378 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2379 | |
| 2380 | sp<FakeWindowHandle> rightWindow = |
| 2381 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2382 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2383 | |
| 2384 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2385 | |
| 2386 | const int32_t touchDeviceId = 4; |
| 2387 | const int32_t mouseDeviceId = 6; |
| 2388 | NotifyMotionArgs args; |
| 2389 | |
| 2390 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2391 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2392 | .deviceId(mouseDeviceId) |
| 2393 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2394 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2395 | leftWindow->consumeMotionEvent( |
| 2396 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2397 | |
| 2398 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2399 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2400 | .deviceId(mouseDeviceId) |
| 2401 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2402 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2403 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2404 | |
| 2405 | leftWindow->consumeMotionEvent( |
| 2406 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2407 | leftWindow->consumeMotionEvent( |
| 2408 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2409 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2410 | mDispatcher->notifyMotion( |
| 2411 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2412 | .deviceId(mouseDeviceId) |
| 2413 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2414 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2415 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2416 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2417 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2418 | |
| 2419 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2420 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2421 | .deviceId(touchDeviceId) |
| 2422 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2423 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2424 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2425 | |
| 2426 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2427 | |
| 2428 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2429 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2430 | .deviceId(touchDeviceId) |
| 2431 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2432 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2433 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2434 | leftWindow->consumeMotionEvent( |
| 2435 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2436 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2437 | // current implementation. |
| 2438 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2439 | rightWindow->consumeMotionEvent( |
| 2440 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2441 | |
| 2442 | leftWindow->assertNoEvents(); |
| 2443 | rightWindow->assertNoEvents(); |
| 2444 | } |
| 2445 | |
| 2446 | /** |
| 2447 | * On a single window, use two different devices: mouse and touch. |
| 2448 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2449 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2450 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2451 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2452 | * represent a new gesture. |
| 2453 | */ |
| 2454 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { |
| 2455 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2456 | sp<FakeWindowHandle> window = |
| 2457 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2458 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2459 | |
| 2460 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2461 | |
| 2462 | const int32_t touchDeviceId = 4; |
| 2463 | const int32_t mouseDeviceId = 6; |
| 2464 | NotifyMotionArgs args; |
| 2465 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2466 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2467 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2468 | .deviceId(touchDeviceId) |
| 2469 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2470 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2471 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2472 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2473 | .deviceId(touchDeviceId) |
| 2474 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2475 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2476 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2477 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2478 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2479 | .deviceId(touchDeviceId) |
| 2480 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2481 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2482 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2483 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2484 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2485 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2486 | |
| 2487 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2488 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2489 | .deviceId(mouseDeviceId) |
| 2490 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2491 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2492 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2493 | |
| 2494 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2495 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2496 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2497 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2498 | mDispatcher->notifyMotion( |
| 2499 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2500 | .deviceId(mouseDeviceId) |
| 2501 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2502 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2503 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2504 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2505 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2506 | |
| 2507 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2508 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2509 | .deviceId(touchDeviceId) |
| 2510 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2511 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2512 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2513 | // The pointer_down event should be ignored |
| 2514 | window->assertNoEvents(); |
| 2515 | } |
| 2516 | |
| 2517 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2518 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 2519 | * the injected event. |
| 2520 | */ |
| 2521 | TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) { |
| 2522 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2523 | sp<FakeWindowHandle> window = |
| 2524 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2525 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2526 | |
| 2527 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2528 | |
| 2529 | const int32_t touchDeviceId = 4; |
| 2530 | NotifyMotionArgs args; |
| 2531 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 2532 | // completion. |
| 2533 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2534 | injectMotionEvent(mDispatcher, |
| 2535 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2536 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2537 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2538 | .x(50) |
| 2539 | .y(50)) |
| 2540 | .build())); |
| 2541 | window->consumeMotionEvent( |
| 2542 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2543 | |
| 2544 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 2545 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2546 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2547 | .deviceId(touchDeviceId) |
| 2548 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2549 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2550 | |
| 2551 | window->consumeMotionEvent( |
| 2552 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2553 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2554 | } |
| 2555 | |
| 2556 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2557 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2558 | * |
| 2559 | * Two windows: a window on the left and a window on the right. |
| 2560 | * Mouse is hovered over the left window. |
| 2561 | * Next, we tap on the left window, where the cursor was last seen. |
| 2562 | * |
| 2563 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2564 | * the left window. |
| 2565 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2566 | * window (first), and then another on the left window (second). |
| 2567 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2568 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2569 | */ |
| 2570 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2571 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2572 | sp<FakeWindowHandle> leftWindow = |
| 2573 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2574 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2575 | |
| 2576 | sp<FakeWindowHandle> rightWindow = |
| 2577 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2578 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2579 | |
| 2580 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2581 | |
| 2582 | const int32_t mouseDeviceId = 6; |
| 2583 | const int32_t touchDeviceId = 4; |
| 2584 | // Hover over the left window. Keep the cursor there. |
| 2585 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2586 | injectMotionEvent(mDispatcher, |
| 2587 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2588 | AINPUT_SOURCE_MOUSE) |
| 2589 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2590 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2591 | .x(50) |
| 2592 | .y(50)) |
| 2593 | .build())); |
| 2594 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2595 | |
| 2596 | // Tap on left window |
| 2597 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2598 | injectMotionEvent(mDispatcher, |
| 2599 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2600 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2601 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2602 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2603 | .x(100) |
| 2604 | .y(100)) |
| 2605 | .build())); |
| 2606 | |
| 2607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2608 | injectMotionEvent(mDispatcher, |
| 2609 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2610 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2611 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2612 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2613 | .x(100) |
| 2614 | .y(100)) |
| 2615 | .build())); |
| 2616 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2617 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2618 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2619 | |
| 2620 | // First finger down on right window |
| 2621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2622 | injectMotionEvent(mDispatcher, |
| 2623 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2624 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2625 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2626 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2627 | .x(300) |
| 2628 | .y(100)) |
| 2629 | .build())); |
| 2630 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2631 | |
| 2632 | // Second finger down on the left window |
| 2633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2634 | injectMotionEvent(mDispatcher, |
| 2635 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2636 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2637 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2638 | .x(300) |
| 2639 | .y(100)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2640 | .pointer(PointerBuilder(1, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2641 | .x(100) |
| 2642 | .y(100)) |
| 2643 | .build())); |
| 2644 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2645 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2646 | |
| 2647 | // No more events |
| 2648 | leftWindow->assertNoEvents(); |
| 2649 | rightWindow->assertNoEvents(); |
| 2650 | } |
| 2651 | |
| 2652 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2653 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2654 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2655 | * touch is gone, stylus hovering should start working again. |
| 2656 | */ |
| 2657 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 2658 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2659 | sp<FakeWindowHandle> window = |
| 2660 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2661 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2662 | |
| 2663 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2664 | |
| 2665 | const int32_t stylusDeviceId = 5; |
| 2666 | const int32_t touchDeviceId = 4; |
| 2667 | // Start hovering with stylus |
| 2668 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2669 | injectMotionEvent(mDispatcher, |
| 2670 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2671 | AINPUT_SOURCE_STYLUS) |
| 2672 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2673 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2674 | .x(50) |
| 2675 | .y(50)) |
| 2676 | .build())); |
| 2677 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2678 | |
| 2679 | // Finger down on the window |
| 2680 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2681 | injectMotionEvent(mDispatcher, |
| 2682 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2683 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2684 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2685 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2686 | .x(100) |
| 2687 | .y(100)) |
| 2688 | .build())); |
| 2689 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2690 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2691 | |
| 2692 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2693 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2694 | injectMotionEvent(mDispatcher, |
| 2695 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2696 | AINPUT_SOURCE_STYLUS) |
| 2697 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2698 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2699 | .x(50) |
| 2700 | .y(50)) |
| 2701 | .build())); |
| 2702 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2703 | // is already down. |
| 2704 | |
| 2705 | // Lift up the finger |
| 2706 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2707 | injectMotionEvent(mDispatcher, |
| 2708 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2709 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2710 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2711 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2712 | .x(100) |
| 2713 | .y(100)) |
| 2714 | .build())); |
| 2715 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2716 | |
| 2717 | // Now that the touch is gone, stylus hovering should start working again |
| 2718 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2719 | injectMotionEvent(mDispatcher, |
| 2720 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2721 | AINPUT_SOURCE_STYLUS) |
| 2722 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2723 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2724 | .x(50) |
| 2725 | .y(50)) |
| 2726 | .build())); |
| 2727 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2728 | // No more events |
| 2729 | window->assertNoEvents(); |
| 2730 | } |
| 2731 | |
| 2732 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2733 | * A spy window above a window with no input channel. |
| 2734 | * Start hovering with a stylus device, and then tap with it. |
| 2735 | * Ensure spy window receives the entire sequence. |
| 2736 | */ |
| 2737 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 2738 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2739 | sp<FakeWindowHandle> spyWindow = |
| 2740 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2741 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2742 | spyWindow->setTrustedOverlay(true); |
| 2743 | spyWindow->setSpy(true); |
| 2744 | sp<FakeWindowHandle> window = |
| 2745 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2746 | window->setNoInputChannel(true); |
| 2747 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2748 | |
| 2749 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2750 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2751 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2752 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2753 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2754 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2755 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2756 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2757 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2758 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2759 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2760 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2761 | |
| 2762 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2763 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2764 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2765 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2766 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2767 | |
| 2768 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2769 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 2770 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2771 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2772 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 2773 | |
| 2774 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2775 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2776 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2777 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2778 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2779 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2780 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2781 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2782 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2783 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2784 | |
| 2785 | // No more events |
| 2786 | spyWindow->assertNoEvents(); |
| 2787 | window->assertNoEvents(); |
| 2788 | } |
| 2789 | |
| 2790 | /** |
| 2791 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 2792 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 2793 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 2794 | * While the mouse is down, new move events from the touch device should be ignored. |
| 2795 | */ |
| 2796 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 2797 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2798 | sp<FakeWindowHandle> spyWindow = |
| 2799 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2800 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2801 | spyWindow->setTrustedOverlay(true); |
| 2802 | spyWindow->setSpy(true); |
| 2803 | sp<FakeWindowHandle> window = |
| 2804 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2805 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2806 | |
| 2807 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2808 | |
| 2809 | const int32_t mouseDeviceId = 7; |
| 2810 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2811 | |
| 2812 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2813 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2814 | .deviceId(mouseDeviceId) |
| 2815 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2816 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2817 | spyWindow->consumeMotionEvent( |
| 2818 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2819 | window->consumeMotionEvent( |
| 2820 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2821 | |
| 2822 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2823 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2824 | .deviceId(touchDeviceId) |
| 2825 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 2826 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2827 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2828 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2829 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2830 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2831 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2832 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2833 | .deviceId(touchDeviceId) |
| 2834 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 2835 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2836 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2837 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2838 | |
| 2839 | // Pilfer the stream |
| 2840 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2841 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2842 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2843 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2844 | .deviceId(touchDeviceId) |
| 2845 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 2846 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2847 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2848 | |
| 2849 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2850 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2851 | .deviceId(mouseDeviceId) |
| 2852 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2853 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2854 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2855 | |
| 2856 | spyWindow->consumeMotionEvent( |
| 2857 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2858 | spyWindow->consumeMotionEvent( |
| 2859 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2860 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2861 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2862 | mDispatcher->notifyMotion( |
| 2863 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2864 | .deviceId(mouseDeviceId) |
| 2865 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2866 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2867 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2868 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2869 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2870 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2871 | |
| 2872 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2873 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 2874 | .deviceId(mouseDeviceId) |
| 2875 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2876 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 2877 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2878 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2879 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2880 | |
| 2881 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2882 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2883 | .deviceId(touchDeviceId) |
| 2884 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 2885 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2886 | |
| 2887 | // No more events |
| 2888 | spyWindow->assertNoEvents(); |
| 2889 | window->assertNoEvents(); |
| 2890 | } |
| 2891 | |
| 2892 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2893 | * On the display, have a single window, and also an area where there's no window. |
| 2894 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2895 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2896 | */ |
| 2897 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2898 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2899 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2900 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2901 | |
| 2902 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2903 | |
| 2904 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2905 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2906 | |
| 2907 | mDispatcher->waitForIdle(); |
| 2908 | window->assertNoEvents(); |
| 2909 | |
| 2910 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2911 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2912 | mDispatcher->waitForIdle(); |
| 2913 | window->consumeMotionDown(); |
| 2914 | } |
| 2915 | |
| 2916 | /** |
| 2917 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2918 | * non-touchable window. |
| 2919 | */ |
| 2920 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2921 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2922 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2923 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2924 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2925 | window1->setTouchable(false); |
| 2926 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2927 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2928 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2929 | |
| 2930 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2931 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2932 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2933 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2934 | |
| 2935 | mDispatcher->waitForIdle(); |
| 2936 | window1->assertNoEvents(); |
| 2937 | window2->assertNoEvents(); |
| 2938 | |
| 2939 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2940 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2941 | mDispatcher->waitForIdle(); |
| 2942 | window2->consumeMotionDown(); |
| 2943 | } |
| 2944 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2945 | /** |
| 2946 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2947 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2948 | */ |
| 2949 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2950 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2951 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2952 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2953 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2954 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2955 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2956 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2957 | |
| 2958 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2959 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2960 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2961 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2962 | |
| 2963 | mDispatcher->waitForIdle(); |
| 2964 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2965 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2966 | window2->assertNoEvents(); |
| 2967 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2968 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2969 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2970 | |
| 2971 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2972 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2973 | mDispatcher->waitForIdle(); |
| 2974 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2975 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2976 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2977 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2978 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2979 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2980 | |
| 2981 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2982 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2983 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2984 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2985 | |
| 2986 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2987 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2988 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2989 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2990 | |
| 2991 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2992 | window1->consumeMotionMove(); |
| 2993 | window1->assertNoEvents(); |
| 2994 | |
| 2995 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2996 | mDispatcher->notifyMotion( |
| 2997 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2998 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2999 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3000 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3001 | mDispatcher->notifyMotion( |
| 3002 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3003 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3004 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3007 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3008 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3009 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3010 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3011 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3012 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3013 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3014 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3015 | |
| 3016 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3017 | |
| 3018 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 3019 | |
| 3020 | // 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] | 3021 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3022 | injectMotionEvent(mDispatcher, |
| 3023 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3024 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3025 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3026 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3027 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3028 | |
| 3029 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3031 | injectMotionEvent(mDispatcher, |
| 3032 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3033 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3034 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3035 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3036 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3037 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3038 | |
| 3039 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3040 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3041 | injectMotionEvent(mDispatcher, |
| 3042 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3043 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3044 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3045 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3046 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3047 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3048 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3049 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3050 | injectMotionEvent(mDispatcher, |
| 3051 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3052 | AINPUT_SOURCE_MOUSE) |
| 3053 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3054 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3055 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3056 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3057 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3058 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3059 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3060 | injectMotionEvent(mDispatcher, |
| 3061 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3062 | AINPUT_SOURCE_MOUSE) |
| 3063 | .buttonState(0) |
| 3064 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3065 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3066 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3067 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3068 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3069 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3070 | injectMotionEvent(mDispatcher, |
| 3071 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3072 | .buttonState(0) |
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())); |
| 3075 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3076 | |
| 3077 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3078 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3079 | injectMotionEvent(mDispatcher, |
| 3080 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3081 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3082 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3083 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3084 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3085 | |
| 3086 | // No more events |
| 3087 | windowLeft->assertNoEvents(); |
| 3088 | windowRight->assertNoEvents(); |
| 3089 | } |
| 3090 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3091 | /** |
| 3092 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3093 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3094 | * currently active gesture should be canceled, and the new one should proceed. |
| 3095 | */ |
| 3096 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3097 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3098 | sp<FakeWindowHandle> window = |
| 3099 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3100 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3101 | |
| 3102 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3103 | |
| 3104 | const int32_t touchDeviceId = 4; |
| 3105 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3106 | |
| 3107 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3108 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3109 | .deviceId(touchDeviceId) |
| 3110 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3111 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3112 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3113 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3114 | .deviceId(touchDeviceId) |
| 3115 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3116 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3117 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3118 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3119 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3120 | |
| 3121 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3122 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3123 | .deviceId(mouseDeviceId) |
| 3124 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3125 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3126 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3127 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3128 | WithPointerCount(2u))); |
| 3129 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3130 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3131 | mDispatcher->notifyMotion( |
| 3132 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3133 | .deviceId(mouseDeviceId) |
| 3134 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3135 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3136 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3137 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3138 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3139 | |
| 3140 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3141 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3142 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3143 | .deviceId(touchDeviceId) |
| 3144 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3145 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3146 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3147 | window->assertNoEvents(); |
| 3148 | } |
| 3149 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3150 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3151 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3152 | |
| 3153 | sp<FakeWindowHandle> spyWindow = |
| 3154 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3155 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3156 | spyWindow->setTrustedOverlay(true); |
| 3157 | spyWindow->setSpy(true); |
| 3158 | sp<FakeWindowHandle> window = |
| 3159 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3160 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3161 | |
| 3162 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3163 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3164 | |
| 3165 | // Send mouse cursor to the window |
| 3166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3167 | injectMotionEvent(mDispatcher, |
| 3168 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3169 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3170 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3171 | .x(100) |
| 3172 | .y(100)) |
| 3173 | .build())); |
| 3174 | |
| 3175 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3176 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3177 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3178 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3179 | |
| 3180 | window->assertNoEvents(); |
| 3181 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3182 | } |
| 3183 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3184 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3185 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3186 | |
| 3187 | sp<FakeWindowHandle> spyWindow = |
| 3188 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3189 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3190 | spyWindow->setTrustedOverlay(true); |
| 3191 | spyWindow->setSpy(true); |
| 3192 | sp<FakeWindowHandle> window = |
| 3193 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3194 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3195 | |
| 3196 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3197 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3198 | |
| 3199 | // Send mouse cursor to the window |
| 3200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3201 | injectMotionEvent(mDispatcher, |
| 3202 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3203 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3204 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3205 | .x(100) |
| 3206 | .y(100)) |
| 3207 | .build())); |
| 3208 | |
| 3209 | // Move mouse cursor |
| 3210 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3211 | injectMotionEvent(mDispatcher, |
| 3212 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3213 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3214 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3215 | .x(110) |
| 3216 | .y(110)) |
| 3217 | .build())); |
| 3218 | |
| 3219 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3220 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3221 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3222 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3223 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3224 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3225 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3226 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3227 | // Touch down on the window |
| 3228 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3229 | injectMotionEvent(mDispatcher, |
| 3230 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3231 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3232 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3233 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3234 | .x(200) |
| 3235 | .y(200)) |
| 3236 | .build())); |
| 3237 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3238 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3239 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3240 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3241 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3242 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3243 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3244 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3245 | |
| 3246 | // pilfer the motion, retaining the gesture on the spy window. |
| 3247 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3248 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3249 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3250 | |
| 3251 | // Touch UP on the window |
| 3252 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3253 | injectMotionEvent(mDispatcher, |
| 3254 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3255 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3256 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3257 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3258 | .x(200) |
| 3259 | .y(200)) |
| 3260 | .build())); |
| 3261 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3262 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3263 | |
| 3264 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3265 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3266 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3267 | |
| 3268 | // One more tap - DOWN |
| 3269 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3270 | injectMotionEvent(mDispatcher, |
| 3271 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3272 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3273 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3274 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3275 | .x(250) |
| 3276 | .y(250)) |
| 3277 | .build())); |
| 3278 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3279 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3280 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3281 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3282 | |
| 3283 | // Touch UP on the window |
| 3284 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3285 | injectMotionEvent(mDispatcher, |
| 3286 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3287 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3288 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3289 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3290 | .x(250) |
| 3291 | .y(250)) |
| 3292 | .build())); |
| 3293 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3294 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3295 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3296 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3297 | |
| 3298 | window->assertNoEvents(); |
| 3299 | spyWindow->assertNoEvents(); |
| 3300 | } |
| 3301 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3302 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3303 | // directly in this test. |
| 3304 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3305 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3306 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3307 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3308 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3309 | |
| 3310 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3311 | |
| 3312 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3313 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3314 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3315 | injectMotionEvent(mDispatcher, |
| 3316 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3317 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3318 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3319 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3320 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3321 | // Inject a series of mouse events for a mouse click |
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_DOWN, AINPUT_SOURCE_MOUSE) |
| 3325 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
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 | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3328 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3329 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3330 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3332 | injectMotionEvent(mDispatcher, |
| 3333 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3334 | AINPUT_SOURCE_MOUSE) |
| 3335 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3336 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3337 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3338 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3339 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3340 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3341 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3342 | injectMotionEvent(mDispatcher, |
| 3343 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3344 | AINPUT_SOURCE_MOUSE) |
| 3345 | .buttonState(0) |
| 3346 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3347 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3348 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3349 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3350 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3351 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3352 | injectMotionEvent(mDispatcher, |
| 3353 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3354 | .buttonState(0) |
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())); |
| 3357 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3358 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3359 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 3360 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 3361 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3362 | injectMotionEvent(mDispatcher, |
| 3363 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3364 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3365 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3366 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3367 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3368 | } |
| 3369 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3370 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3371 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3372 | * is generated. |
| 3373 | */ |
| 3374 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3375 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3376 | sp<FakeWindowHandle> window = |
| 3377 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3378 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3379 | |
| 3380 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3381 | |
| 3382 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3383 | |
| 3384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3385 | injectMotionEvent(mDispatcher, |
| 3386 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3387 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3388 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3389 | .x(300) |
| 3390 | .y(400)) |
| 3391 | .build())); |
| 3392 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3393 | |
| 3394 | // Remove the window, but keep the channel. |
| 3395 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3396 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3397 | } |
| 3398 | |
| 3399 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3400 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3401 | */ |
| 3402 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3403 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3404 | sp<FakeWindowHandle> window = |
| 3405 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3406 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3407 | |
| 3408 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3409 | |
| 3410 | const int32_t mouseDeviceId = 7; |
| 3411 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3412 | |
| 3413 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3414 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3415 | .deviceId(mouseDeviceId) |
| 3416 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 3417 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3418 | window->consumeMotionEvent( |
| 3419 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3420 | |
| 3421 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3422 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3423 | .deviceId(touchDeviceId) |
| 3424 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3425 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3426 | |
| 3427 | window->consumeMotionEvent( |
| 3428 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3429 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3430 | } |
| 3431 | |
| 3432 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3433 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3434 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3435 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3436 | */ |
| 3437 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3438 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3439 | sp<FakeWindowHandle> window = |
| 3440 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3441 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3442 | |
| 3443 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3444 | |
| 3445 | // Inject a hover_move from mouse. |
| 3446 | NotifyMotionArgs motionArgs = |
| 3447 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 3448 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3449 | motionArgs.xCursorPosition = 50; |
| 3450 | motionArgs.yCursorPosition = 50; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3451 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3452 | ASSERT_NO_FATAL_FAILURE( |
| 3453 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3454 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3455 | |
| 3456 | // Tap on the window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3457 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3458 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3459 | {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3460 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3461 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3462 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3463 | |
| 3464 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3465 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3466 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3467 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3468 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3469 | ADISPLAY_ID_DEFAULT, {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3470 | ASSERT_NO_FATAL_FAILURE( |
| 3471 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3472 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3473 | } |
| 3474 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3475 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3476 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3477 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3478 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3479 | ADISPLAY_ID_DEFAULT); |
| 3480 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3481 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3482 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3483 | SECOND_DISPLAY_ID); |
| 3484 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3485 | |
| 3486 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3487 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3488 | |
| 3489 | // Set cursor position in window in default display and check that hover enter and move |
| 3490 | // events are generated. |
| 3491 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3492 | injectMotionEvent(mDispatcher, |
| 3493 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3494 | AINPUT_SOURCE_MOUSE) |
| 3495 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3496 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3497 | .x(300) |
| 3498 | .y(600)) |
| 3499 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3500 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3501 | |
| 3502 | // Remove all windows in secondary display and check that no event happens on window in |
| 3503 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3504 | mDispatcher->setInputWindows( |
| 3505 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3506 | windowDefaultDisplay->assertNoEvents(); |
| 3507 | |
| 3508 | // Move cursor position in window in default display and check that only hover move |
| 3509 | // event is generated and not hover enter event. |
| 3510 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3511 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3512 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3513 | injectMotionEvent(mDispatcher, |
| 3514 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3515 | AINPUT_SOURCE_MOUSE) |
| 3516 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3517 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3518 | .x(400) |
| 3519 | .y(700)) |
| 3520 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3521 | windowDefaultDisplay->consumeMotionEvent( |
| 3522 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3523 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3524 | windowDefaultDisplay->assertNoEvents(); |
| 3525 | } |
| 3526 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3527 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3528 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3529 | |
| 3530 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3531 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3532 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3533 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3534 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3535 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3536 | |
| 3537 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3538 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3539 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3540 | |
| 3541 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3542 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3544 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3545 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3546 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3547 | windowRight->assertNoEvents(); |
| 3548 | } |
| 3549 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3550 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3551 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3552 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3553 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3554 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3555 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3556 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3557 | setFocusedWindow(window); |
| 3558 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3559 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3560 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3561 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3562 | |
| 3563 | // Window should receive key down event. |
| 3564 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3565 | |
| 3566 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3567 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3568 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3569 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3570 | AKEY_EVENT_FLAG_CANCELED); |
| 3571 | } |
| 3572 | |
| 3573 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3574 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3575 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3576 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3577 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3578 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3579 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3580 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3581 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3582 | |
| 3583 | // Window should receive motion down event. |
| 3584 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3585 | |
| 3586 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 3587 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3588 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3589 | window->consumeMotionEvent( |
| 3590 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3591 | } |
| 3592 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 3593 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 3594 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3595 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3596 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3597 | |
| 3598 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3599 | |
| 3600 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3601 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 3602 | .build()); |
| 3603 | |
| 3604 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3605 | |
| 3606 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 3607 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 3608 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3609 | |
| 3610 | // After the device has been reset, a new hovering stream can be sent to the window |
| 3611 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3612 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 3613 | .build()); |
| 3614 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3615 | } |
| 3616 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3617 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 3618 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3619 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3620 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3621 | window->setFocusable(true); |
| 3622 | |
| 3623 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3624 | setFocusedWindow(window); |
| 3625 | |
| 3626 | window->consumeFocusEvent(true); |
| 3627 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3628 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3629 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3630 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3631 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3632 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3633 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3634 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3635 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3636 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3637 | } |
| 3638 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 3639 | /** |
| 3640 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 3641 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3642 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3643 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3644 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3645 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3646 | window->setFocusable(true); |
| 3647 | |
| 3648 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3649 | setFocusedWindow(window); |
| 3650 | |
| 3651 | window->consumeFocusEvent(true); |
| 3652 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3653 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3654 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 3655 | |
| 3656 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 3657 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 3658 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 3659 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 3660 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3661 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3662 | } |
| 3663 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3664 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3665 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3666 | * WATCH_OUTSIDE_TOUCH. |
| 3667 | * Both windows are owned by the same UID. |
| 3668 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3669 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3670 | */ |
| 3671 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3672 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3673 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3674 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3675 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3676 | |
| 3677 | sp<FakeWindowHandle> outsideWindow = |
| 3678 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3679 | ADISPLAY_ID_DEFAULT); |
| 3680 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3681 | outsideWindow->setWatchOutsideTouch(true); |
| 3682 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3684 | |
| 3685 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3686 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3687 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3688 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3689 | window->consumeMotionDown(); |
| 3690 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3691 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3692 | outsideWindow->consumeMotionEvent( |
| 3693 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3694 | } |
| 3695 | |
| 3696 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3697 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3698 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3699 | * ACTION_OUTSIDE event is sent per gesture. |
| 3700 | */ |
| 3701 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3702 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 3703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3704 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3705 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3706 | window->setWatchOutsideTouch(true); |
| 3707 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3708 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3709 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3710 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3711 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3712 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3713 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3714 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3715 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3716 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3717 | |
| 3718 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3719 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3720 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3721 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3722 | window->assertNoEvents(); |
| 3723 | secondWindow->assertNoEvents(); |
| 3724 | |
| 3725 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3726 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3727 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3728 | ADISPLAY_ID_DEFAULT, |
| 3729 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3730 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3731 | window->consumeMotionEvent( |
| 3732 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3733 | secondWindow->consumeMotionDown(); |
| 3734 | thirdWindow->assertNoEvents(); |
| 3735 | |
| 3736 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3737 | // 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] | 3738 | mDispatcher->notifyMotion( |
| 3739 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3740 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3741 | window->assertNoEvents(); |
| 3742 | secondWindow->consumeMotionMove(); |
| 3743 | thirdWindow->consumeMotionDown(); |
| 3744 | } |
| 3745 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3746 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3747 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3748 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3749 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3750 | window->setFocusable(true); |
| 3751 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3752 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3753 | setFocusedWindow(window); |
| 3754 | |
| 3755 | window->consumeFocusEvent(true); |
| 3756 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3757 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3758 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3759 | mDispatcher->notifyKey(keyDown); |
| 3760 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3761 | |
| 3762 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3763 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3764 | |
| 3765 | // 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] | 3766 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3767 | |
| 3768 | window->consumeFocusEvent(false); |
| 3769 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3770 | mDispatcher->notifyKey(keyDown); |
| 3771 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3772 | window->assertNoEvents(); |
| 3773 | } |
| 3774 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3775 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3776 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3777 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3778 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3779 | // Ensure window is non-split and have some transform. |
| 3780 | window->setPreventSplitting(true); |
| 3781 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3782 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3783 | |
| 3784 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3785 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3786 | {50, 50})) |
| 3787 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3788 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3789 | |
| 3790 | const MotionEvent secondFingerDownEvent = |
| 3791 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3792 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3793 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3794 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 3795 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3796 | .build(); |
| 3797 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3798 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3799 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3800 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3801 | |
| 3802 | const MotionEvent* event = window->consumeMotion(); |
| 3803 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3804 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3805 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3806 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3807 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3808 | } |
| 3809 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 3810 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 3811 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3812 | sp<FakeWindowHandle> window = |
| 3813 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3814 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3815 | sp<FakeWindowHandle> trustedOverlay = |
| 3816 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 3817 | ADISPLAY_ID_DEFAULT); |
| 3818 | trustedOverlay->setSpy(true); |
| 3819 | trustedOverlay->setTrustedOverlay(true); |
| 3820 | |
| 3821 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}}); |
| 3822 | |
| 3823 | // Start a three-finger touchpad swipe |
| 3824 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3825 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3826 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3827 | .build()); |
| 3828 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 3829 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3830 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3831 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3832 | .build()); |
| 3833 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 3834 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3835 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3836 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 3837 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3838 | .build()); |
| 3839 | |
| 3840 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3841 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3842 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 3843 | |
| 3844 | // Move the swipe a bit |
| 3845 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3846 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3847 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3848 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3849 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3850 | .build()); |
| 3851 | |
| 3852 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3853 | |
| 3854 | // End the swipe |
| 3855 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 3856 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3857 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3858 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3859 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3860 | .build()); |
| 3861 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 3862 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3863 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3864 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3865 | .build()); |
| 3866 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3867 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3868 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3869 | .build()); |
| 3870 | |
| 3871 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 3872 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 3873 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3874 | |
| 3875 | window->assertNoEvents(); |
| 3876 | } |
| 3877 | |
| 3878 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 3879 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3880 | sp<FakeWindowHandle> window = |
| 3881 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3882 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3883 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3884 | |
| 3885 | // Start a three-finger touchpad swipe |
| 3886 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3887 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3888 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3889 | .build()); |
| 3890 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 3891 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3892 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3893 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3894 | .build()); |
| 3895 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 3896 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3897 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3898 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 3899 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3900 | .build()); |
| 3901 | |
| 3902 | // Move the swipe a bit |
| 3903 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3904 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3905 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3906 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3907 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3908 | .build()); |
| 3909 | |
| 3910 | // End the swipe |
| 3911 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, 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 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 3918 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3919 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3920 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3921 | .build()); |
| 3922 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3923 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3924 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3925 | .build()); |
| 3926 | |
| 3927 | window->assertNoEvents(); |
| 3928 | } |
| 3929 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3930 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 3931 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 3932 | * the first pointer. |
| 3933 | * Ensure that the second pointer is not sent to the window. |
| 3934 | * |
| 3935 | * The subsequent gesture should be correctly delivered to the window. |
| 3936 | */ |
| 3937 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 3938 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3939 | sp<FakeWindowHandle> window = |
| 3940 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3941 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3942 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3943 | |
| 3944 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3945 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3946 | .downTime(baseTime + 10) |
| 3947 | .eventTime(baseTime + 10) |
| 3948 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3949 | .build()); |
| 3950 | |
| 3951 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3952 | |
| 3953 | // We need a new window object for the same window, because dispatcher will store objects by |
| 3954 | // reference. That means that the testing code and the dispatcher will refer to the same shared |
| 3955 | // object. Calling window->setTransform here would affect dispatcher's comparison |
| 3956 | // of the old window to the new window, since both the old window and the new window would be |
| 3957 | // updated to the same value. |
| 3958 | sp<FakeWindowHandle> windowDup = window->duplicate(); |
| 3959 | |
| 3960 | // Change the transform so that the orientation is now different from original. |
| 3961 | windowDup->setWindowTransform(0, -1, 1, 0); |
| 3962 | |
| 3963 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDup}}}); |
| 3964 | |
| 3965 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3966 | |
| 3967 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3968 | .downTime(baseTime + 10) |
| 3969 | .eventTime(baseTime + 30) |
| 3970 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3971 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 3972 | .build()); |
| 3973 | |
| 3974 | // Finish the gesture and start a new one. Ensure the new gesture is sent to the window |
| 3975 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3976 | .downTime(baseTime + 10) |
| 3977 | .eventTime(baseTime + 40) |
| 3978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3979 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 3980 | .build()); |
| 3981 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3982 | .downTime(baseTime + 10) |
| 3983 | .eventTime(baseTime + 50) |
| 3984 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3985 | .build()); |
| 3986 | |
| 3987 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3988 | .downTime(baseTime + 60) |
| 3989 | .eventTime(baseTime + 60) |
| 3990 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 3991 | .build()); |
| 3992 | |
| 3993 | windowDup->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3994 | } |
| 3995 | |
| 3996 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3997 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 3998 | * |
| 3999 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4000 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4001 | * space. |
| 4002 | */ |
| 4003 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4004 | public: |
| 4005 | void SetUp() override { |
| 4006 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4007 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4011 | gui::DisplayInfo info; |
| 4012 | info.displayId = displayId; |
| 4013 | info.transform = transform; |
| 4014 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4015 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4019 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4020 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4021 | } |
| 4022 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4023 | void removeAllWindowsAndDisplays() { |
| 4024 | mDisplayInfos.clear(); |
| 4025 | mWindowInfos.clear(); |
| 4026 | } |
| 4027 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4028 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4029 | // on the display. |
| 4030 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4031 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4032 | // respectively. |
| 4033 | ui::Transform displayTransform; |
| 4034 | displayTransform.set(2, 0, 0, 4); |
| 4035 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4036 | |
| 4037 | std::shared_ptr<FakeApplicationHandle> application = |
| 4038 | std::make_shared<FakeApplicationHandle>(); |
| 4039 | |
| 4040 | // Add two windows to the display. Their frames are represented in the display space. |
| 4041 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4042 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4043 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4044 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 4045 | addWindow(firstWindow); |
| 4046 | |
| 4047 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4048 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4049 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4050 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 4051 | addWindow(secondWindow); |
| 4052 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 4053 | } |
| 4054 | |
| 4055 | private: |
| 4056 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 4057 | std::vector<gui::WindowInfo> mWindowInfos; |
| 4058 | }; |
| 4059 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4060 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4061 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4062 | // 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] | 4063 | // selected so that if the hit test was performed with the point and the bounds being in |
| 4064 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4065 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4066 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4067 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4068 | |
| 4069 | firstWindow->consumeMotionDown(); |
| 4070 | secondWindow->assertNoEvents(); |
| 4071 | } |
| 4072 | |
| 4073 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 4074 | // the event should be treated as being in the logical display space. |
| 4075 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 4076 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4077 | // Send down to the first window. The point is represented in the logical display space. The |
| 4078 | // point is selected so that if the hit test was done in logical display space, then it would |
| 4079 | // end up in the incorrect window. |
| 4080 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4081 | PointF{75 * 2, 55 * 4}); |
| 4082 | |
| 4083 | firstWindow->consumeMotionDown(); |
| 4084 | secondWindow->assertNoEvents(); |
| 4085 | } |
| 4086 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4087 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 4088 | // event should be treated as being in the logical display space. |
| 4089 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 4090 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4091 | |
| 4092 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4093 | ui::Transform injectedEventTransform; |
| 4094 | injectedEventTransform.set(matrix); |
| 4095 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 4096 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 4097 | |
| 4098 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4099 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4100 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4101 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4102 | .x(untransformedPoint.x) |
| 4103 | .y(untransformedPoint.y)) |
| 4104 | .build(); |
| 4105 | event.transform(matrix); |
| 4106 | |
| 4107 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 4108 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4109 | |
| 4110 | firstWindow->consumeMotionDown(); |
| 4111 | secondWindow->assertNoEvents(); |
| 4112 | } |
| 4113 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4114 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4115 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4116 | |
| 4117 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4118 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4119 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4120 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4121 | |
| 4122 | firstWindow->assertNoEvents(); |
| 4123 | const MotionEvent* event = secondWindow->consumeMotion(); |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 4124 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4125 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 4126 | |
| 4127 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 4128 | EXPECT_EQ(300, event->getRawX(0)); |
| 4129 | EXPECT_EQ(880, event->getRawY(0)); |
| 4130 | |
| 4131 | // Ensure that the x and y values are in the window's coordinate space. |
| 4132 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4133 | // the logical display space. This will be the origin of the window space. |
| 4134 | EXPECT_EQ(100, event->getX(0)); |
| 4135 | EXPECT_EQ(80, event->getY(0)); |
| 4136 | } |
| 4137 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4138 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4139 | class InputDispatcherDisplayOrientationFixture |
| 4140 | : public InputDispatcherDisplayProjectionTest, |
| 4141 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 4142 | |
| 4143 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 4144 | // in different locations on the display, specifically points close to the four corners of a |
| 4145 | // window. |
| 4146 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 4147 | constexpr static int32_t displayWidth = 400; |
| 4148 | constexpr static int32_t displayHeight = 800; |
| 4149 | |
| 4150 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4151 | |
| 4152 | const auto rotation = GetParam(); |
| 4153 | |
| 4154 | // Set up the display with the specified rotation. |
| 4155 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4156 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4157 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4158 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4159 | logicalDisplayWidth, logicalDisplayHeight); |
| 4160 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4161 | |
| 4162 | // Create a window with its bounds determined in the logical display. |
| 4163 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 4164 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4165 | sp<FakeWindowHandle> window = |
| 4166 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4167 | window->setFrame(frameInDisplay, displayTransform); |
| 4168 | addWindow(window); |
| 4169 | |
| 4170 | // The following points in logical display space should be inside the window. |
| 4171 | static const std::array<vec2, 4> insidePoints{ |
| 4172 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4173 | for (const auto pointInsideWindow : insidePoints) { |
| 4174 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4175 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4176 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4177 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4178 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4179 | window->consumeMotionDown(); |
| 4180 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4181 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4182 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4183 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4184 | window->consumeMotionUp(); |
| 4185 | } |
| 4186 | |
| 4187 | // The following points in logical display space should be outside the window. |
| 4188 | static const std::array<vec2, 5> outsidePoints{ |
| 4189 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4190 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4191 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4192 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4193 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4194 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4195 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4196 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4197 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4198 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4199 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4200 | } |
| 4201 | window->assertNoEvents(); |
| 4202 | } |
| 4203 | |
| 4204 | // Run the precision tests for all rotations. |
| 4205 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4206 | InputDispatcherDisplayOrientationFixture, |
| 4207 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4208 | ui::ROTATION_270), |
| 4209 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4210 | return ftl::enum_string(testParamInfo.param); |
| 4211 | }); |
| 4212 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4213 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4214 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4215 | |
| 4216 | class TransferTouchFixture : public InputDispatcherTest, |
| 4217 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4218 | |
| 4219 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4220 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4221 | |
| 4222 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4223 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4224 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4225 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4226 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4227 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4228 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4229 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4230 | sp<FakeWindowHandle> wallpaper = |
| 4231 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 4232 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4233 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4234 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4235 | |
| 4236 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4237 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4238 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4239 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4240 | // Only the first window should get the down event |
| 4241 | firstWindow->consumeMotionDown(); |
| 4242 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4243 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4244 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4245 | // Transfer touch to the second window |
| 4246 | TransferFunction f = GetParam(); |
| 4247 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4248 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4249 | // The first window gets cancel and the second gets down |
| 4250 | firstWindow->consumeMotionCancel(); |
| 4251 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4252 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4253 | |
| 4254 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4255 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4256 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4257 | // The first window gets no events and the second gets up |
| 4258 | firstWindow->assertNoEvents(); |
| 4259 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4260 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4261 | } |
| 4262 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4263 | /** |
| 4264 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 4265 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 4266 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 4267 | * natural to the user. |
| 4268 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 4269 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 4270 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 4271 | * the first window to the second. |
| 4272 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 4273 | * the other API, as well. |
| 4274 | */ |
| 4275 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 4276 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4277 | |
| 4278 | // Create a couple of windows + a spy window |
| 4279 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4280 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4281 | spyWindow->setTrustedOverlay(true); |
| 4282 | spyWindow->setSpy(true); |
| 4283 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4284 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4285 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4286 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4287 | |
| 4288 | // Add the windows to the dispatcher |
| 4289 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 4290 | |
| 4291 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4292 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4293 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4294 | // Only the first window and spy should get the down event |
| 4295 | spyWindow->consumeMotionDown(); |
| 4296 | firstWindow->consumeMotionDown(); |
| 4297 | |
| 4298 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 4299 | // if f === 'transferTouch'. |
| 4300 | TransferFunction f = GetParam(); |
| 4301 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4302 | ASSERT_TRUE(success); |
| 4303 | // The first window gets cancel and the second gets down |
| 4304 | firstWindow->consumeMotionCancel(); |
| 4305 | secondWindow->consumeMotionDown(); |
| 4306 | |
| 4307 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4308 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4309 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4310 | // The first window gets no events and the second+spy get up |
| 4311 | firstWindow->assertNoEvents(); |
| 4312 | spyWindow->consumeMotionUp(); |
| 4313 | secondWindow->consumeMotionUp(); |
| 4314 | } |
| 4315 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4316 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4317 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4318 | |
| 4319 | PointF touchPoint = {10, 10}; |
| 4320 | |
| 4321 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4322 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4323 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4324 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4325 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4326 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4327 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4328 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4329 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4330 | |
| 4331 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4332 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4333 | |
| 4334 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4335 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4336 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4337 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4338 | // Only the first window should get the down event |
| 4339 | firstWindow->consumeMotionDown(); |
| 4340 | secondWindow->assertNoEvents(); |
| 4341 | |
| 4342 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4343 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4344 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4345 | // Only the first window should get the pointer down event |
| 4346 | firstWindow->consumeMotionPointerDown(1); |
| 4347 | secondWindow->assertNoEvents(); |
| 4348 | |
| 4349 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4350 | TransferFunction f = GetParam(); |
| 4351 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4352 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4353 | // The first window gets cancel and the second gets down and pointer down |
| 4354 | firstWindow->consumeMotionCancel(); |
| 4355 | secondWindow->consumeMotionDown(); |
| 4356 | secondWindow->consumeMotionPointerDown(1); |
| 4357 | |
| 4358 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4359 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4360 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4361 | // The first window gets nothing and the second gets pointer up |
| 4362 | firstWindow->assertNoEvents(); |
| 4363 | secondWindow->consumeMotionPointerUp(1); |
| 4364 | |
| 4365 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4366 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4367 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4368 | // The first window gets nothing and the second gets up |
| 4369 | firstWindow->assertNoEvents(); |
| 4370 | secondWindow->consumeMotionUp(); |
| 4371 | } |
| 4372 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4373 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 4374 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4375 | |
| 4376 | // Create a couple of windows |
| 4377 | sp<FakeWindowHandle> firstWindow = |
| 4378 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4379 | ADISPLAY_ID_DEFAULT); |
| 4380 | firstWindow->setDupTouchToWallpaper(true); |
| 4381 | sp<FakeWindowHandle> secondWindow = |
| 4382 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4383 | ADISPLAY_ID_DEFAULT); |
| 4384 | secondWindow->setDupTouchToWallpaper(true); |
| 4385 | |
| 4386 | sp<FakeWindowHandle> wallpaper1 = |
| 4387 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 4388 | wallpaper1->setIsWallpaper(true); |
| 4389 | |
| 4390 | sp<FakeWindowHandle> wallpaper2 = |
| 4391 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 4392 | wallpaper2->setIsWallpaper(true); |
| 4393 | // Add the windows to the dispatcher |
| 4394 | mDispatcher->setInputWindows( |
| 4395 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 4396 | |
| 4397 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4398 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4399 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4400 | |
| 4401 | // Only the first window should get the down event |
| 4402 | firstWindow->consumeMotionDown(); |
| 4403 | secondWindow->assertNoEvents(); |
| 4404 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4405 | wallpaper2->assertNoEvents(); |
| 4406 | |
| 4407 | // Transfer touch focus to the second window |
| 4408 | TransferFunction f = GetParam(); |
| 4409 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4410 | ASSERT_TRUE(success); |
| 4411 | |
| 4412 | // The first window gets cancel and the second gets down |
| 4413 | firstWindow->consumeMotionCancel(); |
| 4414 | secondWindow->consumeMotionDown(); |
| 4415 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4416 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4417 | |
| 4418 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4419 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4420 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4421 | // The first window gets no events and the second gets up |
| 4422 | firstWindow->assertNoEvents(); |
| 4423 | secondWindow->consumeMotionUp(); |
| 4424 | wallpaper1->assertNoEvents(); |
| 4425 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4426 | } |
| 4427 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4428 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 4429 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 4430 | // for the case where there are multiple pointers split across several windows. |
| 4431 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 4432 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4433 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4434 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4435 | return dispatcher->transferTouch(destChannelToken, |
| 4436 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4437 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4438 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4439 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4440 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4441 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4442 | })); |
| 4443 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4444 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4445 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4446 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4447 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4448 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4449 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4450 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4451 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4452 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4453 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4454 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4455 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4456 | |
| 4457 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4458 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4459 | |
| 4460 | PointF pointInFirst = {300, 200}; |
| 4461 | PointF pointInSecond = {300, 600}; |
| 4462 | |
| 4463 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4464 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4465 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4466 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4467 | // Only the first window should get the down event |
| 4468 | firstWindow->consumeMotionDown(); |
| 4469 | secondWindow->assertNoEvents(); |
| 4470 | |
| 4471 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4472 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4473 | ADISPLAY_ID_DEFAULT, |
| 4474 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4475 | // The first window gets a move and the second a down |
| 4476 | firstWindow->consumeMotionMove(); |
| 4477 | secondWindow->consumeMotionDown(); |
| 4478 | |
| 4479 | // Transfer touch focus to the second window |
| 4480 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4481 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 4482 | firstWindow->consumeMotionCancel(); |
| 4483 | secondWindow->consumeMotionPointerDown(1); |
| 4484 | |
| 4485 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4486 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4487 | ADISPLAY_ID_DEFAULT, |
| 4488 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4489 | // The first window gets nothing and the second gets pointer up |
| 4490 | firstWindow->assertNoEvents(); |
| 4491 | secondWindow->consumeMotionPointerUp(1); |
| 4492 | |
| 4493 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4494 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4495 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4496 | // The first window gets nothing and the second gets up |
| 4497 | firstWindow->assertNoEvents(); |
| 4498 | secondWindow->consumeMotionUp(); |
| 4499 | } |
| 4500 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4501 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 4502 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 4503 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 4504 | // window should get nothing. |
| 4505 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 4506 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4507 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4508 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4509 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4510 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4511 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4512 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4513 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4514 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4515 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4516 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4517 | |
| 4518 | // Add the windows to the dispatcher |
| 4519 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4520 | |
| 4521 | PointF pointInFirst = {300, 200}; |
| 4522 | PointF pointInSecond = {300, 600}; |
| 4523 | |
| 4524 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4525 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4526 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4527 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4528 | // Only the first window should get the down event |
| 4529 | firstWindow->consumeMotionDown(); |
| 4530 | secondWindow->assertNoEvents(); |
| 4531 | |
| 4532 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4533 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4534 | ADISPLAY_ID_DEFAULT, |
| 4535 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4536 | // The first window gets a move and the second a down |
| 4537 | firstWindow->consumeMotionMove(); |
| 4538 | secondWindow->consumeMotionDown(); |
| 4539 | |
| 4540 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4541 | const bool transferred = |
| 4542 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4543 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 4544 | ASSERT_FALSE(transferred); |
| 4545 | firstWindow->assertNoEvents(); |
| 4546 | secondWindow->assertNoEvents(); |
| 4547 | |
| 4548 | // The rest of the dispatch should proceed as normal |
| 4549 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4550 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4551 | ADISPLAY_ID_DEFAULT, |
| 4552 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4553 | // The first window gets MOVE and the second gets pointer up |
| 4554 | firstWindow->consumeMotionMove(); |
| 4555 | secondWindow->consumeMotionUp(); |
| 4556 | |
| 4557 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4558 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4559 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4560 | // The first window gets nothing and the second gets up |
| 4561 | firstWindow->consumeMotionUp(); |
| 4562 | secondWindow->assertNoEvents(); |
| 4563 | } |
| 4564 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4565 | // This case will create two windows and one mirrored window on the default display and mirror |
| 4566 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 4567 | // the windows info of second display before default display. |
| 4568 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 4569 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4570 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4571 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4572 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4573 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4574 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4575 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4576 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4577 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4578 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4579 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4580 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4581 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4582 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4583 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4584 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4585 | |
| 4586 | // Update window info, let it find window handle of second display first. |
| 4587 | mDispatcher->setInputWindows( |
| 4588 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4589 | {ADISPLAY_ID_DEFAULT, |
| 4590 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4591 | |
| 4592 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4593 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4594 | {50, 50})) |
| 4595 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4596 | |
| 4597 | // Window should receive motion event. |
| 4598 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4599 | |
| 4600 | // Transfer touch focus |
| 4601 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 4602 | secondWindowInPrimary->getToken())); |
| 4603 | // The first window gets cancel. |
| 4604 | firstWindowInPrimary->consumeMotionCancel(); |
| 4605 | secondWindowInPrimary->consumeMotionDown(); |
| 4606 | |
| 4607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4608 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4609 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4610 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4611 | firstWindowInPrimary->assertNoEvents(); |
| 4612 | secondWindowInPrimary->consumeMotionMove(); |
| 4613 | |
| 4614 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4615 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4616 | {150, 50})) |
| 4617 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4618 | firstWindowInPrimary->assertNoEvents(); |
| 4619 | secondWindowInPrimary->consumeMotionUp(); |
| 4620 | } |
| 4621 | |
| 4622 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 4623 | // 'transferTouch' api. |
| 4624 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 4625 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4626 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4627 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4628 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4629 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4630 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4631 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4632 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4633 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4634 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4635 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4636 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4637 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4638 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4639 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4640 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4641 | |
| 4642 | // Update window info, let it find window handle of second display first. |
| 4643 | mDispatcher->setInputWindows( |
| 4644 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4645 | {ADISPLAY_ID_DEFAULT, |
| 4646 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4647 | |
| 4648 | // Touch on second display. |
| 4649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4650 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 4651 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4652 | |
| 4653 | // Window should receive motion event. |
| 4654 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4655 | |
| 4656 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4657 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4658 | |
| 4659 | // The first window gets cancel. |
| 4660 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4661 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4662 | |
| 4663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4664 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4665 | SECOND_DISPLAY_ID, {150, 50})) |
| 4666 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4667 | firstWindowInPrimary->assertNoEvents(); |
| 4668 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 4669 | |
| 4670 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4671 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 4672 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4673 | firstWindowInPrimary->assertNoEvents(); |
| 4674 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 4675 | } |
| 4676 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4677 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4678 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4679 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4680 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4681 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4682 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4683 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4684 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4685 | |
| 4686 | window->consumeFocusEvent(true); |
| 4687 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4688 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4689 | |
| 4690 | // Window should receive key down event. |
| 4691 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4692 | |
| 4693 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4694 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4695 | mFakePolicy->assertUserActivityPoked(); |
| 4696 | } |
| 4697 | |
| 4698 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 4699 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4700 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4701 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4702 | |
| 4703 | window->setDisableUserActivity(true); |
| 4704 | window->setFocusable(true); |
| 4705 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4706 | setFocusedWindow(window); |
| 4707 | |
| 4708 | window->consumeFocusEvent(true); |
| 4709 | |
| 4710 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4711 | |
| 4712 | // Window should receive key down event. |
| 4713 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4714 | |
| 4715 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4716 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4717 | mFakePolicy->assertUserActivityNotPoked(); |
| 4718 | } |
| 4719 | |
| 4720 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 4721 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4722 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4723 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4724 | |
| 4725 | window->setFocusable(true); |
| 4726 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4727 | setFocusedWindow(window); |
| 4728 | |
| 4729 | window->consumeFocusEvent(true); |
| 4730 | |
| 4731 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4732 | mDispatcher->waitForIdle(); |
| 4733 | |
| 4734 | // System key is not passed down |
| 4735 | window->assertNoEvents(); |
| 4736 | |
| 4737 | // Should have poked user activity |
| 4738 | mFakePolicy->assertUserActivityPoked(); |
| 4739 | } |
| 4740 | |
| 4741 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 4742 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4743 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4744 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4745 | |
| 4746 | window->setFocusable(true); |
| 4747 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4748 | setFocusedWindow(window); |
| 4749 | |
| 4750 | window->consumeFocusEvent(true); |
| 4751 | |
| 4752 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4753 | mDispatcher->waitForIdle(); |
| 4754 | |
| 4755 | // System key is not passed down |
| 4756 | window->assertNoEvents(); |
| 4757 | |
| 4758 | // Should have poked user activity |
| 4759 | mFakePolicy->assertUserActivityPoked(); |
| 4760 | } |
| 4761 | |
| 4762 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 4763 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4764 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4765 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4766 | |
| 4767 | window->setDisableUserActivity(true); |
| 4768 | window->setFocusable(true); |
| 4769 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4770 | setFocusedWindow(window); |
| 4771 | |
| 4772 | window->consumeFocusEvent(true); |
| 4773 | |
| 4774 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4775 | mDispatcher->waitForIdle(); |
| 4776 | |
| 4777 | // System key is not passed down |
| 4778 | window->assertNoEvents(); |
| 4779 | |
| 4780 | // Should have poked user activity |
| 4781 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4782 | } |
| 4783 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4784 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 4785 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4786 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4787 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4788 | |
| 4789 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4790 | |
| 4791 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4792 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4793 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 4794 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4795 | |
| 4796 | window->consumeMotionEvent( |
| 4797 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4798 | |
| 4799 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 4800 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4801 | mFakePolicy->assertUserActivityPoked(); |
| 4802 | } |
| 4803 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4804 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4805 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4806 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4807 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4808 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4809 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4810 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4811 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4812 | mDispatcher->waitForIdle(); |
| 4813 | |
| 4814 | window->assertNoEvents(); |
| 4815 | } |
| 4816 | |
| 4817 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4818 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4819 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4820 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4821 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4822 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4823 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4824 | |
| 4825 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4826 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4827 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4828 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4829 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4830 | |
| 4831 | // Window should receive only the motion event |
| 4832 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4833 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4834 | } |
| 4835 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4836 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4837 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4838 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4839 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4840 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4841 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4842 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4843 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4844 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4845 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4846 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4847 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4848 | |
| 4849 | // Add the windows to the dispatcher |
| 4850 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4851 | |
| 4852 | PointF pointInFirst = {300, 200}; |
| 4853 | PointF pointInSecond = {300, 600}; |
| 4854 | |
| 4855 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4856 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4857 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4858 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4859 | // Only the first window should get the down event |
| 4860 | firstWindow->consumeMotionDown(); |
| 4861 | secondWindow->assertNoEvents(); |
| 4862 | |
| 4863 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4864 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4865 | ADISPLAY_ID_DEFAULT, |
| 4866 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4867 | // The first window gets a move and the second a down |
| 4868 | firstWindow->consumeMotionMove(); |
| 4869 | secondWindow->consumeMotionDown(); |
| 4870 | |
| 4871 | // Send pointer cancel to the second window |
| 4872 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4873 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4874 | {pointInFirst, pointInSecond}); |
| 4875 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4876 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4877 | // The first window gets move and the second gets cancel. |
| 4878 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4879 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4880 | |
| 4881 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4882 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4883 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4884 | // The first window gets up and the second gets nothing. |
| 4885 | firstWindow->consumeMotionUp(); |
| 4886 | secondWindow->assertNoEvents(); |
| 4887 | } |
| 4888 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4889 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 4890 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4891 | |
| 4892 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4893 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4894 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4895 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 4896 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 4897 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 4898 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4899 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4900 | window->assertNoEvents(); |
| 4901 | mDispatcher->waitForIdle(); |
| 4902 | } |
| 4903 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4904 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4905 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4906 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4907 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4908 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4909 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4910 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4913 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 4914 | |
| 4915 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4916 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 4917 | expectedFlags); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4918 | } |
| 4919 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4920 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 4921 | |
| 4922 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 4923 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4924 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4925 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4926 | expectedDisplayId, expectedFlags); |
| 4927 | } |
| 4928 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4929 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4930 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4931 | expectedDisplayId, expectedFlags); |
| 4932 | } |
| 4933 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4934 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4935 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4936 | expectedDisplayId, expectedFlags); |
| 4937 | } |
| 4938 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4939 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4940 | mInputReceiver->consumeMotionEvent( |
| 4941 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4942 | WithDisplayId(expectedDisplayId), |
| 4943 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4946 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 4947 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 4948 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4949 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4950 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4953 | MotionEvent* consumeMotion() { |
| 4954 | InputEvent* event = mInputReceiver->consume(); |
| 4955 | if (!event) { |
| 4956 | ADD_FAILURE() << "No event was produced"; |
| 4957 | return nullptr; |
| 4958 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4959 | if (event->getType() != InputEventType::MOTION) { |
| 4960 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4961 | return nullptr; |
| 4962 | } |
| 4963 | return static_cast<MotionEvent*>(event); |
| 4964 | } |
| 4965 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4966 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 4967 | |
| 4968 | private: |
| 4969 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4970 | }; |
| 4971 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4972 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 4973 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4974 | /** |
| 4975 | * Two entities that receive touch: A window, and a global monitor. |
| 4976 | * The touch goes to the window, and then the window disappears. |
| 4977 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 4978 | * for the monitor, as well. |
| 4979 | * 1. foregroundWindow |
| 4980 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 4981 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4982 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4983 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4984 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4985 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4986 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4987 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4988 | |
| 4989 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4990 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4991 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4992 | {100, 200})) |
| 4993 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4994 | |
| 4995 | // Both the foreground window and the global monitor should receive the touch down |
| 4996 | window->consumeMotionDown(); |
| 4997 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4998 | |
| 4999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5000 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5001 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5002 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5003 | |
| 5004 | window->consumeMotionMove(); |
| 5005 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5006 | |
| 5007 | // Now the foreground window goes away |
| 5008 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 5009 | window->consumeMotionCancel(); |
| 5010 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 5011 | |
| 5012 | // If more events come in, there will be no more foreground window to send them to. This will |
| 5013 | // cause a cancel for the monitor, as well. |
| 5014 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5015 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5016 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 5017 | << "Injection should fail because the window was removed"; |
| 5018 | window->assertNoEvents(); |
| 5019 | // Global monitor now gets the cancel |
| 5020 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5021 | } |
| 5022 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5023 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5024 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5025 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5026 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5027 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5028 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5029 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5030 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5031 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5032 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5033 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5034 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5035 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5038 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 5039 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5040 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5041 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5042 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5043 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5044 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5045 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5046 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5047 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5048 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5049 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5050 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5051 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5052 | // Pilfer pointers from the monitor. |
| 5053 | // This should not do anything and the window should continue to receive events. |
| 5054 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5055 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5056 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5057 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5058 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5059 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5060 | |
| 5061 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5062 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5065 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5066 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5067 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5068 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5069 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5070 | window->setWindowOffset(20, 40); |
| 5071 | window->setWindowTransform(0, 1, -1, 0); |
| 5072 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5073 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5074 | |
| 5075 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5076 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5077 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5078 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5079 | MotionEvent* event = monitor.consumeMotion(); |
| 5080 | // Even though window has transform, gesture monitor must not. |
| 5081 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 5082 | } |
| 5083 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5084 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5085 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5086 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5087 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5088 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5089 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5090 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 5091 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5094 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5095 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5096 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5097 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5098 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5099 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5100 | |
| 5101 | NotifyMotionArgs motionArgs = |
| 5102 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5103 | ADISPLAY_ID_DEFAULT); |
| 5104 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5105 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5106 | // Window should receive motion down event. |
| 5107 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5108 | |
| 5109 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5110 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5111 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5112 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5113 | motionArgs.pointerCoords[0].getX() - 10); |
| 5114 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5115 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5116 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5117 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5118 | } |
| 5119 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5120 | /** |
| 5121 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 5122 | * the device default right away. In the test scenario, we check both the default value, |
| 5123 | * and the action of enabling / disabling. |
| 5124 | */ |
| 5125 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5127 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5128 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5129 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5130 | |
| 5131 | // Set focused application. |
| 5132 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5133 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5134 | |
| 5135 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5136 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5137 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5138 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5139 | |
| 5140 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5141 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5142 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5143 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5144 | |
| 5145 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5146 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5147 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5148 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5149 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5150 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5151 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5152 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5153 | |
| 5154 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5155 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5156 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5157 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5158 | |
| 5159 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5160 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5161 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5162 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5163 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5164 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5165 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5166 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5167 | |
| 5168 | window->assertNoEvents(); |
| 5169 | } |
| 5170 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5171 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5172 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5173 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5174 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5175 | |
| 5176 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5177 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5178 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5179 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5180 | setFocusedWindow(window); |
| 5181 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5182 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5183 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5184 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5185 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5186 | |
| 5187 | InputEvent* event = window->consume(); |
| 5188 | ASSERT_NE(event, nullptr); |
| 5189 | |
| 5190 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5191 | ASSERT_NE(verified, nullptr); |
| 5192 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 5193 | |
| 5194 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 5195 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 5196 | ASSERT_EQ(keyArgs.source, verified->source); |
| 5197 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 5198 | |
| 5199 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 5200 | |
| 5201 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5202 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5203 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5204 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 5205 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 5206 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 5207 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 5208 | } |
| 5209 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5210 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5211 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5212 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5213 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5214 | |
| 5215 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5216 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5217 | ui::Transform transform; |
| 5218 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5219 | |
| 5220 | gui::DisplayInfo displayInfo; |
| 5221 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5222 | displayInfo.transform = transform; |
| 5223 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5224 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5225 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5226 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5227 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5228 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5229 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5230 | |
| 5231 | InputEvent* event = window->consume(); |
| 5232 | ASSERT_NE(event, nullptr); |
| 5233 | |
| 5234 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5235 | ASSERT_NE(verified, nullptr); |
| 5236 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 5237 | |
| 5238 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 5239 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 5240 | EXPECT_EQ(motionArgs.source, verified->source); |
| 5241 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 5242 | |
| 5243 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 5244 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5245 | const vec2 rawXY = |
| 5246 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 5247 | motionArgs.pointerCoords[0].getXYValue()); |
| 5248 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 5249 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5250 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5251 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5252 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5253 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 5254 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 5255 | } |
| 5256 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5257 | /** |
| 5258 | * Ensure that separate calls to sign the same data are generating the same key. |
| 5259 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 5260 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 5261 | * tests. |
| 5262 | */ |
| 5263 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 5264 | KeyEvent event = getTestKeyEvent(); |
| 5265 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5266 | |
| 5267 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 5268 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5269 | ASSERT_EQ(hmac1, hmac2); |
| 5270 | } |
| 5271 | |
| 5272 | /** |
| 5273 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 5274 | */ |
| 5275 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 5276 | KeyEvent event = getTestKeyEvent(); |
| 5277 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5278 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 5279 | |
| 5280 | verifiedEvent.deviceId += 1; |
| 5281 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5282 | |
| 5283 | verifiedEvent.source += 1; |
| 5284 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5285 | |
| 5286 | verifiedEvent.eventTimeNanos += 1; |
| 5287 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5288 | |
| 5289 | verifiedEvent.displayId += 1; |
| 5290 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5291 | |
| 5292 | verifiedEvent.action += 1; |
| 5293 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5294 | |
| 5295 | verifiedEvent.downTimeNanos += 1; |
| 5296 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5297 | |
| 5298 | verifiedEvent.flags += 1; |
| 5299 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5300 | |
| 5301 | verifiedEvent.keyCode += 1; |
| 5302 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5303 | |
| 5304 | verifiedEvent.scanCode += 1; |
| 5305 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5306 | |
| 5307 | verifiedEvent.metaState += 1; |
| 5308 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5309 | |
| 5310 | verifiedEvent.repeatCount += 1; |
| 5311 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5312 | } |
| 5313 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5314 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 5315 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5316 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5317 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5318 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5319 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5320 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5321 | |
| 5322 | // Top window is also focusable but is not granted focus. |
| 5323 | windowTop->setFocusable(true); |
| 5324 | windowSecond->setFocusable(true); |
| 5325 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5326 | setFocusedWindow(windowSecond); |
| 5327 | |
| 5328 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5330 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5331 | |
| 5332 | // Focused window should receive event. |
| 5333 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5334 | windowTop->assertNoEvents(); |
| 5335 | } |
| 5336 | |
| 5337 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5338 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5339 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5340 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5341 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5342 | |
| 5343 | window->setFocusable(true); |
| 5344 | // Release channel for window is no longer valid. |
| 5345 | window->releaseChannel(); |
| 5346 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5347 | setFocusedWindow(window); |
| 5348 | |
| 5349 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5350 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5351 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5352 | |
| 5353 | // window channel is invalid, so it should not receive any input event. |
| 5354 | window->assertNoEvents(); |
| 5355 | } |
| 5356 | |
| 5357 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5359 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5360 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5361 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5362 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5363 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5364 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5365 | setFocusedWindow(window); |
| 5366 | |
| 5367 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5368 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5369 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5370 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5371 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5372 | window->assertNoEvents(); |
| 5373 | } |
| 5374 | |
| 5375 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5376 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5377 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5378 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5379 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5380 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5381 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5382 | |
| 5383 | windowTop->setFocusable(true); |
| 5384 | windowSecond->setFocusable(true); |
| 5385 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5386 | setFocusedWindow(windowTop); |
| 5387 | windowTop->consumeFocusEvent(true); |
| 5388 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5389 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
| 5390 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5391 | windowSecond->consumeFocusEvent(true); |
| 5392 | windowTop->consumeFocusEvent(false); |
| 5393 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5394 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5395 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5396 | |
| 5397 | // Focused window should receive event. |
| 5398 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5399 | } |
| 5400 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5401 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5402 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5403 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5404 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5405 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5406 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5407 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5408 | |
| 5409 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5410 | windowSecond->setFocusable(false); |
| 5411 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5412 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5413 | setFocusedWindow(windowTop); |
| 5414 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5415 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5416 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5417 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5418 | |
| 5419 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5420 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5421 | windowSecond->assertNoEvents(); |
| 5422 | } |
| 5423 | |
| 5424 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5425 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5426 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5427 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5428 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5429 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5430 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5431 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5432 | |
| 5433 | window->setFocusable(true); |
| 5434 | previousFocusedWindow->setFocusable(true); |
| 5435 | window->setVisible(false); |
| 5436 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5437 | setFocusedWindow(previousFocusedWindow); |
| 5438 | previousFocusedWindow->consumeFocusEvent(true); |
| 5439 | |
| 5440 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5441 | setFocusedWindow(window); |
| 5442 | previousFocusedWindow->consumeFocusEvent(false); |
| 5443 | |
| 5444 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5445 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5446 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5447 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5448 | |
| 5449 | // Window does not get focus event or key down. |
| 5450 | window->assertNoEvents(); |
| 5451 | |
| 5452 | // Window becomes visible. |
| 5453 | window->setVisible(true); |
| 5454 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5455 | |
| 5456 | // Window receives focus event. |
| 5457 | window->consumeFocusEvent(true); |
| 5458 | // Focused window receives key down. |
| 5459 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5460 | } |
| 5461 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5462 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5464 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5465 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5466 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5467 | |
| 5468 | // window is granted focus. |
| 5469 | window->setFocusable(true); |
| 5470 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5471 | setFocusedWindow(window); |
| 5472 | window->consumeFocusEvent(true); |
| 5473 | |
| 5474 | // When a display is removed window loses focus. |
| 5475 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5476 | window->consumeFocusEvent(false); |
| 5477 | } |
| 5478 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5479 | /** |
| 5480 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5481 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5482 | * of the 'slipperyEnterWindow'. |
| 5483 | * |
| 5484 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5485 | * a way so that the touched location is no longer covered by the top window. |
| 5486 | * |
| 5487 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5488 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5489 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5490 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5491 | * with ACTION_DOWN). |
| 5492 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5493 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5494 | * |
| 5495 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5496 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5497 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5498 | * |
| 5499 | * In this test, we ensure that the event received by the bottom window has |
| 5500 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5501 | */ |
| 5502 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 5503 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5504 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5505 | |
| 5506 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5507 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5508 | |
| 5509 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5510 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5511 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5512 | // Make sure this one overlaps the bottom window |
| 5513 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5514 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5515 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5516 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5517 | |
| 5518 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5519 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5520 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5521 | |
| 5522 | mDispatcher->setInputWindows( |
| 5523 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5524 | |
| 5525 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5526 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5527 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5528 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5529 | slipperyExitWindow->consumeMotionDown(); |
| 5530 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5531 | mDispatcher->setInputWindows( |
| 5532 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5533 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5534 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 5535 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5536 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5537 | |
| 5538 | slipperyExitWindow->consumeMotionCancel(); |
| 5539 | |
| 5540 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5541 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5542 | } |
| 5543 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 5544 | /** |
| 5545 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 5546 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 5547 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 5548 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 5549 | * reproduces a crash. |
| 5550 | */ |
| 5551 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 5552 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5553 | |
| 5554 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 5555 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5556 | leftSlipperyWindow->setSlippery(true); |
| 5557 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5558 | |
| 5559 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 5560 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5561 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 5562 | rightDropTouchesWindow->setDropInput(true); |
| 5563 | |
| 5564 | mDispatcher->setInputWindows( |
| 5565 | {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}}); |
| 5566 | |
| 5567 | // Start touch in the left window |
| 5568 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5569 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5570 | .build()); |
| 5571 | leftSlipperyWindow->consumeMotionDown(); |
| 5572 | |
| 5573 | // And move it into the right window |
| 5574 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5575 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5576 | .build()); |
| 5577 | |
| 5578 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 5579 | // The left window continues to receive the gesture. |
| 5580 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5581 | rightDropTouchesWindow->assertNoEvents(); |
| 5582 | } |
| 5583 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5584 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5585 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5586 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5587 | |
| 5588 | sp<FakeWindowHandle> leftWindow = |
| 5589 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5590 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 5591 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5592 | |
| 5593 | sp<FakeWindowHandle> rightSpy = |
| 5594 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 5595 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 5596 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5597 | rightSpy->setSpy(true); |
| 5598 | rightSpy->setTrustedOverlay(true); |
| 5599 | |
| 5600 | sp<FakeWindowHandle> rightWindow = |
| 5601 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5602 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 5603 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5604 | |
| 5605 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}}); |
| 5606 | |
| 5607 | // Touch in the left window |
| 5608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5609 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5610 | .build()); |
| 5611 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 5612 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5613 | ASSERT_NO_FATAL_FAILURE( |
| 5614 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5615 | |
| 5616 | // Touch another finger over the right windows |
| 5617 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5618 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5619 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5620 | .build()); |
| 5621 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 5622 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 5623 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 5624 | mDispatcher->waitForIdle(); |
| 5625 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5626 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 5627 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5628 | |
| 5629 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 5630 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 5631 | // is part of the UP action, we do not treat this as device interaction. |
| 5632 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5633 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5634 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5635 | .build()); |
| 5636 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 5637 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 5638 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 5639 | mDispatcher->waitForIdle(); |
| 5640 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5641 | |
| 5642 | // Move remaining finger |
| 5643 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5644 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5645 | .build()); |
| 5646 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 5647 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 5648 | mDispatcher->waitForIdle(); |
| 5649 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5650 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5651 | |
| 5652 | // Release all fingers |
| 5653 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5654 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 5655 | .build()); |
| 5656 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 5657 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 5658 | mDispatcher->waitForIdle(); |
| 5659 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5660 | } |
| 5661 | |
| 5662 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 5663 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5664 | |
| 5665 | sp<FakeWindowHandle> window = |
| 5666 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5667 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 5668 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5669 | |
| 5670 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5671 | setFocusedWindow(window); |
| 5672 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 5673 | |
| 5674 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 5675 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 5676 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 5677 | ASSERT_NO_FATAL_FAILURE( |
| 5678 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 5679 | |
| 5680 | // The UP actions are not treated as device interaction. |
| 5681 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 5682 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 5683 | mDispatcher->waitForIdle(); |
| 5684 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 5685 | } |
| 5686 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5687 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5688 | protected: |
| 5689 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5690 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5691 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5692 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5693 | sp<FakeWindowHandle> mWindow; |
| 5694 | |
| 5695 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5696 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5697 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5698 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 5699 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5700 | ASSERT_EQ(OK, mDispatcher->start()); |
| 5701 | |
| 5702 | setUpWindow(); |
| 5703 | } |
| 5704 | |
| 5705 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5706 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5707 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5708 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5709 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5710 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5711 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5712 | mWindow->consumeFocusEvent(true); |
| 5713 | } |
| 5714 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5715 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5716 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5717 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5718 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5719 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5720 | |
| 5721 | // Window should receive key down event. |
| 5722 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5723 | } |
| 5724 | |
| 5725 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 5726 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 5727 | InputEvent* repeatEvent = mWindow->consume(); |
| 5728 | ASSERT_NE(nullptr, repeatEvent); |
| 5729 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5730 | ASSERT_EQ(InputEventType::KEY, repeatEvent->getType()); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5731 | |
| 5732 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 5733 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 5734 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 5735 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 5736 | } |
| 5737 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5738 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5739 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5740 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5741 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5742 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5743 | |
| 5744 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5745 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5746 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5747 | } |
| 5748 | }; |
| 5749 | |
| 5750 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5751 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5752 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5753 | expectKeyRepeatOnce(repeatCount); |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5758 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5759 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5760 | expectKeyRepeatOnce(repeatCount); |
| 5761 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5762 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5763 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5764 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5765 | expectKeyRepeatOnce(repeatCount); |
| 5766 | } |
| 5767 | } |
| 5768 | |
| 5769 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5770 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5771 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5772 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5773 | mWindow->assertNoEvents(); |
| 5774 | } |
| 5775 | |
| 5776 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5777 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5778 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5779 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5780 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5781 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5782 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5783 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5784 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 5785 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5786 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5787 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5788 | mWindow->assertNoEvents(); |
| 5789 | } |
| 5790 | |
| 5791 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5792 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5793 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5794 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5795 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5796 | // 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] | 5797 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5798 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5799 | mWindow->assertNoEvents(); |
| 5800 | } |
| 5801 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5802 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 5803 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5804 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5805 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5806 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 5807 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 5808 | mWindow->assertNoEvents(); |
| 5809 | } |
| 5810 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5811 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5812 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5813 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5814 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5815 | InputEvent* repeatEvent = mWindow->consume(); |
| 5816 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5817 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 5818 | IdGenerator::getSource(repeatEvent->getId())); |
| 5819 | } |
| 5820 | } |
| 5821 | |
| 5822 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5823 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5824 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5825 | |
| 5826 | std::unordered_set<int32_t> idSet; |
| 5827 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5828 | InputEvent* repeatEvent = mWindow->consume(); |
| 5829 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5830 | int32_t id = repeatEvent->getId(); |
| 5831 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 5832 | idSet.insert(id); |
| 5833 | } |
| 5834 | } |
| 5835 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5836 | /* Test InputDispatcher for MultiDisplay */ |
| 5837 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 5838 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5839 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5840 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5841 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5842 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5843 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5844 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5845 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5846 | // Set focus window for primary display, but focused display would be second one. |
| 5847 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5848 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5849 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5850 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5851 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5852 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5853 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5854 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5855 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5856 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5857 | // Set focus display to second one. |
| 5858 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 5859 | // Set focus window for second display. |
| 5860 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5861 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5862 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5863 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5864 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5865 | } |
| 5866 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5867 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5868 | InputDispatcherTest::TearDown(); |
| 5869 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5870 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5871 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5872 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5873 | windowInSecondary.clear(); |
| 5874 | } |
| 5875 | |
| 5876 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5877 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5878 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5879 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5880 | sp<FakeWindowHandle> windowInSecondary; |
| 5881 | }; |
| 5882 | |
| 5883 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 5884 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5885 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5886 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5887 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5888 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5889 | windowInSecondary->assertNoEvents(); |
| 5890 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5891 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5892 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5893 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5894 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5895 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5896 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5897 | } |
| 5898 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5899 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5900 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5902 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5903 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5904 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5905 | windowInSecondary->assertNoEvents(); |
| 5906 | |
| 5907 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5908 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5909 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5910 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5911 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5912 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5913 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5914 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5915 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5916 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5917 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5918 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5919 | |
| 5920 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5921 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5922 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5923 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5924 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5925 | windowInSecondary->assertNoEvents(); |
| 5926 | } |
| 5927 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5928 | // Test per-display input monitors for motion event. |
| 5929 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5930 | FakeMonitorReceiver monitorInPrimary = |
| 5931 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5932 | FakeMonitorReceiver monitorInSecondary = |
| 5933 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5934 | |
| 5935 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5936 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5937 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5939 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5940 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5941 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5942 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5943 | |
| 5944 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5945 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5946 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5947 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5948 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5949 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5950 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5951 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5952 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 5953 | // Lift up the touch from the second display |
| 5954 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5955 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5956 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5957 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5958 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 5959 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5960 | // Test inject a non-pointer motion event. |
| 5961 | // If specific a display, it will dispatch to the focused window of particular display, |
| 5962 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5963 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5964 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 5965 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5966 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5967 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5968 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5969 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | // Test per-display input monitors for key event. |
| 5973 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5974 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5975 | FakeMonitorReceiver monitorInPrimary = |
| 5976 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5977 | FakeMonitorReceiver monitorInSecondary = |
| 5978 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5979 | |
| 5980 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5981 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5982 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5983 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5984 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5985 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5986 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5987 | } |
| 5988 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5989 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 5990 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5991 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5992 | secondWindowInPrimary->setFocusable(true); |
| 5993 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 5994 | setFocusedWindow(secondWindowInPrimary); |
| 5995 | windowInPrimary->consumeFocusEvent(false); |
| 5996 | secondWindowInPrimary->consumeFocusEvent(true); |
| 5997 | |
| 5998 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 6000 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6001 | windowInPrimary->assertNoEvents(); |
| 6002 | windowInSecondary->assertNoEvents(); |
| 6003 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6004 | } |
| 6005 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 6006 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 6007 | FakeMonitorReceiver monitorInPrimary = |
| 6008 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6009 | FakeMonitorReceiver monitorInSecondary = |
| 6010 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6011 | |
| 6012 | // Test touch down on primary display. |
| 6013 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6014 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 6015 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6016 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6017 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6018 | |
| 6019 | // Test touch down on second display. |
| 6020 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6021 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 6022 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6023 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 6024 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 6025 | |
| 6026 | // Trigger cancel touch. |
| 6027 | mDispatcher->cancelCurrentTouch(); |
| 6028 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6029 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6030 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6031 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6032 | |
| 6033 | // Test inject a move motion event, no window/monitor should receive the event. |
| 6034 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6035 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6036 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6037 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 6038 | windowInPrimary->assertNoEvents(); |
| 6039 | monitorInPrimary.assertNoEvents(); |
| 6040 | |
| 6041 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6042 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6043 | SECOND_DISPLAY_ID, {110, 200})) |
| 6044 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 6045 | windowInSecondary->assertNoEvents(); |
| 6046 | monitorInSecondary.assertNoEvents(); |
| 6047 | } |
| 6048 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6049 | class InputFilterTest : public InputDispatcherTest { |
| 6050 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6051 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 6052 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6053 | NotifyMotionArgs motionArgs; |
| 6054 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6055 | motionArgs = |
| 6056 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6057 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6058 | motionArgs = |
| 6059 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6060 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6061 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6062 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6063 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 6064 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6065 | } else { |
| 6066 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 6067 | } |
| 6068 | } |
| 6069 | |
| 6070 | void testNotifyKey(bool expectToBeFiltered) { |
| 6071 | NotifyKeyArgs keyArgs; |
| 6072 | |
| 6073 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6074 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6075 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6076 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6077 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6078 | |
| 6079 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 6080 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 6081 | } else { |
| 6082 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 6083 | } |
| 6084 | } |
| 6085 | }; |
| 6086 | |
| 6087 | // Test InputFilter for MotionEvent |
| 6088 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 6089 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 6090 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 6091 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 6092 | |
| 6093 | // Enable InputFilter |
| 6094 | mDispatcher->setInputFilterEnabled(true); |
| 6095 | // Test touch on both primary and second display, and check if both events are filtered. |
| 6096 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 6097 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 6098 | |
| 6099 | // Disable InputFilter |
| 6100 | mDispatcher->setInputFilterEnabled(false); |
| 6101 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 6102 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 6103 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 6104 | } |
| 6105 | |
| 6106 | // Test InputFilter for KeyEvent |
| 6107 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 6108 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 6109 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 6110 | |
| 6111 | // Enable InputFilter |
| 6112 | mDispatcher->setInputFilterEnabled(true); |
| 6113 | // Send a key event, and check if it is filtered. |
| 6114 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 6115 | |
| 6116 | // Disable InputFilter |
| 6117 | mDispatcher->setInputFilterEnabled(false); |
| 6118 | // Send a key event, and check if it isn't filtered. |
| 6119 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 6120 | } |
| 6121 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6122 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 6123 | // logical display coordinate space. |
| 6124 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 6125 | ui::Transform firstDisplayTransform; |
| 6126 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6127 | ui::Transform secondDisplayTransform; |
| 6128 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 6129 | |
| 6130 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 6131 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 6132 | displayInfos[0].transform = firstDisplayTransform; |
| 6133 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 6134 | displayInfos[1].transform = secondDisplayTransform; |
| 6135 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6136 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 6137 | |
| 6138 | // Enable InputFilter |
| 6139 | mDispatcher->setInputFilterEnabled(true); |
| 6140 | |
| 6141 | // Ensure the correct transforms are used for the displays. |
| 6142 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 6143 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 6144 | } |
| 6145 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6146 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 6147 | protected: |
| 6148 | virtual void SetUp() override { |
| 6149 | InputDispatcherTest::SetUp(); |
| 6150 | |
| 6151 | /** |
| 6152 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 6153 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 6154 | * on. |
| 6155 | */ |
| 6156 | mDispatcher->setInputFilterEnabled(true); |
| 6157 | |
| 6158 | std::shared_ptr<InputApplicationHandle> application = |
| 6159 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6160 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 6161 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6162 | |
| 6163 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6164 | mWindow->setFocusable(true); |
| 6165 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6166 | setFocusedWindow(mWindow); |
| 6167 | mWindow->consumeFocusEvent(true); |
| 6168 | } |
| 6169 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6170 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 6171 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6172 | KeyEvent event; |
| 6173 | |
| 6174 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6175 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 6176 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6177 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6178 | const int32_t additionalPolicyFlags = |
| 6179 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 6180 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6181 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6182 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 6183 | policyFlags | additionalPolicyFlags)); |
| 6184 | |
| 6185 | InputEvent* received = mWindow->consume(); |
| 6186 | ASSERT_NE(nullptr, received); |
| 6187 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6188 | ASSERT_EQ(received->getType(), InputEventType::KEY); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6189 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 6190 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 6191 | } |
| 6192 | |
| 6193 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 6194 | int32_t flags) { |
| 6195 | MotionEvent event; |
| 6196 | PointerProperties pointerProperties[1]; |
| 6197 | PointerCoords pointerCoords[1]; |
| 6198 | pointerProperties[0].clear(); |
| 6199 | pointerProperties[0].id = 0; |
| 6200 | pointerCoords[0].clear(); |
| 6201 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 6202 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 6203 | |
| 6204 | ui::Transform identityTransform; |
| 6205 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6206 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 6207 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 6208 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 6209 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 6210 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 6211 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6212 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 6213 | |
| 6214 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 6215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6216 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6217 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 6218 | policyFlags | additionalPolicyFlags)); |
| 6219 | |
| 6220 | InputEvent* received = mWindow->consume(); |
| 6221 | ASSERT_NE(nullptr, received); |
| 6222 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6223 | ASSERT_EQ(received->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6224 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 6225 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
| 6228 | private: |
| 6229 | sp<FakeWindowHandle> mWindow; |
| 6230 | }; |
| 6231 | |
| 6232 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6233 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 6234 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 6235 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6236 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 6237 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6240 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6241 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6242 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6243 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 6244 | } |
| 6245 | |
| 6246 | TEST_F(InputFilterInjectionPolicyTest, |
| 6247 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 6248 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6249 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6250 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
| 6253 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6254 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 6255 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6256 | } |
| 6257 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6258 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6259 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6260 | InputDispatcherTest::SetUp(); |
| 6261 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6262 | std::shared_ptr<FakeApplicationHandle> application = |
| 6263 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6264 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6265 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6266 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6267 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6268 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6269 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6270 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6271 | |
| 6272 | // Set focused application. |
| 6273 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6274 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6275 | |
| 6276 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6277 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6278 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6279 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6280 | } |
| 6281 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6282 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6283 | InputDispatcherTest::TearDown(); |
| 6284 | |
| 6285 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6286 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6287 | } |
| 6288 | |
| 6289 | protected: |
| 6290 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6291 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6292 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6293 | }; |
| 6294 | |
| 6295 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6296 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 6297 | // the onPointerDownOutsideFocus callback. |
| 6298 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6299 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6300 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6301 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6302 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6303 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6304 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6305 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6306 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 6307 | } |
| 6308 | |
| 6309 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 6310 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 6311 | // onPointerDownOutsideFocus callback. |
| 6312 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6313 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6314 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6315 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6316 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6317 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6318 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6319 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6320 | } |
| 6321 | |
| 6322 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 6323 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 6324 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6325 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6326 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6327 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6328 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6329 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6330 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6331 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6332 | } |
| 6333 | |
| 6334 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6335 | // DOWN on the window that already has focus. Ensure no window received the |
| 6336 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6337 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6339 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6340 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6341 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6342 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6343 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6344 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6345 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6346 | } |
| 6347 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6348 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 6349 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 6350 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 6351 | const MotionEvent event = |
| 6352 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6353 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 6354 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6355 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 6356 | .build(); |
| 6357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 6358 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6359 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6360 | |
| 6361 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6362 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 6363 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 6364 | mUnfocusedWindow->assertNoEvents(); |
| 6365 | } |
| 6366 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6367 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 6368 | // windows that point to the same client token. |
| 6369 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 6370 | virtual void SetUp() override { |
| 6371 | InputDispatcherTest::SetUp(); |
| 6372 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6373 | std::shared_ptr<FakeApplicationHandle> application = |
| 6374 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6375 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 6376 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6377 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 6378 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6379 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 6380 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6381 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 6382 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6383 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6384 | } |
| 6385 | |
| 6386 | protected: |
| 6387 | sp<FakeWindowHandle> mWindow1; |
| 6388 | sp<FakeWindowHandle> mWindow2; |
| 6389 | |
| 6390 | // 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] | 6391 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 6392 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 6393 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6394 | } |
| 6395 | |
| 6396 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 6397 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6398 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6399 | InputEvent* event = window->consume(); |
| 6400 | |
| 6401 | ASSERT_NE(nullptr, event) << name.c_str() |
| 6402 | << ": consumer should have returned non-NULL event."; |
| 6403 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6404 | ASSERT_EQ(InputEventType::MOTION, event->getType()) |
| 6405 | << name.c_str() << ": expected MotionEvent, got " << *event; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6406 | |
| 6407 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6408 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 6409 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6410 | |
| 6411 | for (size_t i = 0; i < points.size(); i++) { |
| 6412 | float expectedX = points[i].x; |
| 6413 | float expectedY = points[i].y; |
| 6414 | |
| 6415 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 6416 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 6417 | << ", got " << motionEvent.getX(i); |
| 6418 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 6419 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 6420 | << ", got " << motionEvent.getY(i); |
| 6421 | } |
| 6422 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6423 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6424 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6425 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6426 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 6427 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6428 | |
| 6429 | // Always consume from window1 since it's the window that has the InputReceiver |
| 6430 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 6431 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6432 | }; |
| 6433 | |
| 6434 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 6435 | // Touch Window 1 |
| 6436 | PointF touchedPoint = {10, 10}; |
| 6437 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6438 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6439 | |
| 6440 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6441 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6442 | |
| 6443 | // Touch Window 2 |
| 6444 | touchedPoint = {150, 150}; |
| 6445 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6446 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6447 | } |
| 6448 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6449 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 6450 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6451 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6452 | |
| 6453 | // Touch Window 1 |
| 6454 | PointF touchedPoint = {10, 10}; |
| 6455 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6456 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6457 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6458 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6459 | |
| 6460 | // Touch Window 2 |
| 6461 | touchedPoint = {150, 150}; |
| 6462 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6463 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 6464 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6465 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6466 | // Update the transform so rotation is set |
| 6467 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6468 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 6469 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6470 | } |
| 6471 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6472 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6473 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6474 | |
| 6475 | // Touch Window 1 |
| 6476 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6477 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6478 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6479 | |
| 6480 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6481 | touchedPoints.push_back(PointF{150, 150}); |
| 6482 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6483 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6484 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6485 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6486 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6487 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6488 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6489 | // Update the transform so rotation is set for Window 2 |
| 6490 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 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 | } |
| 6494 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6495 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6496 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6497 | |
| 6498 | // Touch Window 1 |
| 6499 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6500 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6501 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6502 | |
| 6503 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6504 | touchedPoints.push_back(PointF{150, 150}); |
| 6505 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6506 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6507 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6508 | |
| 6509 | // Move both windows |
| 6510 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6511 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6512 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6513 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6514 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6515 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6516 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6517 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6518 | expectedPoints.pop_back(); |
| 6519 | |
| 6520 | // Touch Window 2 |
| 6521 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6522 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6523 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6524 | |
| 6525 | // Move both windows |
| 6526 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6527 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6528 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6529 | |
| 6530 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 6534 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 6535 | |
| 6536 | // Touch Window 1 |
| 6537 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6538 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6539 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6540 | |
| 6541 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6542 | touchedPoints.push_back(PointF{150, 150}); |
| 6543 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6544 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6545 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6546 | |
| 6547 | // Move both windows |
| 6548 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6549 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6550 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6551 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6552 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6553 | } |
| 6554 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 6555 | /** |
| 6556 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 6557 | * same input channel. |
| 6558 | */ |
| 6559 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 6560 | mWindow1->setSlippery(true); |
| 6561 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6562 | |
| 6563 | // Touch down in window 1 |
| 6564 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6565 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6566 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 6567 | |
| 6568 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 6569 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 6570 | // getting generated. |
| 6571 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6572 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6573 | |
| 6574 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 6575 | } |
| 6576 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 6577 | /** |
| 6578 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 6579 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 6580 | * that the pointer is hovering over may have a different transform. |
| 6581 | */ |
| 6582 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
| 6583 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6584 | |
| 6585 | // Start hover in window 1 |
| 6586 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN, |
| 6587 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6588 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 6589 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
| 6590 | |
| 6591 | // Move hover to window 2. |
| 6592 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6593 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6594 | |
| 6595 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 6596 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 6597 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 6598 | } |
| 6599 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6600 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 6601 | virtual void SetUp() override { |
| 6602 | InputDispatcherTest::SetUp(); |
| 6603 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6604 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6605 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6606 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 6607 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6608 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6609 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6610 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6611 | |
| 6612 | // Set focused application. |
| 6613 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 6614 | |
| 6615 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6616 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6617 | mWindow->consumeFocusEvent(true); |
| 6618 | } |
| 6619 | |
| 6620 | virtual void TearDown() override { |
| 6621 | InputDispatcherTest::TearDown(); |
| 6622 | mWindow.clear(); |
| 6623 | } |
| 6624 | |
| 6625 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6626 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6627 | sp<FakeWindowHandle> mWindow; |
| 6628 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 6629 | |
| 6630 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6631 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6632 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6633 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6634 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6635 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6636 | WINDOW_LOCATION)); |
| 6637 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6638 | |
| 6639 | sp<FakeWindowHandle> addSpyWindow() { |
| 6640 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6641 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6642 | spy->setTrustedOverlay(true); |
| 6643 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6644 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6645 | spy->setDispatchingTimeout(30ms); |
| 6646 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 6647 | return spy; |
| 6648 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6649 | }; |
| 6650 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6651 | // Send a tap and respond, which should not cause an ANR. |
| 6652 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 6653 | tapOnWindow(); |
| 6654 | mWindow->consumeMotionDown(); |
| 6655 | mWindow->consumeMotionUp(); |
| 6656 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6657 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6658 | } |
| 6659 | |
| 6660 | // Send a regular key and respond, which should not cause an ANR. |
| 6661 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6663 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6664 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6665 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6666 | } |
| 6667 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6668 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 6669 | mWindow->setFocusable(false); |
| 6670 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6671 | mWindow->consumeFocusEvent(false); |
| 6672 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6673 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6674 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6675 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6676 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6678 | // Key will not go to window because we have no focused window. |
| 6679 | // The 'no focused window' ANR timer should start instead. |
| 6680 | |
| 6681 | // Now, the focused application goes away. |
| 6682 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 6683 | // The key should get dropped and there should be no ANR. |
| 6684 | |
| 6685 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6686 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6687 | } |
| 6688 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6689 | // 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] | 6690 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 6691 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6692 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6693 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6694 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6695 | WINDOW_LOCATION)); |
| 6696 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6697 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 6698 | ASSERT_TRUE(sequenceNum); |
| 6699 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6700 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6701 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6702 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6703 | mWindow->consumeMotionEvent( |
| 6704 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6705 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6706 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6707 | } |
| 6708 | |
| 6709 | // Send a key to the app and have the app not respond right away. |
| 6710 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 6711 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6713 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 6714 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6715 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6716 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6717 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6718 | } |
| 6719 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6720 | // We have a focused application, but no focused window |
| 6721 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6722 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6723 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6724 | mWindow->consumeFocusEvent(false); |
| 6725 | |
| 6726 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6728 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6729 | WINDOW_LOCATION)); |
| 6730 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 6731 | mDispatcher->waitForIdle(); |
| 6732 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6733 | |
| 6734 | // Once a focused event arrives, we get an ANR for this application |
| 6735 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6736 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6737 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6738 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6739 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6740 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6741 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6742 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6743 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6744 | } |
| 6745 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6746 | /** |
| 6747 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 6748 | * there will not be an ANR. |
| 6749 | */ |
| 6750 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 6751 | mWindow->setFocusable(false); |
| 6752 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6753 | mWindow->consumeFocusEvent(false); |
| 6754 | |
| 6755 | KeyEvent event; |
| 6756 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 6757 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 6758 | |
| 6759 | // Define a valid key down event that is stale (too old). |
| 6760 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 6761 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6762 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6763 | |
| 6764 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 6765 | |
| 6766 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6767 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6768 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 6769 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 6770 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 6771 | << "Injection should fail because the event is stale"; |
| 6772 | |
| 6773 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6774 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6775 | mWindow->assertNoEvents(); |
| 6776 | } |
| 6777 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6778 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6779 | // Make sure that we don't notify policy twice about the same ANR. |
| 6780 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6781 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6782 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6783 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6784 | |
| 6785 | // Once a focused event arrives, we get an ANR for this application |
| 6786 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6787 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6788 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6789 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6790 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6791 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6792 | const std::chrono::duration appTimeout = |
| 6793 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6794 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6795 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6796 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6797 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 6798 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6799 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6800 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 6801 | // '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] | 6802 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6803 | } |
| 6804 | |
| 6805 | // We have a focused application, but no focused window |
| 6806 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6807 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6808 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6809 | mWindow->consumeFocusEvent(false); |
| 6810 | |
| 6811 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6812 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6813 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6814 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6815 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6816 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6817 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6818 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6819 | |
| 6820 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6821 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6822 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6823 | mWindow->assertNoEvents(); |
| 6824 | } |
| 6825 | |
| 6826 | /** |
| 6827 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 6828 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 6829 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 6830 | * the ANR mechanism should still work. |
| 6831 | * |
| 6832 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 6833 | * DOWN event, while not responding on the second one. |
| 6834 | */ |
| 6835 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 6836 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6837 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6838 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6839 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6840 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6841 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6842 | |
| 6843 | // Now send ACTION_UP, with identical timestamp |
| 6844 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6845 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6846 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6847 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6848 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6849 | |
| 6850 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 6851 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6852 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6853 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6854 | } |
| 6855 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6856 | // A spy window can receive an ANR |
| 6857 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 6858 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 6859 | |
| 6860 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6861 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6862 | WINDOW_LOCATION)); |
| 6863 | mWindow->consumeMotionDown(); |
| 6864 | |
| 6865 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 6866 | ASSERT_TRUE(sequenceNum); |
| 6867 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6868 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6869 | |
| 6870 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6871 | spy->consumeMotionEvent( |
| 6872 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6873 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6874 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6875 | } |
| 6876 | |
| 6877 | // 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] | 6878 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6879 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 6880 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6881 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6882 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6883 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6884 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6885 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6886 | |
| 6887 | // Stuck on the ACTION_UP |
| 6888 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6889 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6890 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6891 | // 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] | 6892 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6893 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6894 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6895 | |
| 6896 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6897 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6898 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6899 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6900 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6901 | } |
| 6902 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6903 | // 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] | 6904 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6905 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 6906 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6907 | |
| 6908 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6909 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6910 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6911 | |
| 6912 | mWindow->consumeMotionDown(); |
| 6913 | // Stuck on the ACTION_UP |
| 6914 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6915 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6916 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6917 | // 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] | 6918 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6919 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6920 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6921 | |
| 6922 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6923 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6924 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6925 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6926 | spy->assertNoEvents(); |
| 6927 | } |
| 6928 | |
| 6929 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 6930 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 6931 | |
| 6932 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6933 | |
| 6934 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6935 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6936 | WINDOW_LOCATION)); |
| 6937 | |
| 6938 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6939 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 6940 | ASSERT_TRUE(consumeSeq); |
| 6941 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6942 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6943 | |
| 6944 | monitor.finishEvent(*consumeSeq); |
| 6945 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6946 | |
| 6947 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6948 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6949 | } |
| 6950 | |
| 6951 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 6952 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 6953 | // get an ANR again. |
| 6954 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 6955 | // 2. consume all pending events (= queue becomes healthy again) |
| 6956 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 6957 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 6958 | tapOnWindow(); |
| 6959 | |
| 6960 | mWindow->consumeMotionDown(); |
| 6961 | // Block on ACTION_UP |
| 6962 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6963 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6964 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 6965 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6966 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6967 | mWindow->assertNoEvents(); |
| 6968 | |
| 6969 | tapOnWindow(); |
| 6970 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6971 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6972 | mWindow->consumeMotionUp(); |
| 6973 | |
| 6974 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6975 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6976 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6977 | mWindow->assertNoEvents(); |
| 6978 | } |
| 6979 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6980 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 6981 | // it. |
| 6982 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6983 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6984 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6985 | WINDOW_LOCATION)); |
| 6986 | |
| 6987 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6988 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6989 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6990 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 6991 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6992 | // 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] | 6993 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6994 | mWindow->consumeMotionEvent( |
| 6995 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6996 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6997 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6998 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6999 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7000 | } |
| 7001 | |
| 7002 | /** |
| 7003 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7004 | * not to to the focused window until the motion is processed. |
| 7005 | * |
| 7006 | * Warning!!! |
| 7007 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7008 | * and the injection timeout that we specify when injecting the key. |
| 7009 | * We must have the injection timeout (10ms) be smaller than |
| 7010 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7011 | * |
| 7012 | * If that value changes, this test should also change. |
| 7013 | */ |
| 7014 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 7015 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 7016 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7017 | |
| 7018 | tapOnWindow(); |
| 7019 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 7020 | ASSERT_TRUE(downSequenceNum); |
| 7021 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 7022 | ASSERT_TRUE(upSequenceNum); |
| 7023 | // Don't finish the events yet, and send a key |
| 7024 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 7025 | // window even if motions are still being processed. But because the injection timeout is short, |
| 7026 | // we will receive INJECTION_TIMED_OUT as the result. |
| 7027 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7028 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7029 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7030 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 7031 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7032 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7033 | // and the key remains pending, waiting for the touch events to be processed |
| 7034 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 7035 | ASSERT_FALSE(keySequenceNum); |
| 7036 | |
| 7037 | std::this_thread::sleep_for(500ms); |
| 7038 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 7039 | // to the focused window, even though we have not yet finished the motion event |
| 7040 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7041 | mWindow->finishEvent(*downSequenceNum); |
| 7042 | mWindow->finishEvent(*upSequenceNum); |
| 7043 | } |
| 7044 | |
| 7045 | /** |
| 7046 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7047 | * not go to the focused window until the motion is processed. |
| 7048 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 7049 | * focused window right away. |
| 7050 | */ |
| 7051 | TEST_F(InputDispatcherSingleWindowAnr, |
| 7052 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 7053 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 7054 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 7055 | |
| 7056 | tapOnWindow(); |
| 7057 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 7058 | ASSERT_TRUE(downSequenceNum); |
| 7059 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 7060 | ASSERT_TRUE(upSequenceNum); |
| 7061 | // Don't finish the events yet, and send a key |
| 7062 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7063 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7064 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7065 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7066 | // At this point, key is still pending, and should not be sent to the application yet. |
| 7067 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 7068 | ASSERT_FALSE(keySequenceNum); |
| 7069 | |
| 7070 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 7071 | tapOnWindow(); |
| 7072 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 7073 | // the other events yet. We can finish events in any order. |
| 7074 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 7075 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 7076 | mWindow->consumeMotionDown(); |
| 7077 | mWindow->consumeMotionUp(); |
| 7078 | mWindow->assertNoEvents(); |
| 7079 | } |
| 7080 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 7081 | /** |
| 7082 | * Send an event to the app and have the app not respond right away. |
| 7083 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 7084 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 7085 | * At some point, the window becomes responsive again. |
| 7086 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 7087 | */ |
| 7088 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 7089 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7090 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 7091 | .build()); |
| 7092 | |
| 7093 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 7094 | ASSERT_TRUE(sequenceNum); |
| 7095 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 7096 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 7097 | |
| 7098 | mWindow->finishEvent(*sequenceNum); |
| 7099 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 7100 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7101 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 7102 | |
| 7103 | // Now that the window is responsive, let's continue the gesture. |
| 7104 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7105 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7106 | .build()); |
| 7107 | |
| 7108 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7109 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7110 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 7111 | .build()); |
| 7112 | |
| 7113 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7114 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7115 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 7116 | .build()); |
| 7117 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7118 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 7119 | .build()); |
| 7120 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 7121 | mWindow->assertNoEvents(); |
| 7122 | |
| 7123 | // Start another one. |
| 7124 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7125 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 7126 | .build()); |
| 7127 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7128 | } |
| 7129 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7130 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 7131 | virtual void SetUp() override { |
| 7132 | InputDispatcherTest::SetUp(); |
| 7133 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7134 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7135 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7136 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 7137 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7138 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7139 | // 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] | 7140 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7141 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7142 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 7143 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 7144 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7145 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7146 | |
| 7147 | // Set focused application. |
| 7148 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7149 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7150 | |
| 7151 | // Expect one focus window exist in display. |
| 7152 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7153 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7154 | mFocusedWindow->consumeFocusEvent(true); |
| 7155 | } |
| 7156 | |
| 7157 | virtual void TearDown() override { |
| 7158 | InputDispatcherTest::TearDown(); |
| 7159 | |
| 7160 | mUnfocusedWindow.clear(); |
| 7161 | mFocusedWindow.clear(); |
| 7162 | } |
| 7163 | |
| 7164 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7165 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7166 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 7167 | sp<FakeWindowHandle> mFocusedWindow; |
| 7168 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 7169 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 7170 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 7171 | |
| 7172 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 7173 | |
| 7174 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 7175 | |
| 7176 | private: |
| 7177 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7178 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7179 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7180 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7181 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7182 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7183 | location)); |
| 7184 | } |
| 7185 | }; |
| 7186 | |
| 7187 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 7188 | // should be ANR'd first. |
| 7189 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
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 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7192 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7193 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7194 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7195 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7196 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7197 | // We consumed all events, so no ANR |
| 7198 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7199 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7200 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7201 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7202 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7203 | FOCUSED_WINDOW_LOCATION)); |
| 7204 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7205 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7206 | |
| 7207 | const std::chrono::duration timeout = |
| 7208 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7209 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7210 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 7211 | // sequence to make it consistent |
| 7212 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7213 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7214 | mFocusedWindow->consumeMotionDown(); |
| 7215 | // This cancel is generated because the connection was unresponsive |
| 7216 | mFocusedWindow->consumeMotionCancel(); |
| 7217 | mFocusedWindow->assertNoEvents(); |
| 7218 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7219 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7220 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7221 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7222 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7223 | } |
| 7224 | |
| 7225 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 7226 | // it doesn't matter which order they should have ANR. |
| 7227 | // But we should receive ANR for both. |
| 7228 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 7229 | // Set the timeout for unfocused window to match the focused window |
| 7230 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 7231 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7232 | |
| 7233 | tapOnFocusedWindow(); |
| 7234 | // 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] | 7235 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 7236 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 7237 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7238 | |
| 7239 | // 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] | 7240 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 7241 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 7242 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 7243 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7244 | |
| 7245 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7246 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7247 | |
| 7248 | mFocusedWindow->consumeMotionDown(); |
| 7249 | mFocusedWindow->consumeMotionUp(); |
| 7250 | mUnfocusedWindow->consumeMotionOutside(); |
| 7251 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7252 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 7253 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 7254 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7255 | |
| 7256 | // Both applications should be marked as responsive, in any order |
| 7257 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 7258 | mFocusedWindow->getToken() == responsiveToken2); |
| 7259 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 7260 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 7261 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7262 | } |
| 7263 | |
| 7264 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 7265 | // We should also log an error to account for the dropped event (not tested here). |
| 7266 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 7267 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 7268 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7269 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7270 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7271 | // Receive the events, but don't respond |
| 7272 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 7273 | ASSERT_TRUE(downEventSequenceNum); |
| 7274 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 7275 | ASSERT_TRUE(upEventSequenceNum); |
| 7276 | const std::chrono::duration timeout = |
| 7277 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7278 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7279 | |
| 7280 | // Tap once again |
| 7281 | // 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] | 7282 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7283 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7284 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7285 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7286 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7287 | FOCUSED_WINDOW_LOCATION)); |
| 7288 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 7289 | // valid touch target |
| 7290 | mUnfocusedWindow->assertNoEvents(); |
| 7291 | |
| 7292 | // Consume the first tap |
| 7293 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 7294 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 7295 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7296 | // The second tap did not go to the focused window |
| 7297 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7298 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7299 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7300 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7301 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7302 | } |
| 7303 | |
| 7304 | // If you tap outside of all windows, there will not be ANR |
| 7305 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7306 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7307 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7308 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 7309 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7310 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7311 | } |
| 7312 | |
| 7313 | // Since the focused window is paused, tapping on it should not produce any events |
| 7314 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 7315 | mFocusedWindow->setPaused(true); |
| 7316 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7317 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7318 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7319 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7320 | FOCUSED_WINDOW_LOCATION)); |
| 7321 | |
| 7322 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 7323 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7324 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 7325 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7326 | |
| 7327 | mFocusedWindow->assertNoEvents(); |
| 7328 | mUnfocusedWindow->assertNoEvents(); |
| 7329 | } |
| 7330 | |
| 7331 | /** |
| 7332 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7333 | * not to to the focused window until the motion is processed. |
| 7334 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 7335 | * |
| 7336 | * Warning!!! |
| 7337 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7338 | * and the injection timeout that we specify when injecting the key. |
| 7339 | * We must have the injection timeout (10ms) be smaller than |
| 7340 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7341 | * |
| 7342 | * If that value changes, this test should also change. |
| 7343 | */ |
| 7344 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 7345 | // Set a long ANR timeout to prevent it from triggering |
| 7346 | mFocusedWindow->setDispatchingTimeout(2s); |
| 7347 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7348 | |
| 7349 | tapOnUnfocusedWindow(); |
| 7350 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7351 | ASSERT_TRUE(downSequenceNum); |
| 7352 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7353 | ASSERT_TRUE(upSequenceNum); |
| 7354 | // Don't finish the events yet, and send a key |
| 7355 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 7356 | // window even if motions are still being processed. |
| 7357 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7358 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7359 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7360 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7361 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7362 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7363 | // and the key remains pending, waiting for the touch events to be processed |
| 7364 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 7365 | ASSERT_FALSE(keySequenceNum); |
| 7366 | |
| 7367 | // 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] | 7368 | mFocusedWindow->setFocusable(false); |
| 7369 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7370 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7371 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7372 | |
| 7373 | // Focus events should precede the key events |
| 7374 | mUnfocusedWindow->consumeFocusEvent(true); |
| 7375 | mFocusedWindow->consumeFocusEvent(false); |
| 7376 | |
| 7377 | // Finish the tap events, which should unblock dispatcher |
| 7378 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 7379 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 7380 | |
| 7381 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 7382 | // can finally go to the newly focused "mUnfocusedWindow". |
| 7383 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7384 | mFocusedWindow->assertNoEvents(); |
| 7385 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7386 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7387 | } |
| 7388 | |
| 7389 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 7390 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 7391 | // The other window should not be affected by that. |
| 7392 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 7393 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7394 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7395 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7396 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7397 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7398 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7399 | |
| 7400 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7401 | mDispatcher->notifyMotion( |
| 7402 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7403 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7404 | |
| 7405 | const std::chrono::duration timeout = |
| 7406 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7407 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7408 | |
| 7409 | mUnfocusedWindow->consumeMotionDown(); |
| 7410 | mFocusedWindow->consumeMotionDown(); |
| 7411 | // Focused window may or may not receive ACTION_MOVE |
| 7412 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 7413 | InputEvent* event; |
| 7414 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 7415 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 7416 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 7417 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7418 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7419 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 7420 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 7421 | mFocusedWindow->consumeMotionCancel(); |
| 7422 | } else { |
| 7423 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 7424 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7425 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7426 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7427 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7428 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7429 | mUnfocusedWindow->assertNoEvents(); |
| 7430 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7431 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7432 | } |
| 7433 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7434 | /** |
| 7435 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 7436 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 7437 | * |
| 7438 | * If the user touches another application during this time, the key should be dropped. |
| 7439 | * Next, if a new focused window comes in, without toggling the focused application, |
| 7440 | * then no ANR should occur. |
| 7441 | * |
| 7442 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 7443 | * but in some cases the policy may not update the focused application. |
| 7444 | */ |
| 7445 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 7446 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 7447 | std::make_shared<FakeApplicationHandle>(); |
| 7448 | focusedApplication->setDispatchingTimeout(60ms); |
| 7449 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 7450 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 7451 | mFocusedWindow->setFocusable(false); |
| 7452 | |
| 7453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7454 | mFocusedWindow->consumeFocusEvent(false); |
| 7455 | |
| 7456 | // Send a key. The ANR timer should start because there is no focused window. |
| 7457 | // 'focusedApplication' will get blamed if this timer completes. |
| 7458 | // 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] | 7459 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7460 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7461 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 7462 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7464 | |
| 7465 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 7466 | // then the injected touches won't cause the focused event to get dropped. |
| 7467 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 7468 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 7469 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 7470 | // For this test, it means that the key would get delivered to the window once it becomes |
| 7471 | // focused. |
| 7472 | std::this_thread::sleep_for(10ms); |
| 7473 | |
| 7474 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7475 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7476 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7477 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7478 | |
| 7479 | // We do not consume the motion right away, because that would require dispatcher to first |
| 7480 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 7481 | // Set the focused window first. |
| 7482 | mFocusedWindow->setFocusable(true); |
| 7483 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7484 | setFocusedWindow(mFocusedWindow); |
| 7485 | mFocusedWindow->consumeFocusEvent(true); |
| 7486 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 7487 | // to another application. This could be a bug / behaviour in the policy. |
| 7488 | |
| 7489 | mUnfocusedWindow->consumeMotionDown(); |
| 7490 | |
| 7491 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7492 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 7493 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 7494 | } |
| 7495 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7496 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 7497 | // that has feature NO_INPUT_CHANNEL. |
| 7498 | // Layout: |
| 7499 | // Top (closest to user) |
| 7500 | // mNoInputWindow (above all windows) |
| 7501 | // mBottomWindow |
| 7502 | // Bottom (furthest from user) |
| 7503 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 7504 | virtual void SetUp() override { |
| 7505 | InputDispatcherTest::SetUp(); |
| 7506 | |
| 7507 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7508 | mNoInputWindow = |
| 7509 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7510 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7511 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7512 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7513 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7514 | // It's perfectly valid for this window to not have an associated input channel |
| 7515 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7516 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 7517 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7518 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7519 | |
| 7520 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7521 | } |
| 7522 | |
| 7523 | protected: |
| 7524 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 7525 | sp<FakeWindowHandle> mNoInputWindow; |
| 7526 | sp<FakeWindowHandle> mBottomWindow; |
| 7527 | }; |
| 7528 | |
| 7529 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 7530 | PointF touchedPoint = {10, 10}; |
| 7531 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7532 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7533 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7534 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7535 | |
| 7536 | mNoInputWindow->assertNoEvents(); |
| 7537 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 7538 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 7539 | // and therefore should prevent mBottomWindow from receiving touches |
| 7540 | mBottomWindow->assertNoEvents(); |
| 7541 | } |
| 7542 | |
| 7543 | /** |
| 7544 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 7545 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 7546 | */ |
| 7547 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 7548 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7549 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7550 | "Window with input channel and NO_INPUT_CHANNEL", |
| 7551 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7552 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7553 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7554 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7555 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7556 | |
| 7557 | PointF touchedPoint = {10, 10}; |
| 7558 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7559 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7560 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7561 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7562 | |
| 7563 | mNoInputWindow->assertNoEvents(); |
| 7564 | mBottomWindow->assertNoEvents(); |
| 7565 | } |
| 7566 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7567 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7568 | protected: |
| 7569 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7570 | sp<FakeWindowHandle> mWindow; |
| 7571 | sp<FakeWindowHandle> mMirror; |
| 7572 | |
| 7573 | virtual void SetUp() override { |
| 7574 | InputDispatcherTest::SetUp(); |
| 7575 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7576 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7577 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7578 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7579 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7580 | mWindow->setFocusable(true); |
| 7581 | mMirror->setFocusable(true); |
| 7582 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7583 | } |
| 7584 | }; |
| 7585 | |
| 7586 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7587 | // Request focus on a mirrored window |
| 7588 | setFocusedWindow(mMirror); |
| 7589 | |
| 7590 | // window gets focused |
| 7591 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7592 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7593 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7594 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7595 | } |
| 7596 | |
| 7597 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7598 | // focusable. |
| 7599 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7600 | setFocusedWindow(mMirror); |
| 7601 | |
| 7602 | // window gets focused |
| 7603 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7604 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7605 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7606 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7608 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7609 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7610 | |
| 7611 | mMirror->setFocusable(false); |
| 7612 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7613 | |
| 7614 | // window loses focus since one of the windows associated with the token in not focusable |
| 7615 | mWindow->consumeFocusEvent(false); |
| 7616 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7617 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7618 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7619 | mWindow->assertNoEvents(); |
| 7620 | } |
| 7621 | |
| 7622 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7623 | // invisible. |
| 7624 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 7625 | setFocusedWindow(mMirror); |
| 7626 | |
| 7627 | // window gets focused |
| 7628 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7630 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7631 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7633 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7634 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7635 | |
| 7636 | mMirror->setVisible(false); |
| 7637 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7638 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7639 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7640 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7641 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7643 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7644 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7645 | |
| 7646 | mWindow->setVisible(false); |
| 7647 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7648 | |
| 7649 | // window loses focus only after all windows associated with the token become invisible. |
| 7650 | mWindow->consumeFocusEvent(false); |
| 7651 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7652 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7653 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7654 | mWindow->assertNoEvents(); |
| 7655 | } |
| 7656 | |
| 7657 | // A focused & mirrored window remains focused until both windows are removed. |
| 7658 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7659 | setFocusedWindow(mMirror); |
| 7660 | |
| 7661 | // window gets focused |
| 7662 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7664 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7665 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7667 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7668 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7669 | |
| 7670 | // single window is removed but the window token remains focused |
| 7671 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7672 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7674 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7675 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7677 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7678 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7679 | |
| 7680 | // Both windows are removed |
| 7681 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7682 | mWindow->consumeFocusEvent(false); |
| 7683 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7684 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7685 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7686 | mWindow->assertNoEvents(); |
| 7687 | } |
| 7688 | |
| 7689 | // Focus request can be pending until one window becomes visible. |
| 7690 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7691 | // Request focus on an invisible mirror. |
| 7692 | mWindow->setVisible(false); |
| 7693 | mMirror->setVisible(false); |
| 7694 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7695 | setFocusedWindow(mMirror); |
| 7696 | |
| 7697 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7698 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7699 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7700 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7701 | |
| 7702 | mMirror->setVisible(true); |
| 7703 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7704 | |
| 7705 | // window gets focused |
| 7706 | mWindow->consumeFocusEvent(true); |
| 7707 | // window gets the pending key event |
| 7708 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7709 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7710 | |
| 7711 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7712 | protected: |
| 7713 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7714 | sp<FakeWindowHandle> mWindow; |
| 7715 | sp<FakeWindowHandle> mSecondWindow; |
| 7716 | |
| 7717 | void SetUp() override { |
| 7718 | InputDispatcherTest::SetUp(); |
| 7719 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7720 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7721 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7722 | mSecondWindow = |
| 7723 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7724 | mSecondWindow->setFocusable(true); |
| 7725 | |
| 7726 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7727 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7728 | |
| 7729 | setFocusedWindow(mWindow); |
| 7730 | mWindow->consumeFocusEvent(true); |
| 7731 | } |
| 7732 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7733 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7734 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7735 | } |
| 7736 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7737 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7738 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7739 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7740 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7741 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7742 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7743 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7744 | } |
| 7745 | }; |
| 7746 | |
| 7747 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7748 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7749 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7750 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7751 | mSecondWindow->assertNoEvents(); |
| 7752 | |
| 7753 | // Ensure that capture can be enabled from the focus window. |
| 7754 | requestAndVerifyPointerCapture(mWindow, true); |
| 7755 | |
| 7756 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7757 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7758 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7759 | |
| 7760 | // Ensure that capture can be disabled from the window with capture. |
| 7761 | requestAndVerifyPointerCapture(mWindow, false); |
| 7762 | } |
| 7763 | |
| 7764 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7765 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7766 | |
| 7767 | setFocusedWindow(mSecondWindow); |
| 7768 | |
| 7769 | // Ensure that the capture disabled event was sent first. |
| 7770 | mWindow->consumeCaptureEvent(false); |
| 7771 | mWindow->consumeFocusEvent(false); |
| 7772 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7773 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7774 | |
| 7775 | // 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] | 7776 | notifyPointerCaptureChanged({}); |
| 7777 | notifyPointerCaptureChanged(request); |
| 7778 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7779 | mWindow->assertNoEvents(); |
| 7780 | mSecondWindow->assertNoEvents(); |
| 7781 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7782 | } |
| 7783 | |
| 7784 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7785 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7786 | |
| 7787 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7788 | notifyPointerCaptureChanged({}); |
| 7789 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7790 | |
| 7791 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7792 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7793 | mWindow->consumeCaptureEvent(false); |
| 7794 | mWindow->assertNoEvents(); |
| 7795 | } |
| 7796 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7797 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7798 | requestAndVerifyPointerCapture(mWindow, true); |
| 7799 | |
| 7800 | // The first window loses focus. |
| 7801 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7802 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7803 | mWindow->consumeCaptureEvent(false); |
| 7804 | |
| 7805 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7806 | // arrives. |
| 7807 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7808 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7809 | |
| 7810 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7811 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7812 | |
| 7813 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7814 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7815 | |
| 7816 | mSecondWindow->consumeFocusEvent(true); |
| 7817 | mSecondWindow->consumeCaptureEvent(true); |
| 7818 | } |
| 7819 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7820 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7821 | // App repeatedly enables and disables capture. |
| 7822 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7823 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7824 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7825 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7826 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7827 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7828 | |
| 7829 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7830 | // first request is now stale, this should do nothing. |
| 7831 | notifyPointerCaptureChanged(firstRequest); |
| 7832 | mWindow->assertNoEvents(); |
| 7833 | |
| 7834 | // InputReader notifies that the second request was enabled. |
| 7835 | notifyPointerCaptureChanged(secondRequest); |
| 7836 | mWindow->consumeCaptureEvent(true); |
| 7837 | } |
| 7838 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7839 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7840 | requestAndVerifyPointerCapture(mWindow, true); |
| 7841 | |
| 7842 | // App toggles pointer capture off and on. |
| 7843 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7844 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7845 | |
| 7846 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7847 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7848 | |
| 7849 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7850 | // preceding "disable" request. |
| 7851 | notifyPointerCaptureChanged(enableRequest); |
| 7852 | |
| 7853 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7854 | // any notifications. |
| 7855 | mWindow->assertNoEvents(); |
| 7856 | } |
| 7857 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7858 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7859 | protected: |
| 7860 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7861 | |
| 7862 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7863 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7864 | |
| 7865 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7866 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7867 | |
| 7868 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7869 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7870 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7871 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7872 | MAXIMUM_OBSCURING_OPACITY); |
| 7873 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7874 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 7875 | static constexpr gui::Uid APP_B_UID{10002}; |
| 7876 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7877 | |
| 7878 | sp<FakeWindowHandle> mTouchWindow; |
| 7879 | |
| 7880 | virtual void SetUp() override { |
| 7881 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7882 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7883 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7884 | } |
| 7885 | |
| 7886 | virtual void TearDown() override { |
| 7887 | InputDispatcherTest::TearDown(); |
| 7888 | mTouchWindow.clear(); |
| 7889 | } |
| 7890 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7891 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7892 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7893 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7894 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7895 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7896 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7897 | return window; |
| 7898 | } |
| 7899 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7900 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7901 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7902 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7903 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7904 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 7905 | window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7906 | return window; |
| 7907 | } |
| 7908 | |
| 7909 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7910 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7911 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7912 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7913 | } |
| 7914 | }; |
| 7915 | |
| 7916 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7917 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7918 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7919 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7920 | |
| 7921 | touch(); |
| 7922 | |
| 7923 | mTouchWindow->assertNoEvents(); |
| 7924 | } |
| 7925 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7926 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7927 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7928 | const sp<FakeWindowHandle>& w = |
| 7929 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7930 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7931 | |
| 7932 | touch(); |
| 7933 | |
| 7934 | mTouchWindow->assertNoEvents(); |
| 7935 | } |
| 7936 | |
| 7937 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7938 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7939 | const sp<FakeWindowHandle>& w = |
| 7940 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7941 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7942 | |
| 7943 | touch(); |
| 7944 | |
| 7945 | w->assertNoEvents(); |
| 7946 | } |
| 7947 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7948 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7949 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7950 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7951 | |
| 7952 | touch(); |
| 7953 | |
| 7954 | mTouchWindow->consumeAnyMotionDown(); |
| 7955 | } |
| 7956 | |
| 7957 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7958 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7959 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7960 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7961 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7962 | |
| 7963 | touch({PointF{100, 100}}); |
| 7964 | |
| 7965 | mTouchWindow->consumeAnyMotionDown(); |
| 7966 | } |
| 7967 | |
| 7968 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7969 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7970 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7971 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7972 | |
| 7973 | touch(); |
| 7974 | |
| 7975 | mTouchWindow->consumeAnyMotionDown(); |
| 7976 | } |
| 7977 | |
| 7978 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 7979 | const sp<FakeWindowHandle>& w = |
| 7980 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7981 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7982 | |
| 7983 | touch(); |
| 7984 | |
| 7985 | mTouchWindow->consumeAnyMotionDown(); |
| 7986 | } |
| 7987 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7988 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 7989 | const sp<FakeWindowHandle>& w = |
| 7990 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7991 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7992 | |
| 7993 | touch(); |
| 7994 | |
| 7995 | w->assertNoEvents(); |
| 7996 | } |
| 7997 | |
| 7998 | /** |
| 7999 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 8000 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 8001 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 8002 | */ |
| 8003 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8004 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 8005 | const sp<FakeWindowHandle>& w = |
| 8006 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8007 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8008 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8009 | |
| 8010 | touch(); |
| 8011 | |
| 8012 | w->consumeMotionOutside(); |
| 8013 | } |
| 8014 | |
| 8015 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 8016 | const sp<FakeWindowHandle>& w = |
| 8017 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8018 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8019 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8020 | |
| 8021 | touch(); |
| 8022 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8023 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 8024 | } |
| 8025 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8026 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8027 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8028 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8029 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8030 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8031 | |
| 8032 | touch(); |
| 8033 | |
| 8034 | mTouchWindow->consumeAnyMotionDown(); |
| 8035 | } |
| 8036 | |
| 8037 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 8038 | const sp<FakeWindowHandle>& w = |
| 8039 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8040 | MAXIMUM_OBSCURING_OPACITY); |
| 8041 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8042 | |
| 8043 | touch(); |
| 8044 | |
| 8045 | mTouchWindow->consumeAnyMotionDown(); |
| 8046 | } |
| 8047 | |
| 8048 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8049 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8050 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8051 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8052 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8053 | |
| 8054 | touch(); |
| 8055 | |
| 8056 | mTouchWindow->assertNoEvents(); |
| 8057 | } |
| 8058 | |
| 8059 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 8060 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 8061 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8062 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8063 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8064 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8065 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8066 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8067 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8068 | |
| 8069 | touch(); |
| 8070 | |
| 8071 | mTouchWindow->assertNoEvents(); |
| 8072 | } |
| 8073 | |
| 8074 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 8075 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 8076 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8077 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 8078 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8079 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8080 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 8081 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8082 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8083 | |
| 8084 | touch(); |
| 8085 | |
| 8086 | mTouchWindow->consumeAnyMotionDown(); |
| 8087 | } |
| 8088 | |
| 8089 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8090 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 8091 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8092 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8093 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8094 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8095 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8096 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8097 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 8098 | |
| 8099 | touch(); |
| 8100 | |
| 8101 | mTouchWindow->consumeAnyMotionDown(); |
| 8102 | } |
| 8103 | |
| 8104 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 8105 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8106 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8107 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8108 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8109 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8110 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 8111 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 8112 | |
| 8113 | touch(); |
| 8114 | |
| 8115 | mTouchWindow->assertNoEvents(); |
| 8116 | } |
| 8117 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8118 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8119 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 8120 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8121 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8122 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8123 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8124 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8125 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8126 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 8127 | |
| 8128 | touch(); |
| 8129 | |
| 8130 | mTouchWindow->assertNoEvents(); |
| 8131 | } |
| 8132 | |
| 8133 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8134 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 8135 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8136 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8137 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8138 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8139 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8140 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8141 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 8142 | |
| 8143 | touch(); |
| 8144 | |
| 8145 | mTouchWindow->consumeAnyMotionDown(); |
| 8146 | } |
| 8147 | |
| 8148 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 8149 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8150 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 8151 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 8152 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8153 | |
| 8154 | touch(); |
| 8155 | |
| 8156 | mTouchWindow->consumeAnyMotionDown(); |
| 8157 | } |
| 8158 | |
| 8159 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 8160 | const sp<FakeWindowHandle>& w = |
| 8161 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 8162 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8163 | |
| 8164 | touch(); |
| 8165 | |
| 8166 | mTouchWindow->consumeAnyMotionDown(); |
| 8167 | } |
| 8168 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8169 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8170 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 8171 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 8172 | const sp<FakeWindowHandle>& w = |
| 8173 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 8174 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8175 | |
| 8176 | touch(); |
| 8177 | |
| 8178 | mTouchWindow->assertNoEvents(); |
| 8179 | } |
| 8180 | |
| 8181 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 8182 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 8183 | const sp<FakeWindowHandle>& w = |
| 8184 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 8185 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8186 | |
| 8187 | touch(); |
| 8188 | |
| 8189 | mTouchWindow->consumeAnyMotionDown(); |
| 8190 | } |
| 8191 | |
| 8192 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8193 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 8194 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 8195 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 8196 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8197 | OPACITY_ABOVE_THRESHOLD); |
| 8198 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8199 | |
| 8200 | touch(); |
| 8201 | |
| 8202 | mTouchWindow->consumeAnyMotionDown(); |
| 8203 | } |
| 8204 | |
| 8205 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8206 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 8207 | const sp<FakeWindowHandle>& w1 = |
| 8208 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 8209 | OPACITY_BELOW_THRESHOLD); |
| 8210 | const sp<FakeWindowHandle>& w2 = |
| 8211 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 8212 | OPACITY_BELOW_THRESHOLD); |
| 8213 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 8214 | |
| 8215 | touch(); |
| 8216 | |
| 8217 | mTouchWindow->assertNoEvents(); |
| 8218 | } |
| 8219 | |
| 8220 | /** |
| 8221 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 8222 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 8223 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 8224 | */ |
| 8225 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8226 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 8227 | const sp<FakeWindowHandle>& wB = |
| 8228 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 8229 | OPACITY_BELOW_THRESHOLD); |
| 8230 | const sp<FakeWindowHandle>& wC = |
| 8231 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 8232 | OPACITY_BELOW_THRESHOLD); |
| 8233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 8234 | |
| 8235 | touch(); |
| 8236 | |
| 8237 | mTouchWindow->assertNoEvents(); |
| 8238 | } |
| 8239 | |
| 8240 | /** |
| 8241 | * This test is testing that a window from a different UID but with same application token doesn't |
| 8242 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 8243 | */ |
| 8244 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 8245 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 8246 | const sp<FakeWindowHandle>& w = |
| 8247 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 8248 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 8249 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8250 | |
| 8251 | touch(); |
| 8252 | |
| 8253 | mTouchWindow->consumeAnyMotionDown(); |
| 8254 | } |
| 8255 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8256 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 8257 | protected: |
| 8258 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8259 | sp<FakeWindowHandle> mWindow; |
| 8260 | sp<FakeWindowHandle> mSecondWindow; |
| 8261 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8262 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8263 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 8264 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8265 | |
| 8266 | void SetUp() override { |
| 8267 | InputDispatcherTest::SetUp(); |
| 8268 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8269 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8270 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8271 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8272 | mSecondWindow = |
| 8273 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8274 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8275 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8276 | mSpyWindow = |
| 8277 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8278 | mSpyWindow->setSpy(true); |
| 8279 | mSpyWindow->setTrustedOverlay(true); |
| 8280 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 8281 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8282 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8283 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8284 | } |
| 8285 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8286 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 8287 | switch (fromSource) { |
| 8288 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 8289 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8290 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8291 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8292 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8293 | break; |
| 8294 | case AINPUT_SOURCE_STYLUS: |
| 8295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8296 | injectMotionEvent( |
| 8297 | mDispatcher, |
| 8298 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8299 | AINPUT_SOURCE_STYLUS) |
| 8300 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8301 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8302 | .x(50) |
| 8303 | .y(50)) |
| 8304 | .build())); |
| 8305 | break; |
| 8306 | case AINPUT_SOURCE_MOUSE: |
| 8307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8308 | injectMotionEvent( |
| 8309 | mDispatcher, |
| 8310 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8311 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8312 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8313 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8314 | .x(50) |
| 8315 | .y(50)) |
| 8316 | .build())); |
| 8317 | break; |
| 8318 | default: |
| 8319 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 8320 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8321 | |
| 8322 | // Window should receive motion event. |
| 8323 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8324 | // Spy window should also receive motion event |
| 8325 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8326 | } |
| 8327 | |
| 8328 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 8329 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 8330 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8331 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8332 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8333 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8334 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8335 | |
| 8336 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8337 | mDragWindow = |
| 8338 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8339 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8340 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8341 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8342 | |
| 8343 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8344 | bool transferred = |
| 8345 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8346 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8347 | if (transferred) { |
| 8348 | mWindow->consumeMotionCancel(); |
| 8349 | mDragWindow->consumeMotionDown(); |
| 8350 | } |
| 8351 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8352 | } |
| 8353 | }; |
| 8354 | |
| 8355 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8356 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8357 | |
| 8358 | // Move on window. |
| 8359 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8360 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8361 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8362 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8363 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8364 | mWindow->consumeDragEvent(false, 50, 50); |
| 8365 | mSecondWindow->assertNoEvents(); |
| 8366 | |
| 8367 | // Move to another window. |
| 8368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8369 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8370 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8371 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8372 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8373 | mWindow->consumeDragEvent(true, 150, 50); |
| 8374 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8375 | |
| 8376 | // Move back to original window. |
| 8377 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8378 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8379 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8380 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8381 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8382 | mWindow->consumeDragEvent(false, 50, 50); |
| 8383 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 8384 | |
| 8385 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8386 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8387 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8388 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8389 | mWindow->assertNoEvents(); |
| 8390 | mSecondWindow->assertNoEvents(); |
| 8391 | } |
| 8392 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8393 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8394 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8395 | |
| 8396 | // No cancel event after drag start |
| 8397 | mSpyWindow->assertNoEvents(); |
| 8398 | |
| 8399 | const MotionEvent secondFingerDownEvent = |
| 8400 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8401 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8402 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8403 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8404 | .build(); |
| 8405 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8406 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8407 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8408 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8409 | |
| 8410 | // Receives cancel for first pointer after next pointer down |
| 8411 | mSpyWindow->consumeMotionCancel(); |
| 8412 | mSpyWindow->consumeMotionDown(); |
| 8413 | |
| 8414 | mSpyWindow->assertNoEvents(); |
| 8415 | } |
| 8416 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8417 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8418 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8419 | |
| 8420 | // Move on window. |
| 8421 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8422 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8423 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8424 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8425 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8426 | mWindow->consumeDragEvent(false, 50, 50); |
| 8427 | mSecondWindow->assertNoEvents(); |
| 8428 | |
| 8429 | // Move to another window. |
| 8430 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8431 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8432 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8433 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8434 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8435 | mWindow->consumeDragEvent(true, 150, 50); |
| 8436 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8437 | |
| 8438 | // drop to another window. |
| 8439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8440 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8441 | {150, 50})) |
| 8442 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8443 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8444 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8445 | mWindow->assertNoEvents(); |
| 8446 | mSecondWindow->assertNoEvents(); |
| 8447 | } |
| 8448 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8449 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8450 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8451 | |
| 8452 | // Move on window and keep button pressed. |
| 8453 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8454 | injectMotionEvent(mDispatcher, |
| 8455 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8456 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8457 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8458 | .build())) |
| 8459 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8460 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8461 | mWindow->consumeDragEvent(false, 50, 50); |
| 8462 | mSecondWindow->assertNoEvents(); |
| 8463 | |
| 8464 | // Move to another window and release button, expect to drop item. |
| 8465 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8466 | injectMotionEvent(mDispatcher, |
| 8467 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8468 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8469 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8470 | .build())) |
| 8471 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8472 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8473 | mWindow->assertNoEvents(); |
| 8474 | mSecondWindow->assertNoEvents(); |
| 8475 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8476 | |
| 8477 | // nothing to the window. |
| 8478 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8479 | injectMotionEvent(mDispatcher, |
| 8480 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 8481 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8482 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8483 | .build())) |
| 8484 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8485 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8486 | mWindow->assertNoEvents(); |
| 8487 | mSecondWindow->assertNoEvents(); |
| 8488 | } |
| 8489 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8490 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8491 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8492 | |
| 8493 | // Set second window invisible. |
| 8494 | mSecondWindow->setVisible(false); |
| 8495 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 8496 | |
| 8497 | // Move on window. |
| 8498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8499 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8500 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8501 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8502 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8503 | mWindow->consumeDragEvent(false, 50, 50); |
| 8504 | mSecondWindow->assertNoEvents(); |
| 8505 | |
| 8506 | // Move to another window. |
| 8507 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8508 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8509 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8510 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8511 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8512 | mWindow->consumeDragEvent(true, 150, 50); |
| 8513 | mSecondWindow->assertNoEvents(); |
| 8514 | |
| 8515 | // drop to another window. |
| 8516 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8517 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8518 | {150, 50})) |
| 8519 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8520 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8521 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 8522 | mWindow->assertNoEvents(); |
| 8523 | mSecondWindow->assertNoEvents(); |
| 8524 | } |
| 8525 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8526 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8527 | // Ensure window could track pointerIds if it didn't support split touch. |
| 8528 | mWindow->setPreventSplitting(true); |
| 8529 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8530 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8531 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8532 | {50, 50})) |
| 8533 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8534 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8535 | |
| 8536 | const MotionEvent secondFingerDownEvent = |
| 8537 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8538 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8539 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8540 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8541 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8542 | .build(); |
| 8543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8544 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8545 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8546 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8547 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8548 | |
| 8549 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8550 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8551 | } |
| 8552 | |
| 8553 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 8554 | // First down on second window. |
| 8555 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8556 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8557 | {150, 50})) |
| 8558 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8559 | |
| 8560 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8561 | |
| 8562 | // Second down on first window. |
| 8563 | const MotionEvent secondFingerDownEvent = |
| 8564 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8565 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8566 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8567 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8568 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8569 | .build(); |
| 8570 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8571 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8572 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8573 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8574 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8575 | |
| 8576 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8577 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8578 | |
| 8579 | // Move on window. |
| 8580 | const MotionEvent secondFingerMoveEvent = |
| 8581 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8582 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8583 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8584 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8585 | .build(); |
| 8586 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8587 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8588 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8589 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8590 | mWindow->consumeDragEvent(false, 50, 50); |
| 8591 | mSecondWindow->consumeMotionMove(); |
| 8592 | |
| 8593 | // Release the drag pointer should perform drop. |
| 8594 | const MotionEvent secondFingerUpEvent = |
| 8595 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8596 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8597 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8598 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8599 | .build(); |
| 8600 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8601 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8602 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8603 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8604 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 8605 | mWindow->assertNoEvents(); |
| 8606 | mSecondWindow->consumeMotionMove(); |
| 8607 | } |
| 8608 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8609 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8610 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8611 | |
| 8612 | // Update window of second display. |
| 8613 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8614 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8615 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8616 | |
| 8617 | // Let second display has a touch state. |
| 8618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8619 | injectMotionEvent(mDispatcher, |
| 8620 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8621 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8622 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8623 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8624 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8625 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8626 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8627 | // Update window again. |
| 8628 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8629 | |
| 8630 | // Move on window. |
| 8631 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8632 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8633 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8634 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8635 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8636 | mWindow->consumeDragEvent(false, 50, 50); |
| 8637 | mSecondWindow->assertNoEvents(); |
| 8638 | |
| 8639 | // Move to another window. |
| 8640 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8641 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8642 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8643 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8644 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8645 | mWindow->consumeDragEvent(true, 150, 50); |
| 8646 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8647 | |
| 8648 | // drop to another window. |
| 8649 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8650 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8651 | {150, 50})) |
| 8652 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8653 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8654 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8655 | mWindow->assertNoEvents(); |
| 8656 | mSecondWindow->assertNoEvents(); |
| 8657 | } |
| 8658 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8659 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8660 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8661 | // Move on window. |
| 8662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8663 | injectMotionEvent(mDispatcher, |
| 8664 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8665 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8666 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8667 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8668 | .x(50) |
| 8669 | .y(50)) |
| 8670 | .build())) |
| 8671 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8672 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8673 | mWindow->consumeDragEvent(false, 50, 50); |
| 8674 | mSecondWindow->assertNoEvents(); |
| 8675 | |
| 8676 | // Move to another window. |
| 8677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8678 | injectMotionEvent(mDispatcher, |
| 8679 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8680 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8681 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8682 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8683 | .x(150) |
| 8684 | .y(50)) |
| 8685 | .build())) |
| 8686 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8687 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8688 | mWindow->consumeDragEvent(true, 150, 50); |
| 8689 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8690 | |
| 8691 | // drop to another window. |
| 8692 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8693 | injectMotionEvent(mDispatcher, |
| 8694 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8695 | .buttonState(0) |
| 8696 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8697 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8698 | .x(150) |
| 8699 | .y(50)) |
| 8700 | .build())) |
| 8701 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8702 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8703 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8704 | mWindow->assertNoEvents(); |
| 8705 | mSecondWindow->assertNoEvents(); |
| 8706 | } |
| 8707 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8708 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8709 | |
| 8710 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8711 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8712 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8713 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8714 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8715 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8716 | window->setFocusable(true); |
| 8717 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8718 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8719 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8720 | |
| 8721 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8722 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8723 | window->assertNoEvents(); |
| 8724 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8725 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8726 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8727 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 8728 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8729 | window->assertNoEvents(); |
| 8730 | |
| 8731 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8732 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8733 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8734 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8735 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8736 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8737 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8738 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8739 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8740 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8741 | window->assertNoEvents(); |
| 8742 | } |
| 8743 | |
| 8744 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8745 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8746 | std::make_shared<FakeApplicationHandle>(); |
| 8747 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8748 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8749 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8750 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8751 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8752 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8753 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8754 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8755 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8756 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8757 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8758 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8759 | window->setFocusable(true); |
| 8760 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8761 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8762 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8763 | |
| 8764 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8765 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8766 | window->assertNoEvents(); |
| 8767 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8768 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8769 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8770 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 8771 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8772 | window->assertNoEvents(); |
| 8773 | |
| 8774 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8775 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8776 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8777 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8778 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8779 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8780 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8781 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8782 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8783 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8784 | window->assertNoEvents(); |
| 8785 | } |
| 8786 | |
| 8787 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8788 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8789 | std::make_shared<FakeApplicationHandle>(); |
| 8790 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8791 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8792 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8793 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8794 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8795 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8796 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8797 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8798 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8799 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8800 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8801 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8802 | window->setFocusable(true); |
| 8803 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8804 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8805 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8806 | |
| 8807 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8808 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8809 | window->assertNoEvents(); |
| 8810 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8811 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8812 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 8813 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 8814 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8815 | window->assertNoEvents(); |
| 8816 | |
| 8817 | // When the window is no longer obscured because it went on top, it should get input |
| 8818 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8819 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8820 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8821 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8822 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8823 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8824 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8825 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8826 | window->assertNoEvents(); |
| 8827 | } |
| 8828 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8829 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8830 | protected: |
| 8831 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8832 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8833 | sp<FakeWindowHandle> mWindow; |
| 8834 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8835 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8836 | |
| 8837 | void SetUp() override { |
| 8838 | InputDispatcherTest::SetUp(); |
| 8839 | |
| 8840 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8841 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8842 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8843 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8844 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8845 | mSecondWindow = |
| 8846 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8847 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8848 | mThirdWindow = |
| 8849 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8850 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8851 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8852 | |
| 8853 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8854 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8855 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8856 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8857 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8858 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8859 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8860 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8861 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8862 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8863 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8864 | mThirdWindow->assertNoEvents(); |
| 8865 | } |
| 8866 | |
| 8867 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8868 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8869 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8870 | SECOND_DISPLAY_ID)) { |
| 8871 | mWindow->assertNoEvents(); |
| 8872 | mSecondWindow->assertNoEvents(); |
| 8873 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8874 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8875 | } |
| 8876 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8877 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8878 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8879 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8880 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8881 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8882 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8883 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8884 | } |
| 8885 | }; |
| 8886 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8887 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8888 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8889 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8890 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8891 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8892 | } |
| 8893 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8894 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8895 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8896 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8897 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8898 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8899 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8900 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8901 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8902 | mWindow->assertNoEvents(); |
| 8903 | mSecondWindow->assertNoEvents(); |
| 8904 | } |
| 8905 | |
| 8906 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8907 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8908 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8909 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 8910 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8911 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8912 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8913 | } |
| 8914 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8915 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8916 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8917 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8918 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8919 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8920 | mWindow->assertNoEvents(); |
| 8921 | mSecondWindow->assertNoEvents(); |
| 8922 | } |
| 8923 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8924 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8925 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8926 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8927 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8928 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8929 | mWindow->assertNoEvents(); |
| 8930 | mSecondWindow->assertNoEvents(); |
| 8931 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8932 | } |
| 8933 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8934 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8935 | // Interact with the window first. |
| 8936 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8937 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8938 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8939 | |
| 8940 | // Then remove focus. |
| 8941 | mWindow->setFocusable(false); |
| 8942 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8943 | |
| 8944 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8945 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8946 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8947 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8948 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8949 | } |
| 8950 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8951 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8952 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8953 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8954 | std::shared_ptr<FakeApplicationHandle> application = |
| 8955 | std::make_shared<FakeApplicationHandle>(); |
| 8956 | std::string name = "Fake Spy "; |
| 8957 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8958 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8959 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8960 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8961 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8962 | return spy; |
| 8963 | } |
| 8964 | |
| 8965 | sp<FakeWindowHandle> createForeground() { |
| 8966 | std::shared_ptr<FakeApplicationHandle> application = |
| 8967 | std::make_shared<FakeApplicationHandle>(); |
| 8968 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8969 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 8970 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8971 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8972 | return window; |
| 8973 | } |
| 8974 | |
| 8975 | private: |
| 8976 | int mSpyCount{0}; |
| 8977 | }; |
| 8978 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8979 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8980 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8981 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 8982 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8983 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 8984 | ScopedSilentDeath _silentDeath; |
| 8985 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8986 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8987 | spy->setTrustedOverlay(false); |
| 8988 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 8989 | ".* not a trusted overlay"); |
| 8990 | } |
| 8991 | |
| 8992 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8993 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 8994 | */ |
| 8995 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8996 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 8998 | |
| 8999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9000 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9001 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9002 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9003 | } |
| 9004 | |
| 9005 | /** |
| 9006 | * Verify the order in which different input windows receive events. The touched foreground window |
| 9007 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 9008 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 9009 | * receive events before ones belows it. |
| 9010 | * |
| 9011 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 9012 | * spy1, spy2, window, spy3. |
| 9013 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 9014 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 9015 | * window. |
| 9016 | */ |
| 9017 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 9018 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9019 | auto spy1 = createSpy(); |
| 9020 | auto spy2 = createSpy(); |
| 9021 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9022 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 9023 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 9024 | const size_t numChannels = channels.size(); |
| 9025 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 9026 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9027 | if (!epollFd.ok()) { |
| 9028 | FAIL() << "Failed to create epoll fd"; |
| 9029 | } |
| 9030 | |
| 9031 | for (size_t i = 0; i < numChannels; i++) { |
| 9032 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 9033 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 9034 | FAIL() << "Failed to add fd to epoll"; |
| 9035 | } |
| 9036 | } |
| 9037 | |
| 9038 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9039 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9040 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9041 | |
| 9042 | std::vector<size_t> eventOrder; |
| 9043 | std::vector<struct epoll_event> events(numChannels); |
| 9044 | for (;;) { |
| 9045 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 9046 | (100ms).count()); |
| 9047 | if (nFds < 0) { |
| 9048 | FAIL() << "Failed to call epoll_wait"; |
| 9049 | } |
| 9050 | if (nFds == 0) { |
| 9051 | break; // epoll_wait timed out |
| 9052 | } |
| 9053 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 9054 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 9055 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9056 | channels[i]->consumeMotionDown(); |
| 9057 | } |
| 9058 | } |
| 9059 | |
| 9060 | // Verify the order in which the events were received. |
| 9061 | EXPECT_EQ(3u, eventOrder.size()); |
| 9062 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 9063 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 9064 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 9065 | } |
| 9066 | |
| 9067 | /** |
| 9068 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 9069 | */ |
| 9070 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 9071 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9072 | auto spy = createSpy(); |
| 9073 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9074 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9075 | |
| 9076 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9077 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9078 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9079 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9080 | spy->assertNoEvents(); |
| 9081 | } |
| 9082 | |
| 9083 | /** |
| 9084 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 9085 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 9086 | * to the window. |
| 9087 | */ |
| 9088 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 9089 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9090 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9091 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 9092 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9093 | |
| 9094 | // Inject an event outside the spy window's touchable region. |
| 9095 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9096 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9097 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9098 | window->consumeMotionDown(); |
| 9099 | spy->assertNoEvents(); |
| 9100 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9101 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9102 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9103 | window->consumeMotionUp(); |
| 9104 | spy->assertNoEvents(); |
| 9105 | |
| 9106 | // Inject an event inside the spy window's touchable region. |
| 9107 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9108 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9109 | {5, 10})) |
| 9110 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9111 | window->consumeMotionDown(); |
| 9112 | spy->consumeMotionDown(); |
| 9113 | } |
| 9114 | |
| 9115 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9116 | * 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] | 9117 | * 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] | 9118 | */ |
| 9119 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 9120 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9121 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9122 | auto spy = createSpy(); |
| 9123 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9124 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9125 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 9126 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9127 | |
| 9128 | // Inject an event outside the spy window's frame and touchable region. |
| 9129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9130 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9131 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9132 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9133 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9134 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9135 | } |
| 9136 | |
| 9137 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9138 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 9139 | * pointers that are down within its bounds. |
| 9140 | */ |
| 9141 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 9142 | auto windowLeft = createForeground(); |
| 9143 | windowLeft->setFrame({0, 0, 100, 200}); |
| 9144 | auto windowRight = createForeground(); |
| 9145 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9146 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9147 | spy->setFrame({0, 0, 200, 200}); |
| 9148 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 9149 | |
| 9150 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9151 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9152 | {50, 50})) |
| 9153 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9154 | windowLeft->consumeMotionDown(); |
| 9155 | spy->consumeMotionDown(); |
| 9156 | |
| 9157 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9158 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9159 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9160 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9161 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9162 | .build(); |
| 9163 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9164 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9165 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9166 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9167 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9168 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9169 | } |
| 9170 | |
| 9171 | /** |
| 9172 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 9173 | * the spy should receive the second pointer with ACTION_DOWN. |
| 9174 | */ |
| 9175 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 9176 | auto window = createForeground(); |
| 9177 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9178 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9179 | spyRight->setFrame({100, 0, 200, 200}); |
| 9180 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 9181 | |
| 9182 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9183 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9184 | {50, 50})) |
| 9185 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9186 | window->consumeMotionDown(); |
| 9187 | spyRight->assertNoEvents(); |
| 9188 | |
| 9189 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9190 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9191 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9192 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 9193 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9194 | .build(); |
| 9195 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9196 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9197 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9198 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9199 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 9200 | spyRight->consumeMotionDown(); |
| 9201 | } |
| 9202 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9203 | /** |
| 9204 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 9205 | * windows should be allowed to control split touch. |
| 9206 | */ |
| 9207 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9208 | // 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] | 9209 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9210 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 9211 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9212 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9213 | auto window = createForeground(); |
| 9214 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9215 | |
| 9216 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9217 | |
| 9218 | // First finger down, no window touched. |
| 9219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9220 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9221 | {100, 200})) |
| 9222 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9223 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9224 | window->assertNoEvents(); |
| 9225 | |
| 9226 | // Second finger down on window, the window should receive touch down. |
| 9227 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 9228 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9229 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9230 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9231 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9232 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9233 | .build(); |
| 9234 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9235 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9236 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9237 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9238 | |
| 9239 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9240 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9241 | } |
| 9242 | |
| 9243 | /** |
| 9244 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 9245 | * do not receive key events. |
| 9246 | */ |
| 9247 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9248 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9249 | spy->setFocusable(false); |
| 9250 | |
| 9251 | auto window = createForeground(); |
| 9252 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9253 | setFocusedWindow(window); |
| 9254 | window->consumeFocusEvent(true); |
| 9255 | |
| 9256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 9257 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9258 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9259 | |
| 9260 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 9261 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9262 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9263 | |
| 9264 | spy->assertNoEvents(); |
| 9265 | } |
| 9266 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9267 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 9268 | |
| 9269 | /** |
| 9270 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 9271 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 9272 | */ |
| 9273 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 9274 | auto window = createForeground(); |
| 9275 | auto spy1 = createSpy(); |
| 9276 | auto spy2 = createSpy(); |
| 9277 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 9278 | |
| 9279 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9280 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9281 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9282 | window->consumeMotionDown(); |
| 9283 | spy1->consumeMotionDown(); |
| 9284 | spy2->consumeMotionDown(); |
| 9285 | |
| 9286 | // Pilfer pointers from the second spy window. |
| 9287 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 9288 | spy2->assertNoEvents(); |
| 9289 | spy1->consumeMotionCancel(); |
| 9290 | window->consumeMotionCancel(); |
| 9291 | |
| 9292 | // The rest of the gesture should only be sent to the second spy window. |
| 9293 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9294 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 9295 | ADISPLAY_ID_DEFAULT)) |
| 9296 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9297 | spy2->consumeMotionMove(); |
| 9298 | spy1->assertNoEvents(); |
| 9299 | window->assertNoEvents(); |
| 9300 | } |
| 9301 | |
| 9302 | /** |
| 9303 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 9304 | * in the middle of the gesture. |
| 9305 | */ |
| 9306 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 9307 | auto window = createForeground(); |
| 9308 | auto spy = createSpy(); |
| 9309 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9310 | |
| 9311 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9312 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9313 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9314 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9315 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9316 | |
| 9317 | window->releaseChannel(); |
| 9318 | |
| 9319 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9320 | |
| 9321 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9322 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9323 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9324 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9325 | } |
| 9326 | |
| 9327 | /** |
| 9328 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 9329 | * the spy, but not to any other windows. |
| 9330 | */ |
| 9331 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 9332 | auto spy = createSpy(); |
| 9333 | auto window = createForeground(); |
| 9334 | |
| 9335 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9336 | |
| 9337 | // First finger down on the window and the spy. |
| 9338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9339 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9340 | {100, 200})) |
| 9341 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9342 | spy->consumeMotionDown(); |
| 9343 | window->consumeMotionDown(); |
| 9344 | |
| 9345 | // Spy window pilfers the pointers. |
| 9346 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9347 | window->consumeMotionCancel(); |
| 9348 | |
| 9349 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 9350 | const MotionEvent secondFingerDownEvent = |
| 9351 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9352 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9353 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9354 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9355 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9356 | .build(); |
| 9357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9358 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9359 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9360 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9361 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9362 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9363 | |
| 9364 | // Third finger goes down outside all windows, so injection should fail. |
| 9365 | const MotionEvent thirdFingerDownEvent = |
| 9366 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9367 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9368 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9369 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9370 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 9371 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9372 | .build(); |
| 9373 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 9374 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9375 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9376 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9377 | |
| 9378 | spy->assertNoEvents(); |
| 9379 | window->assertNoEvents(); |
| 9380 | } |
| 9381 | |
| 9382 | /** |
| 9383 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 9384 | */ |
| 9385 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 9386 | auto spy = createSpy(); |
| 9387 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9388 | auto window = createForeground(); |
| 9389 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9390 | |
| 9391 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9392 | |
| 9393 | // First finger down on the window only |
| 9394 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9395 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9396 | {150, 150})) |
| 9397 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9398 | window->consumeMotionDown(); |
| 9399 | |
| 9400 | // Second finger down on the spy and window |
| 9401 | const MotionEvent secondFingerDownEvent = |
| 9402 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9403 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9404 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9405 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9406 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9407 | .build(); |
| 9408 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9409 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9410 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9411 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9412 | spy->consumeMotionDown(); |
| 9413 | window->consumeMotionPointerDown(1); |
| 9414 | |
| 9415 | // Third finger down on the spy and window |
| 9416 | const MotionEvent thirdFingerDownEvent = |
| 9417 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9418 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9419 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9420 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9421 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 9422 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9423 | .build(); |
| 9424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9425 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9426 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9427 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9428 | spy->consumeMotionPointerDown(1); |
| 9429 | window->consumeMotionPointerDown(2); |
| 9430 | |
| 9431 | // Spy window pilfers the pointers. |
| 9432 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9433 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9434 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9435 | |
| 9436 | spy->assertNoEvents(); |
| 9437 | window->assertNoEvents(); |
| 9438 | } |
| 9439 | |
| 9440 | /** |
| 9441 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 9442 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 9443 | * window, then that window should receive ACTION_CANCEL. |
| 9444 | */ |
| 9445 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 9446 | auto spy = createSpy(); |
| 9447 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9448 | auto window = createForeground(); |
| 9449 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9450 | |
| 9451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9452 | |
| 9453 | // First finger down on both spy and window |
| 9454 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9455 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9456 | {10, 10})) |
| 9457 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9458 | window->consumeMotionDown(); |
| 9459 | spy->consumeMotionDown(); |
| 9460 | |
| 9461 | // Second finger down on the spy and window |
| 9462 | const MotionEvent secondFingerDownEvent = |
| 9463 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9464 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9465 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9466 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9467 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9468 | .build(); |
| 9469 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9470 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9471 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9472 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9473 | spy->consumeMotionPointerDown(1); |
| 9474 | window->consumeMotionPointerDown(1); |
| 9475 | |
| 9476 | // Spy window pilfers the pointers. |
| 9477 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9478 | window->consumeMotionCancel(); |
| 9479 | |
| 9480 | spy->assertNoEvents(); |
| 9481 | window->assertNoEvents(); |
| 9482 | } |
| 9483 | |
| 9484 | /** |
| 9485 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 9486 | * be sent to other windows |
| 9487 | */ |
| 9488 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 9489 | auto spy = createSpy(); |
| 9490 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9491 | auto window = createForeground(); |
| 9492 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9493 | |
| 9494 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9495 | |
| 9496 | // First finger down on both window and spy |
| 9497 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9498 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9499 | {10, 10})) |
| 9500 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9501 | window->consumeMotionDown(); |
| 9502 | spy->consumeMotionDown(); |
| 9503 | |
| 9504 | // Spy window pilfers the pointers. |
| 9505 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9506 | window->consumeMotionCancel(); |
| 9507 | |
| 9508 | // Second finger down on the window only |
| 9509 | const MotionEvent secondFingerDownEvent = |
| 9510 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9511 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9512 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9513 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9514 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9515 | .build(); |
| 9516 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9517 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9518 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9519 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9520 | window->consumeMotionDown(); |
| 9521 | window->assertNoEvents(); |
| 9522 | |
| 9523 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 9524 | spy->consumeMotionMove(); |
| 9525 | spy->assertNoEvents(); |
| 9526 | } |
| 9527 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9528 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 9529 | public: |
| 9530 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 9531 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9532 | std::make_shared<FakeApplicationHandle>(); |
| 9533 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9534 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 9535 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9536 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9537 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9538 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9539 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9540 | overlay->setTrustedOverlay(true); |
| 9541 | |
| 9542 | std::shared_ptr<FakeApplicationHandle> application = |
| 9543 | std::make_shared<FakeApplicationHandle>(); |
| 9544 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9545 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9546 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9547 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9548 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9549 | |
| 9550 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9551 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9552 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9553 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9554 | return {std::move(overlay), std::move(window)}; |
| 9555 | } |
| 9556 | |
| 9557 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9558 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9559 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9560 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9561 | } |
| 9562 | |
| 9563 | void sendStylusEvent(int32_t action) { |
| 9564 | NotifyMotionArgs motionArgs = |
| 9565 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9566 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9567 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9568 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9569 | } |
| 9570 | }; |
| 9571 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9572 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9573 | |
| 9574 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 9575 | ScopedSilentDeath _silentDeath; |
| 9576 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9577 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9578 | overlay->setTrustedOverlay(false); |
| 9579 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9580 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9581 | ".* not a trusted overlay"); |
| 9582 | } |
| 9583 | |
| 9584 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9585 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9586 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9587 | |
| 9588 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9589 | overlay->consumeMotionDown(); |
| 9590 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9591 | overlay->consumeMotionUp(); |
| 9592 | |
| 9593 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9594 | window->consumeMotionDown(); |
| 9595 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9596 | window->consumeMotionUp(); |
| 9597 | |
| 9598 | overlay->assertNoEvents(); |
| 9599 | window->assertNoEvents(); |
| 9600 | } |
| 9601 | |
| 9602 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9603 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9604 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9605 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9606 | |
| 9607 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9608 | overlay->consumeMotionDown(); |
| 9609 | window->consumeMotionDown(); |
| 9610 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9611 | overlay->consumeMotionUp(); |
| 9612 | window->consumeMotionUp(); |
| 9613 | |
| 9614 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9615 | window->consumeMotionDown(); |
| 9616 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9617 | window->consumeMotionUp(); |
| 9618 | |
| 9619 | overlay->assertNoEvents(); |
| 9620 | window->assertNoEvents(); |
| 9621 | } |
| 9622 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9623 | /** |
| 9624 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9625 | * The scenario is as follows: |
| 9626 | * - The stylus interceptor overlay is configured as a spy window. |
| 9627 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9628 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9629 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9630 | */ |
| 9631 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9632 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9633 | overlay->setSpy(true); |
| 9634 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9635 | |
| 9636 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9637 | overlay->consumeMotionDown(); |
| 9638 | window->consumeMotionDown(); |
| 9639 | |
| 9640 | // The interceptor pilfers the pointers. |
| 9641 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9642 | window->consumeMotionCancel(); |
| 9643 | |
| 9644 | // The interceptor configures itself so that it is no longer a spy. |
| 9645 | overlay->setSpy(false); |
| 9646 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9647 | |
| 9648 | // It continues to receive the rest of the stylus gesture. |
| 9649 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9650 | overlay->consumeMotionMove(); |
| 9651 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9652 | overlay->consumeMotionUp(); |
| 9653 | |
| 9654 | window->assertNoEvents(); |
| 9655 | } |
| 9656 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9657 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9658 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9659 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9660 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9661 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9662 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9663 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9664 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9665 | |
| 9666 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9667 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9668 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9669 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9670 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9671 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9672 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9673 | } |
| 9674 | |
| 9675 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9676 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9677 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9678 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9679 | mPolicyFlags); |
| 9680 | } |
| 9681 | |
| 9682 | sp<FakeWindowHandle> createWindow() const { |
| 9683 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9684 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9685 | sp<FakeWindowHandle> window = |
| 9686 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9687 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9688 | window->setOwnerInfo(mPid, mUid); |
| 9689 | return window; |
| 9690 | } |
| 9691 | }; |
| 9692 | |
| 9693 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9694 | |
| 9695 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9696 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9697 | auto window = owner.createWindow(); |
| 9698 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9699 | |
| 9700 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9701 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9702 | window->consumeMotionDown(); |
| 9703 | |
| 9704 | setFocusedWindow(window); |
| 9705 | window->consumeFocusEvent(true); |
| 9706 | |
| 9707 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9708 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9709 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9710 | } |
| 9711 | |
| 9712 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9713 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9714 | auto window = owner.createWindow(); |
| 9715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9716 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9717 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9718 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9719 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9720 | |
| 9721 | setFocusedWindow(window); |
| 9722 | window->consumeFocusEvent(true); |
| 9723 | |
| 9724 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9725 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9726 | window->assertNoEvents(); |
| 9727 | } |
| 9728 | |
| 9729 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9730 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9731 | auto window = owner.createWindow(); |
| 9732 | auto spy = owner.createWindow(); |
| 9733 | spy->setSpy(true); |
| 9734 | spy->setTrustedOverlay(true); |
| 9735 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9736 | |
| 9737 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9738 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9739 | spy->consumeMotionDown(); |
| 9740 | window->consumeMotionDown(); |
| 9741 | } |
| 9742 | |
| 9743 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9744 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9745 | auto window = owner.createWindow(); |
| 9746 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9747 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9748 | auto randosSpy = rando.createWindow(); |
| 9749 | randosSpy->setSpy(true); |
| 9750 | randosSpy->setTrustedOverlay(true); |
| 9751 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9752 | |
| 9753 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9754 | // not receive the event. |
| 9755 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9756 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9757 | randosSpy->assertNoEvents(); |
| 9758 | window->consumeMotionDown(); |
| 9759 | } |
| 9760 | |
| 9761 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9762 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9763 | auto window = owner.createWindow(); |
| 9764 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9765 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9766 | auto randosSpy = rando.createWindow(); |
| 9767 | randosSpy->setSpy(true); |
| 9768 | randosSpy->setTrustedOverlay(true); |
| 9769 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9770 | |
| 9771 | // A user that has injection permission can inject into any window. |
| 9772 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9773 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9774 | ADISPLAY_ID_DEFAULT)); |
| 9775 | randosSpy->consumeMotionDown(); |
| 9776 | window->consumeMotionDown(); |
| 9777 | |
| 9778 | setFocusedWindow(randosSpy); |
| 9779 | randosSpy->consumeFocusEvent(true); |
| 9780 | |
| 9781 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9782 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9783 | window->assertNoEvents(); |
| 9784 | } |
| 9785 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9786 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9787 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9788 | auto window = owner.createWindow(); |
| 9789 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame^] | 9790 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9791 | auto randosWindow = rando.createWindow(); |
| 9792 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9793 | randosWindow->setWatchOutsideTouch(true); |
| 9794 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9795 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9796 | // 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] | 9797 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9798 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9799 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 9800 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9801 | } |
| 9802 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9803 | } // namespace android::inputdispatcher |