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 | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 18 | #include "EventBuilders.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 19 | |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 20 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 21 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 24 | #include <binder/Binder.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 26 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 27 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 28 | #include <input/Input.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 29 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 30 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 31 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 32 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 33 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 34 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 35 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 36 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 38 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 39 | using android::gui::FocusRequest; |
| 40 | using android::gui::TouchOcclusionMode; |
| 41 | using android::gui::WindowInfo; |
| 42 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 43 | using android::os::InputEventInjectionResult; |
| 44 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 45 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 46 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 47 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 48 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 49 | using testing::AllOf; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 50 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 51 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 52 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 53 | |
| 54 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 55 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 56 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 57 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 58 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 59 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 60 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 61 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 62 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 63 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 64 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 65 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 66 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 67 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 68 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 69 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 70 | /** |
| 71 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 72 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 73 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 74 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 75 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 76 | * pointer id=0 leaves but the pointer id=1 remains. |
| 77 | */ |
| 78 | static constexpr int32_t POINTER_0_DOWN = |
| 79 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 80 | static constexpr int32_t POINTER_1_DOWN = |
| 81 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 82 | static constexpr int32_t POINTER_2_DOWN = |
| 83 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 84 | static constexpr int32_t POINTER_3_DOWN = |
| 85 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 86 | static constexpr int32_t POINTER_0_UP = |
| 87 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 88 | static constexpr int32_t POINTER_1_UP = |
| 89 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 90 | static constexpr int32_t POINTER_2_UP = |
| 91 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 92 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 93 | // The default pid and uid for windows created on the primary display by the test. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 94 | static constexpr int32_t WINDOW_PID = 999; |
| 95 | static constexpr int32_t WINDOW_UID = 1001; |
| 96 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 97 | // The default pid and uid for the windows created on the secondary display by the test. |
| 98 | static constexpr int32_t SECONDARY_WINDOW_PID = 1010; |
| 99 | static constexpr int32_t SECONDARY_WINDOW_UID = 1012; |
| 100 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 101 | // An arbitrary pid of the gesture monitor window |
| 102 | static constexpr int32_t MONITOR_PID = 2001; |
| 103 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 104 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 105 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 106 | static constexpr int expectedWallpaperFlags = |
| 107 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 108 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 109 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 110 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 111 | struct PointF { |
| 112 | float x; |
| 113 | float y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 114 | auto operator<=>(const PointF&) const = default; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 115 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 116 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 117 | /** |
| 118 | * Return a DOWN key event with KEYCODE_A. |
| 119 | */ |
| 120 | static KeyEvent getTestKeyEvent() { |
| 121 | KeyEvent event; |
| 122 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 123 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 124 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 125 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 126 | return event; |
| 127 | } |
| 128 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 129 | static void assertMotionAction(int32_t expectedAction, int32_t receivedAction) { |
| 130 | ASSERT_EQ(expectedAction, receivedAction) |
| 131 | << "expected " << MotionEvent::actionToString(expectedAction) << ", got " |
| 132 | << MotionEvent::actionToString(receivedAction); |
| 133 | } |
| 134 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 135 | MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") { |
| 136 | bool matches = action == arg.getAction(); |
| 137 | if (!matches) { |
| 138 | *result_listener << "expected action " << MotionEvent::actionToString(action) |
| 139 | << ", but got " << MotionEvent::actionToString(arg.getAction()); |
| 140 | } |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 141 | if (action == AMOTION_EVENT_ACTION_DOWN) { |
| 142 | if (!matches) { |
| 143 | *result_listener << "; "; |
| 144 | } |
| 145 | *result_listener << "downTime should match eventTime for ACTION_DOWN events"; |
| 146 | matches &= arg.getDownTime() == arg.getEventTime(); |
| 147 | } |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 148 | if (action == AMOTION_EVENT_ACTION_CANCEL) { |
| 149 | if (!matches) { |
| 150 | *result_listener << "; "; |
| 151 | } |
| 152 | *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set"; |
| 153 | matches &= (arg.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0; |
| 154 | } |
| 155 | return matches; |
| 156 | } |
| 157 | |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 158 | MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") { |
| 159 | return arg.getDownTime() == downTime; |
| 160 | } |
| 161 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 162 | MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") { |
| 163 | return arg.getDisplayId() == displayId; |
| 164 | } |
| 165 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 166 | MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") { |
| 167 | return arg.getDeviceId() == deviceId; |
| 168 | } |
| 169 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 170 | MATCHER_P(WithSource, source, "InputEvent with specified source") { |
| 171 | *result_listener << "expected source " << inputEventSourceToString(source) << ", but got " |
| 172 | << inputEventSourceToString(arg.getSource()); |
| 173 | return arg.getSource() == source; |
| 174 | } |
| 175 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 176 | MATCHER_P(WithFlags, flags, "InputEvent with specified flags") { |
| 177 | return arg.getFlags() == flags; |
| 178 | } |
| 179 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 180 | MATCHER_P2(WithCoords, x, y, "MotionEvent with specified coordinates") { |
| 181 | if (arg.getPointerCount() != 1) { |
| 182 | *result_listener << "Expected 1 pointer, got " << arg.getPointerCount(); |
| 183 | return false; |
| 184 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 185 | return arg.getX(/*pointerIndex=*/0) == x && arg.getY(/*pointerIndex=*/0) == y; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 188 | MATCHER_P(WithPointerCount, pointerCount, "MotionEvent with specified number of pointers") { |
| 189 | return arg.getPointerCount() == pointerCount; |
| 190 | } |
| 191 | |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 192 | MATCHER_P(WithPointers, pointers, "MotionEvent with specified pointers") { |
| 193 | // Build a map for the received pointers, by pointer id |
| 194 | std::map<int32_t /*pointerId*/, PointF> actualPointers; |
| 195 | for (size_t pointerIndex = 0; pointerIndex < arg.getPointerCount(); pointerIndex++) { |
| 196 | const int32_t pointerId = arg.getPointerId(pointerIndex); |
| 197 | actualPointers[pointerId] = {arg.getX(pointerIndex), arg.getY(pointerIndex)}; |
| 198 | } |
| 199 | return pointers == actualPointers; |
| 200 | } |
| 201 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 | // --- FakeInputDispatcherPolicy --- |
| 203 | |
| 204 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 205 | InputDispatcherConfiguration mConfig; |
| 206 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 207 | using AnrResult = std::pair<sp<IBinder>, int32_t /*pid*/>; |
| 208 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 210 | FakeInputDispatcherPolicy() = default; |
| 211 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 212 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 213 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 214 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 215 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 216 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 217 | |
| 218 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 219 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 220 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 221 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 224 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 225 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 226 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 227 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 228 | |
| 229 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 230 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 231 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 232 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 233 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 234 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 235 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 236 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 239 | void assertFilterInputEventWasNotCalled() { |
| 240 | std::scoped_lock lock(mLock); |
| 241 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 242 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 243 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 244 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 245 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 246 | ASSERT_TRUE(mConfigurationChangedTime) |
| 247 | << "Timed out waiting for configuration changed call"; |
| 248 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 249 | mConfigurationChangedTime = std::nullopt; |
| 250 | } |
| 251 | |
| 252 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 253 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 254 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 255 | // 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] | 256 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 257 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 258 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 259 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 260 | mLastNotifySwitch = std::nullopt; |
| 261 | } |
| 262 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 263 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 264 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 265 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 266 | mOnPointerDownToken.clear(); |
| 267 | } |
| 268 | |
| 269 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 270 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 271 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 272 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 275 | // This function must be called soon after the expected ANR timer starts, |
| 276 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 277 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 278 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 279 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 280 | std::unique_lock lock(mLock); |
| 281 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 282 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 283 | ASSERT_NO_FATAL_FAILURE( |
| 284 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 285 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 288 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 289 | const sp<WindowInfoHandle>& window) { |
| 290 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 291 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 292 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 293 | } |
| 294 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 295 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 296 | const sp<IBinder>& expectedToken, |
| 297 | int32_t expectedPid) { |
| 298 | std::unique_lock lock(mLock); |
| 299 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 300 | AnrResult result; |
| 301 | ASSERT_NO_FATAL_FAILURE(result = |
| 302 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
| 303 | const auto& [token, pid] = result; |
| 304 | ASSERT_EQ(expectedToken, token); |
| 305 | ASSERT_EQ(expectedPid, pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 306 | } |
| 307 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 308 | /** 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] | 309 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 310 | std::unique_lock lock(mLock); |
| 311 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 312 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 313 | const auto& [token, _] = result; |
| 314 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 317 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
| 318 | int32_t expectedPid) { |
| 319 | std::unique_lock lock(mLock); |
| 320 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 321 | AnrResult result; |
| 322 | ASSERT_NO_FATAL_FAILURE( |
| 323 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
| 324 | const auto& [token, pid] = result; |
| 325 | ASSERT_EQ(expectedToken, token); |
| 326 | ASSERT_EQ(expectedPid, pid); |
| 327 | } |
| 328 | |
| 329 | /** 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] | 330 | sp<IBinder> getResponsiveWindowToken() { |
| 331 | std::unique_lock lock(mLock); |
| 332 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 333 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 334 | const auto& [token, _] = result; |
| 335 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void assertNotifyAnrWasNotCalled() { |
| 339 | std::scoped_lock lock(mLock); |
| 340 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 341 | ASSERT_TRUE(mAnrWindows.empty()); |
| 342 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 343 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 344 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 347 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 348 | mConfig.keyRepeatTimeout = timeout; |
| 349 | mConfig.keyRepeatDelay = delay; |
| 350 | } |
| 351 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 352 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 353 | std::unique_lock lock(mLock); |
| 354 | base::ScopedLockAssertion assumeLocked(mLock); |
| 355 | |
| 356 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 357 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 358 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 359 | enabled; |
| 360 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 361 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 362 | << ") to be called."; |
| 363 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 364 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 365 | auto request = *mPointerCaptureRequest; |
| 366 | mPointerCaptureRequest.reset(); |
| 367 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void assertSetPointerCaptureNotCalled() { |
| 371 | std::unique_lock lock(mLock); |
| 372 | base::ScopedLockAssertion assumeLocked(mLock); |
| 373 | |
| 374 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 375 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 376 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 377 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 378 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 379 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 380 | } |
| 381 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 382 | void assertDropTargetEquals(const sp<IBinder>& targetToken) { |
| 383 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 384 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 385 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 386 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 387 | } |
| 388 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 389 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 390 | std::unique_lock lock(mLock); |
| 391 | base::ScopedLockAssertion assumeLocked(mLock); |
| 392 | std::optional<sp<IBinder>> receivedToken = |
| 393 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 394 | mNotifyInputChannelBroken); |
| 395 | ASSERT_TRUE(receivedToken.has_value()); |
| 396 | ASSERT_EQ(token, *receivedToken); |
| 397 | } |
| 398 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 399 | /** |
| 400 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 401 | */ |
| 402 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 403 | mInterceptKeyTimeout = timeout; |
| 404 | } |
| 405 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 406 | void assertUserActivityPoked() { |
| 407 | std::scoped_lock lock(mLock); |
| 408 | ASSERT_TRUE(mPokedUserActivity) << "Expected user activity to have been poked"; |
| 409 | } |
| 410 | |
| 411 | void assertUserActivityNotPoked() { |
| 412 | std::scoped_lock lock(mLock); |
| 413 | ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; |
| 414 | } |
| 415 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 416 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 417 | std::mutex mLock; |
| 418 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 419 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 420 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 421 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 422 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 423 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 424 | |
| 425 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 426 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 427 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 428 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 429 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 430 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 431 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 432 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 433 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 434 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 435 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 436 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 437 | bool mPokedUserActivity GUARDED_BY(mLock) = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 438 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 439 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 440 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 441 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 442 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 443 | // first entry from the container and erase it. |
| 444 | template <class T> |
| 445 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 446 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 447 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 448 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 449 | // before checking if ANR was called. |
| 450 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 451 | // to provide it some time to act. 100ms seems reasonable. |
| 452 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 453 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 454 | std::optional<T> token = |
| 455 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 456 | if (!token.has_value()) { |
| 457 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 458 | return {}; |
| 459 | } |
| 460 | |
| 461 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 462 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 463 | // the dispatcher started counting before this function was called |
| 464 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 465 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 466 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 467 | << "ms, but waited " |
| 468 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 469 | << "ms instead"; |
| 470 | } |
| 471 | return *token; |
| 472 | } |
| 473 | |
| 474 | template <class T> |
| 475 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 476 | std::queue<T>& storage, |
| 477 | std::unique_lock<std::mutex>& lock, |
| 478 | std::condition_variable& condition) |
| 479 | REQUIRES(mLock) { |
| 480 | condition.wait_for(lock, timeout, |
| 481 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 482 | if (storage.empty()) { |
| 483 | ADD_FAILURE() << "Did not receive the expected callback"; |
| 484 | return std::nullopt; |
| 485 | } |
| 486 | T item = storage.front(); |
| 487 | storage.pop(); |
| 488 | return std::make_optional(item); |
| 489 | } |
| 490 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 491 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 492 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 493 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 496 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid, |
| 497 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 498 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 499 | ASSERT_TRUE(pid.has_value()); |
| 500 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 501 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 502 | } |
| 503 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 504 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
| 505 | std::optional<int32_t> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [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 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 509 | mNotifyAnr.notify_all(); |
| 510 | } |
| 511 | |
| 512 | void notifyNoFocusedWindowAnr( |
| 513 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 514 | std::scoped_lock lock(mLock); |
| 515 | mAnrApplications.push(applicationHandle); |
| 516 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 519 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 520 | std::scoped_lock lock(mLock); |
| 521 | mBrokenInputChannels.push(connectionToken); |
| 522 | mNotifyInputChannelBroken.notify_all(); |
| 523 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 524 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 525 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 526 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 527 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 528 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 529 | const std::vector<float>& values) override {} |
| 530 | |
| 531 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 532 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 533 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 534 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 535 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 536 | InputDispatcherConfiguration getDispatcherConfiguration() override { return mConfig; } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 537 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 538 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 539 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 540 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 541 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 542 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 543 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 544 | break; |
| 545 | } |
| 546 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 547 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 548 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 549 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 550 | break; |
| 551 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 552 | default: { |
| 553 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 554 | break; |
| 555 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 556 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 560 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 561 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 562 | // Clear intercept state when we handled the event. |
| 563 | mInterceptKeyTimeout = 0ms; |
| 564 | } |
| 565 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 566 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 567 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 568 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 569 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 570 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 571 | // Clear intercept state so we could dispatch the event in next wake. |
| 572 | mInterceptKeyTimeout = 0ms; |
| 573 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 576 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent&, |
| 577 | uint32_t) override { |
| 578 | return {}; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 581 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 582 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 583 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 584 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 585 | * essentially a passthrough for notifySwitch. |
| 586 | */ |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 587 | mLastNotifySwitch = NotifySwitchArgs(/*id=*/1, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 590 | void pokeUserActivity(nsecs_t, int32_t, int32_t) override { |
| 591 | std::scoped_lock lock(mLock); |
| 592 | mPokedUserActivity = true; |
| 593 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 594 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 595 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 596 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 597 | mOnPointerDownToken = newToken; |
| 598 | } |
| 599 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 600 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 601 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 602 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 603 | mPointerCaptureChangedCondition.notify_all(); |
| 604 | } |
| 605 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 606 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 607 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 608 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 609 | mDropTargetWindowToken = token; |
| 610 | } |
| 611 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 612 | void assertFilterInputEventWasCalledInternal( |
| 613 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 614 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 615 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 616 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 617 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 618 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 619 | }; |
| 620 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 621 | // --- InputDispatcherTest --- |
| 622 | |
| 623 | class InputDispatcherTest : public testing::Test { |
| 624 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 625 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 626 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 628 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 629 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
| 630 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, STALE_EVENT_TIMEOUT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 631 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 632 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 633 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 636 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 637 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 638 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 639 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 640 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 641 | |
| 642 | /** |
| 643 | * Used for debugging when writing the test |
| 644 | */ |
| 645 | void dumpDispatcherState() { |
| 646 | std::string dump; |
| 647 | mDispatcher->dump(dump); |
| 648 | std::stringstream ss(dump); |
| 649 | std::string to; |
| 650 | |
| 651 | while (std::getline(ss, to, '\n')) { |
| 652 | ALOGE("%s", to.c_str()); |
| 653 | } |
| 654 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 655 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 656 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 657 | FocusRequest request; |
| 658 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 659 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 660 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 661 | request.displayId = window->getInfo()->displayId; |
| 662 | mDispatcher->setFocusedWindow(request); |
| 663 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 664 | }; |
| 665 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 666 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 667 | KeyEvent event; |
| 668 | |
| 669 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 670 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 671 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 672 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 673 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 674 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 675 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 676 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 677 | << "Should reject key events with undefined action."; |
| 678 | |
| 679 | // 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] | 680 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 681 | 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] | 682 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 683 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 684 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 685 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 686 | << "Should reject key events with ACTION_MULTIPLE."; |
| 687 | } |
| 688 | |
| 689 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 690 | MotionEvent event; |
| 691 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 692 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 693 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 694 | pointerProperties[i].clear(); |
| 695 | pointerProperties[i].id = i; |
| 696 | pointerCoords[i].clear(); |
| 697 | } |
| 698 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 699 | // Some constants commonly used below |
| 700 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 701 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 702 | constexpr int32_t metaState = AMETA_NONE; |
| 703 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 704 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 705 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 706 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 707 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 708 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 709 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 710 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 711 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 712 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 713 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 714 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 715 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | << "Should reject motion events with undefined action."; |
| 717 | |
| 718 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 719 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 720 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 721 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 722 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 723 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 724 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 725 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 726 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 727 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 728 | << "Should reject motion events with pointer down index too large."; |
| 729 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 730 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 731 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 732 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 733 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 734 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 735 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 736 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 737 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 738 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 739 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 740 | << "Should reject motion events with pointer down index too small."; |
| 741 | |
| 742 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 743 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 744 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 745 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 746 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 747 | ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 748 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 749 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 750 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 751 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 752 | << "Should reject motion events with pointer up index too large."; |
| 753 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 754 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 755 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 756 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 757 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 758 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 759 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 760 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 761 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 762 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 763 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 764 | << "Should reject motion events with pointer up index too small."; |
| 765 | |
| 766 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 767 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 768 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 769 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 770 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 771 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 772 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 773 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 774 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 775 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 776 | << "Should reject motion events with 0 pointers."; |
| 777 | |
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*/ MAX_POINTERS + 1, 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 more than MAX_POINTERS pointers."; |
| 788 | |
| 789 | // Rejects motion events with invalid pointer ids. |
| 790 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 791 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 792 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 793 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 794 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 795 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 796 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 797 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 798 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 799 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 800 | << "Should reject motion events with pointer ids less than 0."; |
| 801 | |
| 802 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 803 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 804 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 805 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 806 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 807 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 808 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 809 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 810 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 811 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 812 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 813 | |
| 814 | // Rejects motion events with duplicate pointer ids. |
| 815 | pointerProperties[0].id = 1; |
| 816 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 817 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 818 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 819 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 820 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 821 | ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 822 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 823 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 824 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 825 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 826 | << "Should reject motion events with duplicate pointer ids."; |
| 827 | } |
| 828 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 829 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 830 | |
| 831 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 832 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 833 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 834 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 835 | |
| 836 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 837 | } |
| 838 | |
| 839 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 840 | NotifySwitchArgs args(/*id=*/10, /*eventTime=*/20, /*policyFlags=*/0, /*switchValues=*/1, |
| 841 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 842 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 843 | |
| 844 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 845 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 846 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 847 | } |
| 848 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 849 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 850 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 851 | // Default input dispatching timeout if there is no focused application or paused window |
| 852 | // from which to determine an appropriate dispatching timeout. |
| 853 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 854 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 855 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 856 | |
| 857 | class FakeApplicationHandle : public InputApplicationHandle { |
| 858 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 859 | FakeApplicationHandle() { |
| 860 | mInfo.name = "Fake Application"; |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 861 | mInfo.token = sp<BBinder>::make(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 862 | mInfo.dispatchingTimeoutMillis = |
| 863 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 864 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 865 | virtual ~FakeApplicationHandle() {} |
| 866 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 867 | virtual bool updateInfo() override { return true; } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 868 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 869 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 870 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 871 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 872 | }; |
| 873 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 874 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 875 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 876 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 877 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 878 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 881 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 882 | InputEvent* event; |
| 883 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 884 | if (!consumeSeq) { |
| 885 | return nullptr; |
| 886 | } |
| 887 | finishEvent(*consumeSeq); |
| 888 | return event; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * Receive an event without acknowledging it. |
| 893 | * Return the sequence number that could later be used to send finished signal. |
| 894 | */ |
| 895 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 896 | uint32_t consumeSeq; |
| 897 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 898 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 899 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 900 | status_t status = WOULD_BLOCK; |
| 901 | while (status == WOULD_BLOCK) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 902 | status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 903 | &event); |
| 904 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 905 | if (elapsed > 100ms) { |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if (status == WOULD_BLOCK) { |
| 911 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 912 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | if (status != OK) { |
| 916 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 917 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 918 | } |
| 919 | if (event == nullptr) { |
| 920 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 921 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 922 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 923 | if (outEvent != nullptr) { |
| 924 | *outEvent = event; |
| 925 | } |
| 926 | return consumeSeq; |
| 927 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 928 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 929 | /** |
| 930 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 931 | */ |
| 932 | void finishEvent(uint32_t consumeSeq) { |
| 933 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 934 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 935 | } |
| 936 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 937 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 938 | const status_t status = mConsumer->sendTimeline(inputEventId, timeline); |
| 939 | ASSERT_EQ(OK, status); |
| 940 | } |
| 941 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 942 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 943 | std::optional<int32_t> expectedDisplayId, |
| 944 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 945 | InputEvent* event = consume(); |
| 946 | |
| 947 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 948 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 949 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 950 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 951 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 952 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 953 | if (expectedDisplayId.has_value()) { |
| 954 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 955 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 956 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 957 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 958 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 959 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 960 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 961 | if (expectedFlags.has_value()) { |
| 962 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 963 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 964 | break; |
| 965 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 966 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 967 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 968 | assertMotionAction(expectedAction, motionEvent.getAction()); |
| 969 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 970 | if (expectedFlags.has_value()) { |
| 971 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 972 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 973 | break; |
| 974 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 975 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 976 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 977 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 978 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 979 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 980 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 981 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 982 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 983 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 984 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 985 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 986 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 987 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 988 | } |
| 989 | |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 990 | MotionEvent* consumeMotion() { |
| 991 | InputEvent* event = consume(); |
| 992 | |
| 993 | if (event == nullptr) { |
| 994 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 995 | return nullptr; |
| 996 | } |
| 997 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 998 | if (event->getType() != InputEventType::MOTION) { |
| 999 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1000 | return nullptr; |
| 1001 | } |
| 1002 | return static_cast<MotionEvent*>(event); |
| 1003 | } |
| 1004 | |
| 1005 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1006 | MotionEvent* motionEvent = consumeMotion(); |
| 1007 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1008 | ASSERT_THAT(*motionEvent, matcher); |
| 1009 | } |
| 1010 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1011 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 1012 | InputEvent* event = consume(); |
| 1013 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1014 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1015 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1016 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1017 | |
| 1018 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1019 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1020 | |
| 1021 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 1022 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1023 | } |
| 1024 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1025 | void consumeCaptureEvent(bool hasCapture) { |
| 1026 | const InputEvent* event = consume(); |
| 1027 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1028 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1029 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1030 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1031 | |
| 1032 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1033 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1034 | |
| 1035 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1036 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1037 | } |
| 1038 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1039 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1040 | const InputEvent* event = consume(); |
| 1041 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1042 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1043 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1044 | |
| 1045 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1046 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1047 | |
| 1048 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1049 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1050 | EXPECT_EQ(x, dragEvent.getX()); |
| 1051 | EXPECT_EQ(y, dragEvent.getY()); |
| 1052 | } |
| 1053 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1054 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1055 | const InputEvent* event = consume(); |
| 1056 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1057 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1058 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1059 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1060 | |
| 1061 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1062 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1063 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1064 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1065 | } |
| 1066 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1067 | void assertNoEvents() { |
| 1068 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1069 | if (event == nullptr) { |
| 1070 | return; |
| 1071 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1072 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1073 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 1074 | ADD_FAILURE() << "Received key event " |
| 1075 | << KeyEvent::actionToString(keyEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1076 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1077 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1078 | ADD_FAILURE() << "Received motion event " |
| 1079 | << MotionEvent::actionToString(motionEvent.getAction()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1080 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1081 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1082 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1083 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1084 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1085 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1086 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1087 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1088 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1089 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1090 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1091 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1092 | } |
| 1093 | FAIL() << mName.c_str() |
| 1094 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 1098 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1099 | int getChannelFd() { return mConsumer->getChannel()->getFd().get(); } |
| 1100 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1101 | protected: |
| 1102 | std::unique_ptr<InputConsumer> mConsumer; |
| 1103 | PreallocatedInputEventFactory mEventFactory; |
| 1104 | |
| 1105 | std::string mName; |
| 1106 | }; |
| 1107 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1108 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1109 | public: |
| 1110 | static const int32_t WIDTH = 600; |
| 1111 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1112 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1113 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1114 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1115 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1116 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1117 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1118 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1119 | dispatcher->createInputChannel(name); |
| 1120 | token = (*channel)->getConnectionToken(); |
| 1121 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | inputApplicationHandle->updateInfo(); |
| 1125 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1126 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1127 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1128 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1129 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1130 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1131 | mInfo.alpha = 1.0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1132 | mInfo.frameLeft = 0; |
| 1133 | mInfo.frameTop = 0; |
| 1134 | mInfo.frameRight = WIDTH; |
| 1135 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1136 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1137 | mInfo.globalScaleFactor = 1.0; |
| 1138 | mInfo.touchableRegion.clear(); |
| 1139 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1140 | mInfo.ownerPid = WINDOW_PID; |
| 1141 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1142 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1143 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1146 | sp<FakeWindowHandle> clone( |
| 1147 | const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1148 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1149 | sp<FakeWindowHandle> handle = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1150 | sp<FakeWindowHandle>::make(inputApplicationHandle, dispatcher, |
| 1151 | mInfo.name + "(Mirror)", displayId, mInfo.token); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 1152 | return handle; |
| 1153 | } |
| 1154 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1155 | void setTouchable(bool touchable) { |
| 1156 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1157 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1158 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1159 | void setFocusable(bool focusable) { |
| 1160 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1161 | } |
| 1162 | |
| 1163 | void setVisible(bool visible) { |
| 1164 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1165 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1166 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1167 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1168 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1171 | void setPaused(bool paused) { |
| 1172 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1173 | } |
| 1174 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1175 | void setPreventSplitting(bool preventSplitting) { |
| 1176 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | void setSlippery(bool slippery) { |
| 1180 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1181 | } |
| 1182 | |
| 1183 | void setWatchOutsideTouch(bool watchOutside) { |
| 1184 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1185 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1186 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1187 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1188 | |
| 1189 | void setInterceptsStylus(bool interceptsStylus) { |
| 1190 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1191 | } |
| 1192 | |
| 1193 | void setDropInput(bool dropInput) { |
| 1194 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1195 | } |
| 1196 | |
| 1197 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1198 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1199 | } |
| 1200 | |
| 1201 | void setNoInputChannel(bool noInputChannel) { |
| 1202 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1203 | } |
| 1204 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1205 | void setDisableUserActivity(bool disableUserActivity) { |
| 1206 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1207 | } |
| 1208 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1209 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1210 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1211 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1212 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1213 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1214 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1215 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1216 | mInfo.frameLeft = frame.left; |
| 1217 | mInfo.frameTop = frame.top; |
| 1218 | mInfo.frameRight = frame.right; |
| 1219 | mInfo.frameBottom = frame.bottom; |
| 1220 | mInfo.touchableRegion.clear(); |
| 1221 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1222 | |
| 1223 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1224 | ui::Transform translate; |
| 1225 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1226 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1229 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1230 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1231 | void setIsWallpaper(bool isWallpaper) { |
| 1232 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1233 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1234 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1235 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1236 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1237 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1238 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1239 | void setTrustedOverlay(bool trustedOverlay) { |
| 1240 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1241 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1242 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1243 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1244 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1245 | } |
| 1246 | |
| 1247 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1248 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1249 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1250 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1251 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1252 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1255 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1256 | consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1259 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1260 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1261 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1262 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1266 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1267 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1268 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1272 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1273 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1274 | } |
| 1275 | |
| 1276 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1277 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1278 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1279 | expectedFlags); |
| 1280 | } |
| 1281 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1282 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1283 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1284 | int32_t expectedFlags = 0) { |
| 1285 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1286 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1287 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1291 | int32_t expectedFlags = 0) { |
| 1292 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
| 1293 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1294 | consumeEvent(InputEventType::MOTION, action, expectedDisplayId, expectedFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1298 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1299 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1300 | expectedFlags); |
| 1301 | } |
| 1302 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1303 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1304 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1305 | consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, expectedDisplayId, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1306 | expectedFlags); |
| 1307 | } |
| 1308 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1309 | void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1310 | int32_t expectedFlags = 0) { |
| 1311 | InputEvent* event = consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 1312 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1313 | ASSERT_EQ(InputEventType::MOTION, event->getType()); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1314 | const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 1315 | EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked()); |
| 1316 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getX()); |
| 1317 | EXPECT_EQ(0.f, motionEvent.getRawPointerCoords(0)->getY()); |
| 1318 | } |
| 1319 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1320 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1321 | ASSERT_NE(mInputReceiver, nullptr) |
| 1322 | << "Cannot consume events from a window with no receiver"; |
| 1323 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1324 | } |
| 1325 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1326 | void consumeCaptureEvent(bool hasCapture) { |
| 1327 | ASSERT_NE(mInputReceiver, nullptr) |
| 1328 | << "Cannot consume events from a window with no receiver"; |
| 1329 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1330 | } |
| 1331 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 1332 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
| 1333 | MotionEvent* motionEvent = consumeMotion(); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 1334 | 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] | 1335 | ASSERT_THAT(*motionEvent, matcher); |
| 1336 | } |
| 1337 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1338 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1339 | std::optional<int32_t> expectedDisplayId, |
| 1340 | std::optional<int32_t> expectedFlags) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1341 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 1342 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 1343 | expectedFlags); |
| 1344 | } |
| 1345 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1346 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1347 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1348 | } |
| 1349 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1350 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1351 | ASSERT_NE(mInputReceiver, nullptr) |
| 1352 | << "Cannot consume events from a window with no receiver"; |
| 1353 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1354 | } |
| 1355 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1356 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1357 | if (mInputReceiver == nullptr) { |
| 1358 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1359 | return std::nullopt; |
| 1360 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1361 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | void finishEvent(uint32_t sequenceNum) { |
| 1365 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1366 | mInputReceiver->finishEvent(sequenceNum); |
| 1367 | } |
| 1368 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1369 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1370 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1371 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1372 | } |
| 1373 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1374 | InputEvent* consume() { |
| 1375 | if (mInputReceiver == nullptr) { |
| 1376 | return nullptr; |
| 1377 | } |
| 1378 | return mInputReceiver->consume(); |
| 1379 | } |
| 1380 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1381 | MotionEvent* consumeMotion() { |
| 1382 | InputEvent* event = consume(); |
| 1383 | if (event == nullptr) { |
| 1384 | ADD_FAILURE() << "Consume failed : no event"; |
| 1385 | return nullptr; |
| 1386 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1387 | if (event->getType() != InputEventType::MOTION) { |
| 1388 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 1389 | return nullptr; |
| 1390 | } |
| 1391 | return static_cast<MotionEvent*>(event); |
| 1392 | } |
| 1393 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1394 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1395 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1396 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1397 | return; // Can't receive events if the window does not have input channel |
| 1398 | } |
| 1399 | ASSERT_NE(nullptr, mInputReceiver) |
| 1400 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1401 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1402 | } |
| 1403 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1404 | sp<IBinder> getToken() { return mInfo.token; } |
| 1405 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1406 | const std::string& getName() { return mName; } |
| 1407 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1408 | void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { |
| 1409 | mInfo.ownerPid = ownerPid; |
| 1410 | mInfo.ownerUid = ownerUid; |
| 1411 | } |
| 1412 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1413 | int32_t getPid() const { return mInfo.ownerPid; } |
| 1414 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1415 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1416 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1417 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1418 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1419 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1420 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1421 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1422 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1423 | }; |
| 1424 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1425 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1426 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1427 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1428 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1429 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1430 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1431 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1432 | bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, |
| 1433 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1434 | KeyEvent event; |
| 1435 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1436 | |
| 1437 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1438 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1439 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1440 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1441 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1442 | if (!allowKeyRepeat) { |
| 1443 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1444 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1445 | // Inject event until dispatch out. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1446 | return dispatcher->injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1447 | } |
| 1448 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1449 | static InputEventInjectionResult injectKeyDown(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1450 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1451 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1454 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1455 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1456 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1457 | static InputEventInjectionResult injectKeyDownNoRepeat( |
| 1458 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1459 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1460 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1461 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1464 | static InputEventInjectionResult injectKeyUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1465 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1466 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1469 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1470 | const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1471 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1472 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 1473 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
| 1474 | return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1475 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1478 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1479 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t action, int32_t source, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1480 | int32_t displayId, const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1481 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1482 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1483 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1484 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1485 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
| 1486 | std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1487 | MotionEventBuilder motionBuilder = |
| 1488 | MotionEventBuilder(action, source) |
| 1489 | .displayId(displayId) |
| 1490 | .eventTime(eventTime) |
| 1491 | .rawXCursorPosition(cursorPosition.x) |
| 1492 | .rawYCursorPosition(cursorPosition.y) |
| 1493 | .pointer( |
| 1494 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1495 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1496 | motionBuilder.downTime(eventTime); |
| 1497 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1498 | |
| 1499 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1500 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1501 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1504 | static InputEventInjectionResult injectMotionDown( |
| 1505 | const std::unique_ptr<InputDispatcher>& dispatcher, int32_t source, int32_t displayId, |
| 1506 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1507 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1510 | static InputEventInjectionResult injectMotionUp(const std::unique_ptr<InputDispatcher>& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1511 | int32_t source, int32_t displayId, |
| 1512 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1513 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1516 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1517 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1518 | // Define a valid key event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1519 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 1520 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, |
| 1521 | KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1522 | |
| 1523 | return args; |
| 1524 | } |
| 1525 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1526 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1527 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1528 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1529 | // Define a valid key event. |
| 1530 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1531 | displayId, 0, action, /* flags */ 0, AKEYCODE_C, KEY_C, AMETA_META_ON, |
| 1532 | currentTime); |
| 1533 | |
| 1534 | return args; |
| 1535 | } |
| 1536 | |
| 1537 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1538 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1539 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1540 | // Define a valid key event. |
| 1541 | NotifyKeyArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 1542 | displayId, 0, action, /* flags */ 0, AKEYCODE_ASSIST, KEY_ASSISTANT, |
| 1543 | AMETA_NONE, currentTime); |
| 1544 | |
| 1545 | return args; |
| 1546 | } |
| 1547 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1548 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1549 | int32_t displayId, |
| 1550 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1551 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1552 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1553 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1554 | } |
| 1555 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1556 | PointerProperties pointerProperties[pointerCount]; |
| 1557 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1558 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1559 | for (size_t i = 0; i < pointerCount; i++) { |
| 1560 | pointerProperties[i].clear(); |
| 1561 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1562 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1563 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1564 | pointerCoords[i].clear(); |
| 1565 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1566 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1567 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1568 | |
| 1569 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1570 | // Define a valid motion event. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1571 | NotifyMotionArgs args(/*id=*/0, currentTime, /*readTime=*/0, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1572 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1573 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1574 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1575 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1576 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1577 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1578 | |
| 1579 | return args; |
| 1580 | } |
| 1581 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1582 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1583 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1584 | } |
| 1585 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1586 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1587 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1588 | } |
| 1589 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1590 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1591 | const PointerCaptureRequest& request) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1592 | return NotifyPointerCaptureChangedArgs(/*id=*/0, systemTime(SYSTEM_TIME_MONOTONIC), request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1595 | /** |
| 1596 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1597 | * broken channel. |
| 1598 | */ |
| 1599 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1600 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1601 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1602 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1603 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1604 | |
| 1605 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1606 | |
| 1607 | // Window closes its channel, but the window remains. |
| 1608 | window->destroyReceiver(); |
| 1609 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1610 | } |
| 1611 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1612 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1613 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1614 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1615 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1616 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1617 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1619 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1620 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1621 | |
| 1622 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1623 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1624 | } |
| 1625 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1626 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1627 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1628 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1629 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1630 | |
| 1631 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1632 | // Inject a MotionEvent to an unknown display. |
| 1633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1634 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
| 1635 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1636 | |
| 1637 | // Window should receive motion event. |
| 1638 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1639 | } |
| 1640 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1641 | /** |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1642 | * Calling setInputWindows once should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1643 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1644 | * called twice. |
| 1645 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1646 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
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); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1650 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1651 | |
| 1652 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1653 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1654 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1655 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1656 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1657 | |
| 1658 | // Window should receive motion event. |
| 1659 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1660 | } |
| 1661 | |
| 1662 | /** |
| 1663 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1664 | */ |
| 1665 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1666 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1667 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1668 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1669 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1670 | |
| 1671 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1672 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1674 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1675 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1676 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1677 | |
| 1678 | // Window should receive motion event. |
| 1679 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1680 | } |
| 1681 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1682 | // The foreground window should receive the first touch down event. |
| 1683 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1684 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1685 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1686 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1687 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1688 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1689 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1690 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1691 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1692 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1693 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1694 | |
| 1695 | // 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] | 1696 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1697 | windowSecond->assertNoEvents(); |
| 1698 | } |
| 1699 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1700 | /** |
| 1701 | * Two windows: A top window, and a wallpaper behind the window. |
| 1702 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1703 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1704 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1705 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1706 | */ |
| 1707 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1708 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1709 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1710 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1711 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1712 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1713 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1714 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1715 | |
| 1716 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1717 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1718 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1719 | {100, 200})) |
| 1720 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1721 | |
| 1722 | // Both foreground window and its wallpaper should receive the touch down |
| 1723 | foregroundWindow->consumeMotionDown(); |
| 1724 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1725 | |
| 1726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1727 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1728 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1729 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1730 | |
| 1731 | foregroundWindow->consumeMotionMove(); |
| 1732 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1733 | |
| 1734 | // Now the foreground window goes away, but the wallpaper stays |
| 1735 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1736 | foregroundWindow->consumeMotionCancel(); |
| 1737 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1738 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1739 | } |
| 1740 | |
| 1741 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1742 | * Two fingers down on the window, and lift off the first finger. |
| 1743 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1744 | * contains a single pointer. |
| 1745 | */ |
| 1746 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1747 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1748 | sp<FakeWindowHandle> window = |
| 1749 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1750 | |
| 1751 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1752 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1753 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1754 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1755 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1756 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1757 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1758 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1759 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1760 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1761 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1762 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1763 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1764 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1765 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1766 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1767 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1768 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1769 | |
| 1770 | // Remove the window. The gesture should be canceled |
| 1771 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 1772 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1773 | window->consumeMotionEvent( |
| 1774 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1775 | } |
| 1776 | |
| 1777 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1778 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1779 | * with the following differences: |
| 1780 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1781 | * clean up the connection. |
| 1782 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1783 | * Ensure that there's no crash in the dispatcher. |
| 1784 | */ |
| 1785 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1786 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1787 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1788 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1789 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1790 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1791 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1792 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1793 | |
| 1794 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
| 1795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1796 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1797 | {100, 200})) |
| 1798 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1799 | |
| 1800 | // Both foreground window and its wallpaper should receive the touch down |
| 1801 | foregroundWindow->consumeMotionDown(); |
| 1802 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1803 | |
| 1804 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1805 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 1806 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1807 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1808 | |
| 1809 | foregroundWindow->consumeMotionMove(); |
| 1810 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1811 | |
| 1812 | // Wallpaper closes its channel, but the window remains. |
| 1813 | wallpaperWindow->destroyReceiver(); |
| 1814 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1815 | |
| 1816 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1817 | // is no longer valid. |
| 1818 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wallpaperWindow}}}); |
| 1819 | foregroundWindow->consumeMotionCancel(); |
| 1820 | } |
| 1821 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1822 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1823 | public ::testing::WithParamInterface<bool> {}; |
| 1824 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1825 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1826 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1827 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1828 | * The top window gets a multitouch gesture. |
| 1829 | * Ensure that wallpaper gets the same gesture. |
| 1830 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1831 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1832 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1833 | sp<FakeWindowHandle> foregroundWindow = |
| 1834 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1835 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1836 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1837 | |
| 1838 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1839 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1840 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1841 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1842 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {foregroundWindow, wallpaperWindow}}}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1843 | |
| 1844 | // Touch down on top window |
| 1845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1846 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1847 | {100, 100})) |
| 1848 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1849 | |
| 1850 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1851 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1852 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1853 | |
| 1854 | // Second finger down on the top window |
| 1855 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1856 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1857 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1858 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1859 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1860 | .build(); |
| 1861 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1862 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1863 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1864 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1865 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1866 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1867 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1868 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1869 | |
| 1870 | const MotionEvent secondFingerUpEvent = |
| 1871 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1872 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1873 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1874 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1875 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1876 | .build(); |
| 1877 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1878 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 1879 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1880 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1881 | foregroundWindow->consumeMotionPointerUp(0); |
| 1882 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1883 | |
| 1884 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1885 | injectMotionEvent(mDispatcher, |
| 1886 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1887 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1888 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1889 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 1890 | .pointer(PointerBuilder(/* id */ 1, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1891 | ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1892 | .x(100) |
| 1893 | .y(100)) |
| 1894 | .build(), |
| 1895 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1896 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1897 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1898 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /** |
| 1902 | * Two windows: a window on the left and window on the right. |
| 1903 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1904 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1905 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1906 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1907 | * ACTION_POINTER_DOWN(1). |
| 1908 | */ |
| 1909 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1910 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1911 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1912 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1913 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1914 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1915 | |
| 1916 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1917 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1918 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1919 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1920 | |
| 1921 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1922 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1923 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1924 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1925 | |
| 1926 | mDispatcher->setInputWindows( |
| 1927 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
| 1928 | |
| 1929 | // Touch down on left window |
| 1930 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1931 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1932 | {100, 100})) |
| 1933 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1934 | |
| 1935 | // Both foreground window and its wallpaper should receive the touch down |
| 1936 | leftWindow->consumeMotionDown(); |
| 1937 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1938 | |
| 1939 | // Second finger down on the right window |
| 1940 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1941 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1942 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1943 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1944 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1945 | .build(); |
| 1946 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1947 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 1948 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1949 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1950 | |
| 1951 | leftWindow->consumeMotionMove(); |
| 1952 | // Since the touch is split, right window gets ACTION_DOWN |
| 1953 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1954 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1955 | expectedWallpaperFlags); |
| 1956 | |
| 1957 | // Now, leftWindow, which received the first finger, disappears. |
| 1958 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightWindow, wallpaperWindow}}}); |
| 1959 | leftWindow->consumeMotionCancel(); |
| 1960 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1961 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1962 | |
| 1963 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1964 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1965 | const MotionEvent secondFingerMoveEvent = |
| 1966 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1967 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1968 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1969 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1970 | .build(); |
| 1971 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1972 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 1973 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1974 | rightWindow->consumeMotionMove(); |
| 1975 | |
| 1976 | leftWindow->assertNoEvents(); |
| 1977 | rightWindow->assertNoEvents(); |
| 1978 | wallpaperWindow->assertNoEvents(); |
| 1979 | } |
| 1980 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1981 | /** |
| 1982 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 1983 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 1984 | * The right window should receive ACTION_DOWN. |
| 1985 | */ |
| 1986 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1987 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1988 | sp<FakeWindowHandle> leftWindow = |
| 1989 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1990 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1991 | leftWindow->setDupTouchToWallpaper(true); |
| 1992 | leftWindow->setSlippery(true); |
| 1993 | |
| 1994 | sp<FakeWindowHandle> rightWindow = |
| 1995 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1996 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1997 | |
| 1998 | sp<FakeWindowHandle> wallpaperWindow = |
| 1999 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2000 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2001 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2002 | mDispatcher->setInputWindows( |
| 2003 | {{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow, wallpaperWindow}}}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2004 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2005 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2006 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2007 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2008 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2009 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2010 | |
| 2011 | // Both foreground window and its wallpaper should receive the touch down |
| 2012 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2013 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2014 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2015 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2017 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2018 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2019 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2020 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2021 | leftWindow->consumeMotionCancel(); |
| 2022 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2023 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2026 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2027 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2028 | * interactive, it might stop sending this flag. |
| 2029 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2030 | * to have a consistent input stream. |
| 2031 | * |
| 2032 | * Test procedure: |
| 2033 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2034 | * DOWN (new gesture). |
| 2035 | * |
| 2036 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2037 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2038 | * |
| 2039 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2040 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2041 | */ |
| 2042 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2043 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2044 | sp<FakeWindowHandle> spyWindow = |
| 2045 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2046 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2047 | spyWindow->setTrustedOverlay(true); |
| 2048 | spyWindow->setSpy(true); |
| 2049 | |
| 2050 | sp<FakeWindowHandle> window = |
| 2051 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2052 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2053 | |
| 2054 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2055 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2056 | |
| 2057 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2058 | mDispatcher->notifyMotion( |
| 2059 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2060 | .deviceId(touchDeviceId) |
| 2061 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2062 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2063 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2064 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2065 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2066 | .deviceId(touchDeviceId) |
| 2067 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2068 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2069 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2070 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2071 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2072 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2073 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2074 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2075 | |
| 2076 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2077 | mDispatcher->notifyMotion( |
| 2078 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2079 | .deviceId(touchDeviceId) |
| 2080 | .policyFlags(0) |
| 2081 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2082 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2083 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2084 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2085 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2086 | |
| 2087 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2088 | // 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] | 2089 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2090 | |
| 2091 | // Start new gesture |
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 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2099 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2100 | |
| 2101 | // No more events |
| 2102 | spyWindow->assertNoEvents(); |
| 2103 | window->assertNoEvents(); |
| 2104 | } |
| 2105 | |
| 2106 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2107 | * Two windows: a window on the left and a window on the right. |
| 2108 | * Mouse is hovered from the right window into the left window. |
| 2109 | * Next, we tap on the left window, where the cursor was last seen. |
| 2110 | * The second tap is done onto the right window. |
| 2111 | * The mouse and tap are from two different devices. |
| 2112 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2113 | * explicitly helps during debugging. |
| 2114 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2115 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2116 | */ |
| 2117 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2118 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2119 | sp<FakeWindowHandle> leftWindow = |
| 2120 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2121 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2122 | |
| 2123 | sp<FakeWindowHandle> rightWindow = |
| 2124 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2125 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2126 | |
| 2127 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2128 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2129 | // stale. |
| 2130 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2131 | const int32_t mouseDeviceId = 6; |
| 2132 | const int32_t touchDeviceId = 4; |
| 2133 | // Move the cursor from right |
| 2134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2135 | injectMotionEvent(mDispatcher, |
| 2136 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2137 | AINPUT_SOURCE_MOUSE) |
| 2138 | .deviceId(mouseDeviceId) |
| 2139 | .downTime(baseTime + 10) |
| 2140 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2141 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2142 | .x(300) |
| 2143 | .y(100)) |
| 2144 | .build())); |
| 2145 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2146 | |
| 2147 | // .. to the left window |
| 2148 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2149 | injectMotionEvent(mDispatcher, |
| 2150 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2151 | AINPUT_SOURCE_MOUSE) |
| 2152 | .deviceId(mouseDeviceId) |
| 2153 | .downTime(baseTime + 10) |
| 2154 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2155 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2156 | .x(110) |
| 2157 | .y(100)) |
| 2158 | .build())); |
| 2159 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2160 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2161 | // Now tap the left window |
| 2162 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2163 | injectMotionEvent(mDispatcher, |
| 2164 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2165 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2166 | .deviceId(touchDeviceId) |
| 2167 | .downTime(baseTime + 40) |
| 2168 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2169 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2170 | .x(100) |
| 2171 | .y(100)) |
| 2172 | .build())); |
| 2173 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2174 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2175 | |
| 2176 | // release tap |
| 2177 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2178 | injectMotionEvent(mDispatcher, |
| 2179 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2180 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2181 | .deviceId(touchDeviceId) |
| 2182 | .downTime(baseTime + 40) |
| 2183 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2184 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2185 | .x(100) |
| 2186 | .y(100)) |
| 2187 | .build())); |
| 2188 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2189 | |
| 2190 | // Tap the window on the right |
| 2191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2192 | injectMotionEvent(mDispatcher, |
| 2193 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2194 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2195 | .deviceId(touchDeviceId) |
| 2196 | .downTime(baseTime + 60) |
| 2197 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2198 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2199 | .x(300) |
| 2200 | .y(100)) |
| 2201 | .build())); |
| 2202 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2203 | |
| 2204 | // release tap |
| 2205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2206 | injectMotionEvent(mDispatcher, |
| 2207 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2208 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2209 | .deviceId(touchDeviceId) |
| 2210 | .downTime(baseTime + 60) |
| 2211 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2212 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2213 | .x(300) |
| 2214 | .y(100)) |
| 2215 | .build())); |
| 2216 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2217 | |
| 2218 | // No more events |
| 2219 | leftWindow->assertNoEvents(); |
| 2220 | rightWindow->assertNoEvents(); |
| 2221 | } |
| 2222 | |
| 2223 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2224 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2225 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2226 | * While the top window is present, the hovering is stopped. |
| 2227 | * Later, hovering gets resumed again. |
| 2228 | * Ensure that new hover gesture is handled correctly. |
| 2229 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2230 | * to the window that's currently being hovered over. |
| 2231 | */ |
| 2232 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2233 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2234 | sp<FakeWindowHandle> window = |
| 2235 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2236 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2237 | |
| 2238 | // Only a single window is present at first |
| 2239 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2240 | |
| 2241 | // Start hovering in the window |
| 2242 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2243 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2244 | .build()); |
| 2245 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2246 | |
| 2247 | // Now, an obscuring window appears! |
| 2248 | sp<FakeWindowHandle> obscuringWindow = |
| 2249 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2250 | ADISPLAY_ID_DEFAULT, |
| 2251 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2252 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2253 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2254 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2255 | obscuringWindow->setNoInputChannel(true); |
| 2256 | obscuringWindow->setFocusable(false); |
| 2257 | obscuringWindow->setAlpha(1.0); |
| 2258 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2259 | |
| 2260 | // While this new obscuring window is present, the hovering is stopped |
| 2261 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2262 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2263 | .build()); |
| 2264 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2265 | |
| 2266 | // Now the obscuring window goes away. |
| 2267 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2268 | |
| 2269 | // And a new hover gesture starts. |
| 2270 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2271 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2272 | .build()); |
| 2273 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2274 | } |
| 2275 | |
| 2276 | /** |
| 2277 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2278 | * the obscuring window. |
| 2279 | */ |
| 2280 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2281 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2282 | sp<FakeWindowHandle> window = |
| 2283 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2284 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2285 | |
| 2286 | // Only a single window is present at first |
| 2287 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2288 | |
| 2289 | // Start hovering in the window |
| 2290 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2291 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2292 | .build()); |
| 2293 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2294 | |
| 2295 | // Now, an obscuring window appears! |
| 2296 | sp<FakeWindowHandle> obscuringWindow = |
| 2297 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2298 | ADISPLAY_ID_DEFAULT, |
| 2299 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
| 2300 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2301 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2302 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2303 | obscuringWindow->setNoInputChannel(true); |
| 2304 | obscuringWindow->setFocusable(false); |
| 2305 | obscuringWindow->setAlpha(1.0); |
| 2306 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 2307 | |
| 2308 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2309 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2310 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2311 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2312 | .build()); |
| 2313 | obscuringWindow->assertNoEvents(); |
| 2314 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2315 | |
| 2316 | // Now the obscuring window goes away. |
| 2317 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2318 | |
| 2319 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2320 | // so it should generate a HOVER_ENTER |
| 2321 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2322 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2323 | .build()); |
| 2324 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2325 | |
| 2326 | // Now the MOVE should be getting dispatched normally |
| 2327 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2328 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2329 | .build()); |
| 2330 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2331 | } |
| 2332 | |
| 2333 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2334 | * Two windows: a window on the left and a window on the right. |
| 2335 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2336 | * down. Then, on the left window, also place second touch pointer down. |
| 2337 | * This test tries to reproduce a crash. |
| 2338 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2339 | */ |
| 2340 | TEST_F(InputDispatcherTest, MultiDeviceSplitTouch) { |
| 2341 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2342 | sp<FakeWindowHandle> leftWindow = |
| 2343 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2344 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2345 | |
| 2346 | sp<FakeWindowHandle> rightWindow = |
| 2347 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2348 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2349 | |
| 2350 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2351 | |
| 2352 | const int32_t touchDeviceId = 4; |
| 2353 | const int32_t mouseDeviceId = 6; |
| 2354 | NotifyMotionArgs args; |
| 2355 | |
| 2356 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2357 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2358 | .deviceId(mouseDeviceId) |
| 2359 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2360 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2361 | leftWindow->consumeMotionEvent( |
| 2362 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2363 | |
| 2364 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2365 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2366 | .deviceId(mouseDeviceId) |
| 2367 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2368 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2369 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2370 | |
| 2371 | leftWindow->consumeMotionEvent( |
| 2372 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2373 | leftWindow->consumeMotionEvent( |
| 2374 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2375 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2376 | mDispatcher->notifyMotion( |
| 2377 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2378 | .deviceId(mouseDeviceId) |
| 2379 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2380 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2381 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2382 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2383 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2384 | |
| 2385 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2386 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2387 | .deviceId(touchDeviceId) |
| 2388 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2389 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2390 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2391 | |
| 2392 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2393 | |
| 2394 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2395 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2396 | .deviceId(touchDeviceId) |
| 2397 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2398 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2399 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2400 | leftWindow->consumeMotionEvent( |
| 2401 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2402 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2403 | // current implementation. |
| 2404 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2405 | rightWindow->consumeMotionEvent( |
| 2406 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2407 | |
| 2408 | leftWindow->assertNoEvents(); |
| 2409 | rightWindow->assertNoEvents(); |
| 2410 | } |
| 2411 | |
| 2412 | /** |
| 2413 | * On a single window, use two different devices: mouse and touch. |
| 2414 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2415 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2416 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2417 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2418 | * represent a new gesture. |
| 2419 | */ |
| 2420 | TEST_F(InputDispatcherTest, MixedTouchAndMouseWithPointerDown) { |
| 2421 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2422 | sp<FakeWindowHandle> window = |
| 2423 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2424 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2425 | |
| 2426 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2427 | |
| 2428 | const int32_t touchDeviceId = 4; |
| 2429 | const int32_t mouseDeviceId = 6; |
| 2430 | NotifyMotionArgs args; |
| 2431 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2432 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2433 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2434 | .deviceId(touchDeviceId) |
| 2435 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2436 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2437 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2438 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2439 | .deviceId(touchDeviceId) |
| 2440 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2441 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2442 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2443 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2444 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2445 | .deviceId(touchDeviceId) |
| 2446 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2447 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2448 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2449 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2450 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2451 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2452 | |
| 2453 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2454 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2455 | .deviceId(mouseDeviceId) |
| 2456 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2457 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2458 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2459 | |
| 2460 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2461 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2462 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2463 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2464 | mDispatcher->notifyMotion( |
| 2465 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2466 | .deviceId(mouseDeviceId) |
| 2467 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2468 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2469 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2470 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2471 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2472 | |
| 2473 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2474 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2475 | .deviceId(touchDeviceId) |
| 2476 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2477 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2478 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2479 | // The pointer_down event should be ignored |
| 2480 | window->assertNoEvents(); |
| 2481 | } |
| 2482 | |
| 2483 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2484 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 2485 | * the injected event. |
| 2486 | */ |
| 2487 | TEST_F(InputDispatcherTest, UnfinishedInjectedEvent) { |
| 2488 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2489 | sp<FakeWindowHandle> window = |
| 2490 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2491 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2492 | |
| 2493 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2494 | |
| 2495 | const int32_t touchDeviceId = 4; |
| 2496 | NotifyMotionArgs args; |
| 2497 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 2498 | // completion. |
| 2499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2500 | injectMotionEvent(mDispatcher, |
| 2501 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2502 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2503 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2504 | .x(50) |
| 2505 | .y(50)) |
| 2506 | .build())); |
| 2507 | window->consumeMotionEvent( |
| 2508 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2509 | |
| 2510 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 2511 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2512 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2513 | .deviceId(touchDeviceId) |
| 2514 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2515 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2516 | |
| 2517 | window->consumeMotionEvent( |
| 2518 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2519 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2520 | } |
| 2521 | |
| 2522 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2523 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2524 | * |
| 2525 | * Two windows: a window on the left and a window on the right. |
| 2526 | * Mouse is hovered over the left window. |
| 2527 | * Next, we tap on the left window, where the cursor was last seen. |
| 2528 | * |
| 2529 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2530 | * the left window. |
| 2531 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2532 | * window (first), and then another on the left window (second). |
| 2533 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2534 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2535 | */ |
| 2536 | TEST_F(InputDispatcherTest, HoverTapAndSplitTouch) { |
| 2537 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2538 | sp<FakeWindowHandle> leftWindow = |
| 2539 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2540 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2541 | |
| 2542 | sp<FakeWindowHandle> rightWindow = |
| 2543 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2544 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2545 | |
| 2546 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {leftWindow, rightWindow}}}); |
| 2547 | |
| 2548 | const int32_t mouseDeviceId = 6; |
| 2549 | const int32_t touchDeviceId = 4; |
| 2550 | // Hover over the left window. Keep the cursor there. |
| 2551 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2552 | injectMotionEvent(mDispatcher, |
| 2553 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2554 | AINPUT_SOURCE_MOUSE) |
| 2555 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2556 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2557 | .x(50) |
| 2558 | .y(50)) |
| 2559 | .build())); |
| 2560 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2561 | |
| 2562 | // Tap on left window |
| 2563 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2564 | injectMotionEvent(mDispatcher, |
| 2565 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2566 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2567 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2568 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2569 | .x(100) |
| 2570 | .y(100)) |
| 2571 | .build())); |
| 2572 | |
| 2573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2574 | injectMotionEvent(mDispatcher, |
| 2575 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2576 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2577 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2578 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2579 | .x(100) |
| 2580 | .y(100)) |
| 2581 | .build())); |
| 2582 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2583 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2584 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2585 | |
| 2586 | // First finger down on right window |
| 2587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2588 | injectMotionEvent(mDispatcher, |
| 2589 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2590 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2591 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2592 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2593 | .x(300) |
| 2594 | .y(100)) |
| 2595 | .build())); |
| 2596 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2597 | |
| 2598 | // Second finger down on the left window |
| 2599 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2600 | injectMotionEvent(mDispatcher, |
| 2601 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2602 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2603 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2604 | .x(300) |
| 2605 | .y(100)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2606 | .pointer(PointerBuilder(1, ToolType::FINGER) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2607 | .x(100) |
| 2608 | .y(100)) |
| 2609 | .build())); |
| 2610 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2611 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2612 | |
| 2613 | // No more events |
| 2614 | leftWindow->assertNoEvents(); |
| 2615 | rightWindow->assertNoEvents(); |
| 2616 | } |
| 2617 | |
| 2618 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2619 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2620 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2621 | * touch is gone, stylus hovering should start working again. |
| 2622 | */ |
| 2623 | TEST_F(InputDispatcherTest, StylusHoverAndTouchTap) { |
| 2624 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2625 | sp<FakeWindowHandle> window = |
| 2626 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2627 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2628 | |
| 2629 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2630 | |
| 2631 | const int32_t stylusDeviceId = 5; |
| 2632 | const int32_t touchDeviceId = 4; |
| 2633 | // Start hovering with stylus |
| 2634 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2635 | injectMotionEvent(mDispatcher, |
| 2636 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2637 | AINPUT_SOURCE_STYLUS) |
| 2638 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2639 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2640 | .x(50) |
| 2641 | .y(50)) |
| 2642 | .build())); |
| 2643 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2644 | |
| 2645 | // Finger down on the window |
| 2646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2647 | injectMotionEvent(mDispatcher, |
| 2648 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2649 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2650 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2651 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2652 | .x(100) |
| 2653 | .y(100)) |
| 2654 | .build())); |
| 2655 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2656 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2657 | |
| 2658 | // Try to continue hovering with stylus. Since we are already down, injection should fail |
| 2659 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 2660 | injectMotionEvent(mDispatcher, |
| 2661 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2662 | AINPUT_SOURCE_STYLUS) |
| 2663 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2664 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2665 | .x(50) |
| 2666 | .y(50)) |
| 2667 | .build())); |
| 2668 | // No event should be sent. This event should be ignored because a pointer from another device |
| 2669 | // is already down. |
| 2670 | |
| 2671 | // Lift up the finger |
| 2672 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2673 | injectMotionEvent(mDispatcher, |
| 2674 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2675 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2676 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2677 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2678 | .x(100) |
| 2679 | .y(100)) |
| 2680 | .build())); |
| 2681 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2682 | |
| 2683 | // Now that the touch is gone, stylus hovering should start working again |
| 2684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2685 | injectMotionEvent(mDispatcher, |
| 2686 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2687 | AINPUT_SOURCE_STYLUS) |
| 2688 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2689 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2690 | .x(50) |
| 2691 | .y(50)) |
| 2692 | .build())); |
| 2693 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2694 | // No more events |
| 2695 | window->assertNoEvents(); |
| 2696 | } |
| 2697 | |
| 2698 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2699 | * A spy window above a window with no input channel. |
| 2700 | * Start hovering with a stylus device, and then tap with it. |
| 2701 | * Ensure spy window receives the entire sequence. |
| 2702 | */ |
| 2703 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 2704 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2705 | sp<FakeWindowHandle> spyWindow = |
| 2706 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2707 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2708 | spyWindow->setTrustedOverlay(true); |
| 2709 | spyWindow->setSpy(true); |
| 2710 | sp<FakeWindowHandle> window = |
| 2711 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2712 | window->setNoInputChannel(true); |
| 2713 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2714 | |
| 2715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2716 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2717 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2718 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2719 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2720 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2721 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2722 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2723 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2724 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2725 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2726 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2727 | |
| 2728 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2729 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2730 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2731 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2732 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2733 | |
| 2734 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2735 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 2736 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2737 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2738 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 2739 | |
| 2740 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2741 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2742 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2743 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2744 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2745 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2746 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2747 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 2748 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2749 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2750 | |
| 2751 | // No more events |
| 2752 | spyWindow->assertNoEvents(); |
| 2753 | window->assertNoEvents(); |
| 2754 | } |
| 2755 | |
| 2756 | /** |
| 2757 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 2758 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 2759 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 2760 | * While the mouse is down, new move events from the touch device should be ignored. |
| 2761 | */ |
| 2762 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 2763 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2764 | sp<FakeWindowHandle> spyWindow = |
| 2765 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2766 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2767 | spyWindow->setTrustedOverlay(true); |
| 2768 | spyWindow->setSpy(true); |
| 2769 | sp<FakeWindowHandle> window = |
| 2770 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2771 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2772 | |
| 2773 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 2774 | |
| 2775 | const int32_t mouseDeviceId = 7; |
| 2776 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2777 | |
| 2778 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2779 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2780 | .deviceId(mouseDeviceId) |
| 2781 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2782 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2783 | spyWindow->consumeMotionEvent( |
| 2784 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2785 | window->consumeMotionEvent( |
| 2786 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2787 | |
| 2788 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2789 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2790 | .deviceId(touchDeviceId) |
| 2791 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 2792 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2793 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2794 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2795 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2796 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2797 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2798 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2799 | .deviceId(touchDeviceId) |
| 2800 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 2801 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2802 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2803 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2804 | |
| 2805 | // Pilfer the stream |
| 2806 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 2807 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 2808 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2809 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2810 | .deviceId(touchDeviceId) |
| 2811 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 2812 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2813 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2814 | |
| 2815 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2816 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2817 | .deviceId(mouseDeviceId) |
| 2818 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2819 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2820 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2821 | |
| 2822 | spyWindow->consumeMotionEvent( |
| 2823 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2824 | spyWindow->consumeMotionEvent( |
| 2825 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2826 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2827 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2828 | mDispatcher->notifyMotion( |
| 2829 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2830 | .deviceId(mouseDeviceId) |
| 2831 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2832 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2833 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2834 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2835 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2836 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2837 | |
| 2838 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2839 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 2840 | .deviceId(mouseDeviceId) |
| 2841 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2842 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 2843 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2844 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2845 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 2846 | |
| 2847 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2848 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2849 | .deviceId(touchDeviceId) |
| 2850 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 2851 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2852 | |
| 2853 | // No more events |
| 2854 | spyWindow->assertNoEvents(); |
| 2855 | window->assertNoEvents(); |
| 2856 | } |
| 2857 | |
| 2858 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2859 | * On the display, have a single window, and also an area where there's no window. |
| 2860 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 2861 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 2862 | */ |
| 2863 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 2864 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2865 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2866 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2867 | |
| 2868 | mDispatcher->setInputWindows({{DISPLAY_ID, {window}}}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2869 | |
| 2870 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2871 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2872 | |
| 2873 | mDispatcher->waitForIdle(); |
| 2874 | window->assertNoEvents(); |
| 2875 | |
| 2876 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2877 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2878 | mDispatcher->waitForIdle(); |
| 2879 | window->consumeMotionDown(); |
| 2880 | } |
| 2881 | |
| 2882 | /** |
| 2883 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 2884 | * non-touchable window. |
| 2885 | */ |
| 2886 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 2887 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2888 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2889 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2890 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2891 | window1->setTouchable(false); |
| 2892 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2893 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2894 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2895 | |
| 2896 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2897 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2898 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2899 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2900 | |
| 2901 | mDispatcher->waitForIdle(); |
| 2902 | window1->assertNoEvents(); |
| 2903 | window2->assertNoEvents(); |
| 2904 | |
| 2905 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2906 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2907 | mDispatcher->waitForIdle(); |
| 2908 | window2->consumeMotionDown(); |
| 2909 | } |
| 2910 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2911 | /** |
| 2912 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 2913 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 2914 | */ |
| 2915 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 2916 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2917 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2918 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2919 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 2920 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2921 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2922 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 2923 | |
| 2924 | mDispatcher->setInputWindows({{DISPLAY_ID, {window1, window2}}}); |
| 2925 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2926 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2927 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2928 | |
| 2929 | mDispatcher->waitForIdle(); |
| 2930 | InputEvent* inputEvent1 = window1->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2931 | ASSERT_NE(inputEvent1, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2932 | window2->assertNoEvents(); |
| 2933 | MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1); |
| 2934 | nsecs_t downTimeForWindow1 = motionEvent1.getDownTime(); |
| 2935 | ASSERT_EQ(motionEvent1.getDownTime(), motionEvent1.getEventTime()); |
| 2936 | |
| 2937 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2938 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2939 | mDispatcher->waitForIdle(); |
| 2940 | InputEvent* inputEvent2 = window2->consume(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2941 | ASSERT_NE(inputEvent2, nullptr); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2942 | MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2); |
| 2943 | nsecs_t downTimeForWindow2 = motionEvent2.getDownTime(); |
| 2944 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
| 2945 | ASSERT_EQ(motionEvent2.getDownTime(), motionEvent2.getEventTime()); |
| 2946 | |
| 2947 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2948 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2949 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2950 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2951 | |
| 2952 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2953 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2954 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2955 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2956 | |
| 2957 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 2958 | window1->consumeMotionMove(); |
| 2959 | window1->assertNoEvents(); |
| 2960 | |
| 2961 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2962 | mDispatcher->notifyMotion( |
| 2963 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2964 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2965 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2966 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2967 | mDispatcher->notifyMotion( |
| 2968 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2969 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 2970 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2973 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2974 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2975 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2976 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2977 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2978 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2979 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2980 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2981 | |
| 2982 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2983 | |
| 2984 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 2985 | |
| 2986 | // 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] | 2987 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2988 | injectMotionEvent(mDispatcher, |
| 2989 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2990 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2991 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2992 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 2993 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2994 | |
| 2995 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2996 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 2997 | injectMotionEvent(mDispatcher, |
| 2998 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2999 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3000 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3001 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3002 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3003 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3004 | |
| 3005 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3006 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3007 | injectMotionEvent(mDispatcher, |
| 3008 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3009 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3010 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3011 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3012 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3013 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3014 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3015 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3016 | injectMotionEvent(mDispatcher, |
| 3017 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3018 | AINPUT_SOURCE_MOUSE) |
| 3019 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3020 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3021 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3022 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3023 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3024 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3025 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3026 | injectMotionEvent(mDispatcher, |
| 3027 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3028 | AINPUT_SOURCE_MOUSE) |
| 3029 | .buttonState(0) |
| 3030 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3031 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3032 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3033 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3034 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3035 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3036 | injectMotionEvent(mDispatcher, |
| 3037 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3038 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3039 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3040 | .build())); |
| 3041 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3042 | |
| 3043 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3044 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3045 | injectMotionEvent(mDispatcher, |
| 3046 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3047 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3048 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3049 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3050 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3051 | |
| 3052 | // No more events |
| 3053 | windowLeft->assertNoEvents(); |
| 3054 | windowRight->assertNoEvents(); |
| 3055 | } |
| 3056 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3057 | /** |
| 3058 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3059 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3060 | * currently active gesture should be canceled, and the new one should proceed. |
| 3061 | */ |
| 3062 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3063 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3064 | sp<FakeWindowHandle> window = |
| 3065 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3066 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3067 | |
| 3068 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3069 | |
| 3070 | const int32_t touchDeviceId = 4; |
| 3071 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3072 | |
| 3073 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3074 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3075 | .deviceId(touchDeviceId) |
| 3076 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3077 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3078 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3079 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3080 | .deviceId(touchDeviceId) |
| 3081 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3082 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3083 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3084 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3085 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3086 | |
| 3087 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3088 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3089 | .deviceId(mouseDeviceId) |
| 3090 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3091 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3092 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3093 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3094 | WithPointerCount(2u))); |
| 3095 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3096 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3097 | mDispatcher->notifyMotion( |
| 3098 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3099 | .deviceId(mouseDeviceId) |
| 3100 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3101 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3102 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3103 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3104 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3105 | |
| 3106 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3107 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3108 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3109 | .deviceId(touchDeviceId) |
| 3110 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3111 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3112 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3113 | window->assertNoEvents(); |
| 3114 | } |
| 3115 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3116 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3117 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3118 | |
| 3119 | sp<FakeWindowHandle> spyWindow = |
| 3120 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3121 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3122 | spyWindow->setTrustedOverlay(true); |
| 3123 | spyWindow->setSpy(true); |
| 3124 | sp<FakeWindowHandle> window = |
| 3125 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3126 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3127 | |
| 3128 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3129 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, window}}}); |
| 3130 | |
| 3131 | // Send mouse cursor to the window |
| 3132 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3133 | injectMotionEvent(mDispatcher, |
| 3134 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3135 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3136 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3137 | .x(100) |
| 3138 | .y(100)) |
| 3139 | .build())); |
| 3140 | |
| 3141 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3142 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3143 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3144 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3145 | |
| 3146 | window->assertNoEvents(); |
| 3147 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3148 | } |
| 3149 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3150 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 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 | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3171 | .x(100) |
| 3172 | .y(100)) |
| 3173 | .build())); |
| 3174 | |
| 3175 | // Move mouse cursor |
| 3176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3177 | injectMotionEvent(mDispatcher, |
| 3178 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3179 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3180 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3181 | .x(110) |
| 3182 | .y(110)) |
| 3183 | .build())); |
| 3184 | |
| 3185 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3186 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3187 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3188 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3189 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3190 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3191 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3192 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3193 | // Touch down on the window |
| 3194 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3195 | injectMotionEvent(mDispatcher, |
| 3196 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3197 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3198 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3199 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3200 | .x(200) |
| 3201 | .y(200)) |
| 3202 | .build())); |
| 3203 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3204 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3205 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3206 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3207 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3208 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3209 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3210 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3211 | |
| 3212 | // pilfer the motion, retaining the gesture on the spy window. |
| 3213 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3214 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3215 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3216 | |
| 3217 | // Touch UP on the window |
| 3218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3219 | injectMotionEvent(mDispatcher, |
| 3220 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3221 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3222 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3223 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3224 | .x(200) |
| 3225 | .y(200)) |
| 3226 | .build())); |
| 3227 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3228 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3229 | |
| 3230 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3231 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3232 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3233 | |
| 3234 | // One more tap - DOWN |
| 3235 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3236 | injectMotionEvent(mDispatcher, |
| 3237 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3238 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3239 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3240 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3241 | .x(250) |
| 3242 | .y(250)) |
| 3243 | .build())); |
| 3244 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3245 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3246 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3247 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3248 | |
| 3249 | // Touch UP on the window |
| 3250 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3251 | injectMotionEvent(mDispatcher, |
| 3252 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3253 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3254 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3255 | .pointer(PointerBuilder(0, ToolType::FINGER) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3256 | .x(250) |
| 3257 | .y(250)) |
| 3258 | .build())); |
| 3259 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3260 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3261 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3262 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3263 | |
| 3264 | window->assertNoEvents(); |
| 3265 | spyWindow->assertNoEvents(); |
| 3266 | } |
| 3267 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3268 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3269 | // directly in this test. |
| 3270 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3271 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3272 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3273 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3274 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3275 | |
| 3276 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3277 | |
| 3278 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3279 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3281 | injectMotionEvent(mDispatcher, |
| 3282 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3283 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3284 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3285 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3286 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3287 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3288 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3289 | injectMotionEvent(mDispatcher, |
| 3290 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3291 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3292 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3293 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3294 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3295 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3296 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3297 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3298 | injectMotionEvent(mDispatcher, |
| 3299 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3300 | AINPUT_SOURCE_MOUSE) |
| 3301 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3302 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3303 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3304 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3305 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3306 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3308 | injectMotionEvent(mDispatcher, |
| 3309 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3310 | AINPUT_SOURCE_MOUSE) |
| 3311 | .buttonState(0) |
| 3312 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3313 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3314 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3315 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3316 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3317 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3318 | injectMotionEvent(mDispatcher, |
| 3319 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3320 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3321 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3322 | .build())); |
| 3323 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3324 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3325 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 3326 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 3327 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3328 | injectMotionEvent(mDispatcher, |
| 3329 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3330 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3331 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3332 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3333 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3334 | } |
| 3335 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3336 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3337 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3338 | * is generated. |
| 3339 | */ |
| 3340 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3341 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3342 | sp<FakeWindowHandle> window = |
| 3343 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3344 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3345 | |
| 3346 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3347 | |
| 3348 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3349 | |
| 3350 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3351 | injectMotionEvent(mDispatcher, |
| 3352 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3353 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3354 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3355 | .x(300) |
| 3356 | .y(400)) |
| 3357 | .build())); |
| 3358 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3359 | |
| 3360 | // Remove the window, but keep the channel. |
| 3361 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3362 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3363 | } |
| 3364 | |
| 3365 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3366 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3367 | */ |
| 3368 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3369 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3370 | sp<FakeWindowHandle> window = |
| 3371 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3372 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3373 | |
| 3374 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3375 | |
| 3376 | const int32_t mouseDeviceId = 7; |
| 3377 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3378 | |
| 3379 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3380 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3381 | .deviceId(mouseDeviceId) |
| 3382 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 3383 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3384 | window->consumeMotionEvent( |
| 3385 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3386 | |
| 3387 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3388 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3389 | .deviceId(touchDeviceId) |
| 3390 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3391 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3392 | |
| 3393 | window->consumeMotionEvent( |
| 3394 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3395 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3396 | } |
| 3397 | |
| 3398 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3399 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3400 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3401 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3402 | */ |
| 3403 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3404 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3405 | sp<FakeWindowHandle> window = |
| 3406 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3407 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3408 | |
| 3409 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3410 | |
| 3411 | // Inject a hover_move from mouse. |
| 3412 | NotifyMotionArgs motionArgs = |
| 3413 | generateMotionArgs(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE, |
| 3414 | ADISPLAY_ID_DEFAULT, {{50, 50}}); |
| 3415 | motionArgs.xCursorPosition = 50; |
| 3416 | motionArgs.yCursorPosition = 50; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3417 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3418 | ASSERT_NO_FATAL_FAILURE( |
| 3419 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3420 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3421 | |
| 3422 | // Tap on the window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3423 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3424 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3425 | {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3426 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3427 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3428 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3429 | |
| 3430 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3431 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3432 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3433 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3434 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3435 | ADISPLAY_ID_DEFAULT, {{10, 10}})); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3436 | ASSERT_NO_FATAL_FAILURE( |
| 3437 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3438 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3439 | } |
| 3440 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3441 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3442 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3443 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3444 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3445 | ADISPLAY_ID_DEFAULT); |
| 3446 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3447 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3448 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3449 | SECOND_DISPLAY_ID); |
| 3450 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3451 | |
| 3452 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3453 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3454 | |
| 3455 | // Set cursor position in window in default display and check that hover enter and move |
| 3456 | // events are generated. |
| 3457 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3458 | injectMotionEvent(mDispatcher, |
| 3459 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3460 | AINPUT_SOURCE_MOUSE) |
| 3461 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3462 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3463 | .x(300) |
| 3464 | .y(600)) |
| 3465 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3466 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3467 | |
| 3468 | // Remove all windows in secondary display and check that no event happens on window in |
| 3469 | // primary display. |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3470 | mDispatcher->setInputWindows( |
| 3471 | {{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, {SECOND_DISPLAY_ID, {}}}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3472 | windowDefaultDisplay->assertNoEvents(); |
| 3473 | |
| 3474 | // Move cursor position in window in default display and check that only hover move |
| 3475 | // event is generated and not hover enter event. |
| 3476 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowDefaultDisplay}}, |
| 3477 | {SECOND_DISPLAY_ID, {windowSecondDisplay}}}); |
| 3478 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3479 | injectMotionEvent(mDispatcher, |
| 3480 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3481 | AINPUT_SOURCE_MOUSE) |
| 3482 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3483 | .pointer(PointerBuilder(0, ToolType::MOUSE) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3484 | .x(400) |
| 3485 | .y(700)) |
| 3486 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3487 | windowDefaultDisplay->consumeMotionEvent( |
| 3488 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3489 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3490 | windowDefaultDisplay->assertNoEvents(); |
| 3491 | } |
| 3492 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3493 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3494 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3495 | |
| 3496 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3497 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3498 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3499 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3500 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3501 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3502 | |
| 3503 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3504 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3505 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3506 | |
| 3507 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3508 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3509 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3510 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3511 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3512 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3513 | windowRight->assertNoEvents(); |
| 3514 | } |
| 3515 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3516 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3517 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3518 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3519 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3520 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3521 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3522 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3523 | setFocusedWindow(window); |
| 3524 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3525 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3526 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3527 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3528 | |
| 3529 | // Window should receive key down event. |
| 3530 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3531 | |
| 3532 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3533 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3534 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 3535 | window->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3536 | AKEY_EVENT_FLAG_CANCELED); |
| 3537 | } |
| 3538 | |
| 3539 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3540 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3541 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3542 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3543 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 3544 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3545 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3546 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3547 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3548 | |
| 3549 | // Window should receive motion down event. |
| 3550 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3551 | |
| 3552 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 3553 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3554 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 3555 | window->consumeMotionEvent( |
| 3556 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3557 | } |
| 3558 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 3559 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 3560 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3561 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3562 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3563 | |
| 3564 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3565 | |
| 3566 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3567 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 3568 | .build()); |
| 3569 | |
| 3570 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3571 | |
| 3572 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 3573 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 3574 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3575 | |
| 3576 | // After the device has been reset, a new hovering stream can be sent to the window |
| 3577 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3578 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 3579 | .build()); |
| 3580 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3581 | } |
| 3582 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3583 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 3584 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3585 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3586 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3587 | window->setFocusable(true); |
| 3588 | |
| 3589 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3590 | setFocusedWindow(window); |
| 3591 | |
| 3592 | window->consumeFocusEvent(true); |
| 3593 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3594 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3595 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 3596 | const nsecs_t injectTime = keyArgs.eventTime; |
| 3597 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3598 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3599 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 3600 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3601 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 3602 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 3603 | } |
| 3604 | |
| 3605 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 3606 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3607 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3608 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3609 | window->setFocusable(true); |
| 3610 | |
| 3611 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3612 | setFocusedWindow(window); |
| 3613 | |
| 3614 | window->consumeFocusEvent(true); |
| 3615 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3616 | mFakePolicy->setInterceptKeyTimeout(150ms); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3617 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 3618 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 3619 | |
| 3620 | // Window should receive key event immediately when same key up. |
| 3621 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3622 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3623 | } |
| 3624 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3625 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3626 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 3627 | * WATCH_OUTSIDE_TOUCH. |
| 3628 | * Both windows are owned by the same UID. |
| 3629 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 3630 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 3631 | */ |
| 3632 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 3633 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3634 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3635 | "First Window", ADISPLAY_ID_DEFAULT); |
| 3636 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3637 | |
| 3638 | sp<FakeWindowHandle> outsideWindow = |
| 3639 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3640 | ADISPLAY_ID_DEFAULT); |
| 3641 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3642 | outsideWindow->setWatchOutsideTouch(true); |
| 3643 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
| 3644 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {outsideWindow, window}}}); |
| 3645 | |
| 3646 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3647 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3648 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3649 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3650 | window->consumeMotionDown(); |
| 3651 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 3652 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 3653 | outsideWindow->consumeMotionEvent( |
| 3654 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
| 3655 | } |
| 3656 | |
| 3657 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3658 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 3659 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 3660 | * ACTION_OUTSIDE event is sent per gesture. |
| 3661 | */ |
| 3662 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 3663 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 3664 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3665 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3666 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3667 | window->setWatchOutsideTouch(true); |
| 3668 | window->setFrame(Rect{0, 0, 100, 100}); |
| 3669 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3670 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3671 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3672 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 3673 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3674 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 3675 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3676 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
| 3677 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, secondWindow, thirdWindow}}}); |
| 3678 | |
| 3679 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3680 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3681 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3682 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3683 | window->assertNoEvents(); |
| 3684 | secondWindow->assertNoEvents(); |
| 3685 | |
| 3686 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 3687 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3688 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3689 | ADISPLAY_ID_DEFAULT, |
| 3690 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 3691 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 3692 | window->consumeMotionEvent( |
| 3693 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3694 | secondWindow->consumeMotionDown(); |
| 3695 | thirdWindow->assertNoEvents(); |
| 3696 | |
| 3697 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 3698 | // 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] | 3699 | mDispatcher->notifyMotion( |
| 3700 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3701 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3702 | window->assertNoEvents(); |
| 3703 | secondWindow->consumeMotionMove(); |
| 3704 | thirdWindow->consumeMotionDown(); |
| 3705 | } |
| 3706 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3707 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 3708 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3709 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3710 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3711 | window->setFocusable(true); |
| 3712 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3713 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3714 | setFocusedWindow(window); |
| 3715 | |
| 3716 | window->consumeFocusEvent(true); |
| 3717 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3718 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 3719 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 3720 | mDispatcher->notifyKey(keyDown); |
| 3721 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3722 | |
| 3723 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3724 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 3725 | |
| 3726 | // 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] | 3727 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3728 | |
| 3729 | window->consumeFocusEvent(false); |
| 3730 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3731 | mDispatcher->notifyKey(keyDown); |
| 3732 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 3733 | window->assertNoEvents(); |
| 3734 | } |
| 3735 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3736 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 3737 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3738 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3739 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 3740 | // Ensure window is non-split and have some transform. |
| 3741 | window->setPreventSplitting(true); |
| 3742 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3743 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3744 | |
| 3745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3746 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3747 | {50, 50})) |
| 3748 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3749 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3750 | |
| 3751 | const MotionEvent secondFingerDownEvent = |
| 3752 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3753 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3754 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3755 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 3756 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 3757 | .build(); |
| 3758 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3759 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 3760 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 3761 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 3762 | |
| 3763 | const MotionEvent* event = window->consumeMotion(); |
| 3764 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 3765 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 3766 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 3767 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 3768 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
| 3769 | } |
| 3770 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 3771 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 3772 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3773 | sp<FakeWindowHandle> window = |
| 3774 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3775 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3776 | sp<FakeWindowHandle> trustedOverlay = |
| 3777 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 3778 | ADISPLAY_ID_DEFAULT); |
| 3779 | trustedOverlay->setSpy(true); |
| 3780 | trustedOverlay->setTrustedOverlay(true); |
| 3781 | |
| 3782 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {trustedOverlay, window}}}); |
| 3783 | |
| 3784 | // Start a three-finger touchpad swipe |
| 3785 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3786 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3787 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3788 | .build()); |
| 3789 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 3790 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3791 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3792 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3793 | .build()); |
| 3794 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 3795 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3796 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3797 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 3798 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3799 | .build()); |
| 3800 | |
| 3801 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3802 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3803 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 3804 | |
| 3805 | // Move the swipe a bit |
| 3806 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3807 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3808 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3809 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3810 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3811 | .build()); |
| 3812 | |
| 3813 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3814 | |
| 3815 | // End the swipe |
| 3816 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 3817 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3818 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3819 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3820 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3821 | .build()); |
| 3822 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 3823 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3824 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3825 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3826 | .build()); |
| 3827 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3828 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3829 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3830 | .build()); |
| 3831 | |
| 3832 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 3833 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 3834 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3835 | |
| 3836 | window->assertNoEvents(); |
| 3837 | } |
| 3838 | |
| 3839 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 3840 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3841 | sp<FakeWindowHandle> window = |
| 3842 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3843 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3844 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 3845 | |
| 3846 | // Start a three-finger touchpad swipe |
| 3847 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3848 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3849 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3850 | .build()); |
| 3851 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 3852 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3853 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3854 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3855 | .build()); |
| 3856 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 3857 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 3858 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 3859 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 3860 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3861 | .build()); |
| 3862 | |
| 3863 | // Move the swipe a bit |
| 3864 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3865 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3866 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3867 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3868 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3869 | .build()); |
| 3870 | |
| 3871 | // End the swipe |
| 3872 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 3873 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3874 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3875 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 3876 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3877 | .build()); |
| 3878 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 3879 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3880 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 3881 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3882 | .build()); |
| 3883 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3884 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 3885 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 3886 | .build()); |
| 3887 | |
| 3888 | window->assertNoEvents(); |
| 3889 | } |
| 3890 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 3891 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3892 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 3893 | * |
| 3894 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 3895 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 3896 | * space. |
| 3897 | */ |
| 3898 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 3899 | public: |
| 3900 | void SetUp() override { |
| 3901 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3902 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 3906 | gui::DisplayInfo info; |
| 3907 | info.displayId = displayId; |
| 3908 | info.transform = transform; |
| 3909 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3910 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 3914 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 3915 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3918 | void removeAllWindowsAndDisplays() { |
| 3919 | mDisplayInfos.clear(); |
| 3920 | mWindowInfos.clear(); |
| 3921 | } |
| 3922 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3923 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 3924 | // on the display. |
| 3925 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 3926 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 3927 | // respectively. |
| 3928 | ui::Transform displayTransform; |
| 3929 | displayTransform.set(2, 0, 0, 4); |
| 3930 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 3931 | |
| 3932 | std::shared_ptr<FakeApplicationHandle> application = |
| 3933 | std::make_shared<FakeApplicationHandle>(); |
| 3934 | |
| 3935 | // Add two windows to the display. Their frames are represented in the display space. |
| 3936 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3937 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 3938 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3939 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 3940 | addWindow(firstWindow); |
| 3941 | |
| 3942 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3943 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 3944 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3945 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 3946 | addWindow(secondWindow); |
| 3947 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 3948 | } |
| 3949 | |
| 3950 | private: |
| 3951 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 3952 | std::vector<gui::WindowInfo> mWindowInfos; |
| 3953 | }; |
| 3954 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 3955 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3956 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3957 | // 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] | 3958 | // selected so that if the hit test was performed with the point and the bounds being in |
| 3959 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3960 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3961 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3962 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 3963 | |
| 3964 | firstWindow->consumeMotionDown(); |
| 3965 | secondWindow->assertNoEvents(); |
| 3966 | } |
| 3967 | |
| 3968 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 3969 | // the event should be treated as being in the logical display space. |
| 3970 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 3971 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3972 | // Send down to the first window. The point is represented in the logical display space. The |
| 3973 | // point is selected so that if the hit test was done in logical display space, then it would |
| 3974 | // end up in the incorrect window. |
| 3975 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3976 | PointF{75 * 2, 55 * 4}); |
| 3977 | |
| 3978 | firstWindow->consumeMotionDown(); |
| 3979 | secondWindow->assertNoEvents(); |
| 3980 | } |
| 3981 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3982 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 3983 | // event should be treated as being in the logical display space. |
| 3984 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 3985 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 3986 | |
| 3987 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 3988 | ui::Transform injectedEventTransform; |
| 3989 | injectedEventTransform.set(matrix); |
| 3990 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 3991 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 3992 | |
| 3993 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3994 | .displayId(ADISPLAY_ID_DEFAULT) |
| 3995 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3996 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 3997 | .x(untransformedPoint.x) |
| 3998 | .y(untransformedPoint.y)) |
| 3999 | .build(); |
| 4000 | event.transform(matrix); |
| 4001 | |
| 4002 | injectMotionEvent(mDispatcher, event, INJECT_EVENT_TIMEOUT, |
| 4003 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4004 | |
| 4005 | firstWindow->consumeMotionDown(); |
| 4006 | secondWindow->assertNoEvents(); |
| 4007 | } |
| 4008 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4009 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4010 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4011 | |
| 4012 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4013 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4014 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4015 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4016 | |
| 4017 | firstWindow->assertNoEvents(); |
| 4018 | const MotionEvent* event = secondWindow->consumeMotion(); |
Siarhei Vishniakou | b681c20 | 2023-05-01 11:22:33 -0700 | [diff] [blame] | 4019 | ASSERT_NE(nullptr, event); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4020 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
| 4021 | |
| 4022 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
| 4023 | EXPECT_EQ(300, event->getRawX(0)); |
| 4024 | EXPECT_EQ(880, event->getRawY(0)); |
| 4025 | |
| 4026 | // Ensure that the x and y values are in the window's coordinate space. |
| 4027 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4028 | // the logical display space. This will be the origin of the window space. |
| 4029 | EXPECT_EQ(100, event->getX(0)); |
| 4030 | EXPECT_EQ(80, event->getY(0)); |
| 4031 | } |
| 4032 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4033 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4034 | class InputDispatcherDisplayOrientationFixture |
| 4035 | : public InputDispatcherDisplayProjectionTest, |
| 4036 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 4037 | |
| 4038 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 4039 | // in different locations on the display, specifically points close to the four corners of a |
| 4040 | // window. |
| 4041 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 4042 | constexpr static int32_t displayWidth = 400; |
| 4043 | constexpr static int32_t displayHeight = 800; |
| 4044 | |
| 4045 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4046 | |
| 4047 | const auto rotation = GetParam(); |
| 4048 | |
| 4049 | // Set up the display with the specified rotation. |
| 4050 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4051 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4052 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4053 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4054 | logicalDisplayWidth, logicalDisplayHeight); |
| 4055 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4056 | |
| 4057 | // Create a window with its bounds determined in the logical display. |
| 4058 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 4059 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4060 | sp<FakeWindowHandle> window = |
| 4061 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4062 | window->setFrame(frameInDisplay, displayTransform); |
| 4063 | addWindow(window); |
| 4064 | |
| 4065 | // The following points in logical display space should be inside the window. |
| 4066 | static const std::array<vec2, 4> insidePoints{ |
| 4067 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4068 | for (const auto pointInsideWindow : insidePoints) { |
| 4069 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4070 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4071 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4072 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4073 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4074 | window->consumeMotionDown(); |
| 4075 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4076 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4077 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4078 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4079 | window->consumeMotionUp(); |
| 4080 | } |
| 4081 | |
| 4082 | // The following points in logical display space should be outside the window. |
| 4083 | static const std::array<vec2, 5> outsidePoints{ |
| 4084 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4085 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4086 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4087 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4088 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4089 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4090 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4091 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4092 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4093 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4094 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4095 | } |
| 4096 | window->assertNoEvents(); |
| 4097 | } |
| 4098 | |
| 4099 | // Run the precision tests for all rotations. |
| 4100 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4101 | InputDispatcherDisplayOrientationFixture, |
| 4102 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4103 | ui::ROTATION_270), |
| 4104 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4105 | return ftl::enum_string(testParamInfo.param); |
| 4106 | }); |
| 4107 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4108 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4109 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4110 | |
| 4111 | class TransferTouchFixture : public InputDispatcherTest, |
| 4112 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4113 | |
| 4114 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4115 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4116 | |
| 4117 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4118 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4119 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4120 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4121 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4122 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4123 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4124 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4125 | sp<FakeWindowHandle> wallpaper = |
| 4126 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 4127 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4128 | // Add the windows to the dispatcher |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4129 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow, wallpaper}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4130 | |
| 4131 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4132 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4133 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4134 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4135 | // Only the first window should get the down event |
| 4136 | firstWindow->consumeMotionDown(); |
| 4137 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4138 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4139 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4140 | // Transfer touch to the second window |
| 4141 | TransferFunction f = GetParam(); |
| 4142 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4143 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4144 | // The first window gets cancel and the second gets down |
| 4145 | firstWindow->consumeMotionCancel(); |
| 4146 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4147 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4148 | |
| 4149 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4150 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4151 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4152 | // The first window gets no events and the second gets up |
| 4153 | firstWindow->assertNoEvents(); |
| 4154 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4155 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4156 | } |
| 4157 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4158 | /** |
| 4159 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 4160 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 4161 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 4162 | * natural to the user. |
| 4163 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 4164 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 4165 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 4166 | * the first window to the second. |
| 4167 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 4168 | * the other API, as well. |
| 4169 | */ |
| 4170 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 4171 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4172 | |
| 4173 | // Create a couple of windows + a spy window |
| 4174 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4175 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4176 | spyWindow->setTrustedOverlay(true); |
| 4177 | spyWindow->setSpy(true); |
| 4178 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4179 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4180 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4181 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4182 | |
| 4183 | // Add the windows to the dispatcher |
| 4184 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyWindow, firstWindow, secondWindow}}}); |
| 4185 | |
| 4186 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4187 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4188 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4189 | // Only the first window and spy should get the down event |
| 4190 | spyWindow->consumeMotionDown(); |
| 4191 | firstWindow->consumeMotionDown(); |
| 4192 | |
| 4193 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 4194 | // if f === 'transferTouch'. |
| 4195 | TransferFunction f = GetParam(); |
| 4196 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4197 | ASSERT_TRUE(success); |
| 4198 | // The first window gets cancel and the second gets down |
| 4199 | firstWindow->consumeMotionCancel(); |
| 4200 | secondWindow->consumeMotionDown(); |
| 4201 | |
| 4202 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4203 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4204 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4205 | // The first window gets no events and the second+spy get up |
| 4206 | firstWindow->assertNoEvents(); |
| 4207 | spyWindow->consumeMotionUp(); |
| 4208 | secondWindow->consumeMotionUp(); |
| 4209 | } |
| 4210 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4211 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4212 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4213 | |
| 4214 | PointF touchPoint = {10, 10}; |
| 4215 | |
| 4216 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4217 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4218 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4219 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4220 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4221 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4222 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4223 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 4224 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4225 | |
| 4226 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4227 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4228 | |
| 4229 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4230 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4231 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4232 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4233 | // Only the first window should get the down event |
| 4234 | firstWindow->consumeMotionDown(); |
| 4235 | secondWindow->assertNoEvents(); |
| 4236 | |
| 4237 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4238 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4239 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4240 | // Only the first window should get the pointer down event |
| 4241 | firstWindow->consumeMotionPointerDown(1); |
| 4242 | secondWindow->assertNoEvents(); |
| 4243 | |
| 4244 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4245 | TransferFunction f = GetParam(); |
| 4246 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4247 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4248 | // The first window gets cancel and the second gets down and pointer down |
| 4249 | firstWindow->consumeMotionCancel(); |
| 4250 | secondWindow->consumeMotionDown(); |
| 4251 | secondWindow->consumeMotionPointerDown(1); |
| 4252 | |
| 4253 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4254 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4255 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4256 | // The first window gets nothing and the second gets pointer up |
| 4257 | firstWindow->assertNoEvents(); |
| 4258 | secondWindow->consumeMotionPointerUp(1); |
| 4259 | |
| 4260 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4261 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4262 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4263 | // The first window gets nothing and the second gets up |
| 4264 | firstWindow->assertNoEvents(); |
| 4265 | secondWindow->consumeMotionUp(); |
| 4266 | } |
| 4267 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4268 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 4269 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4270 | |
| 4271 | // Create a couple of windows |
| 4272 | sp<FakeWindowHandle> firstWindow = |
| 4273 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4274 | ADISPLAY_ID_DEFAULT); |
| 4275 | firstWindow->setDupTouchToWallpaper(true); |
| 4276 | sp<FakeWindowHandle> secondWindow = |
| 4277 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4278 | ADISPLAY_ID_DEFAULT); |
| 4279 | secondWindow->setDupTouchToWallpaper(true); |
| 4280 | |
| 4281 | sp<FakeWindowHandle> wallpaper1 = |
| 4282 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 4283 | wallpaper1->setIsWallpaper(true); |
| 4284 | |
| 4285 | sp<FakeWindowHandle> wallpaper2 = |
| 4286 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 4287 | wallpaper2->setIsWallpaper(true); |
| 4288 | // Add the windows to the dispatcher |
| 4289 | mDispatcher->setInputWindows( |
| 4290 | {{ADISPLAY_ID_DEFAULT, {firstWindow, wallpaper1, secondWindow, wallpaper2}}}); |
| 4291 | |
| 4292 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4293 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4294 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4295 | |
| 4296 | // Only the first window should get the down event |
| 4297 | firstWindow->consumeMotionDown(); |
| 4298 | secondWindow->assertNoEvents(); |
| 4299 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4300 | wallpaper2->assertNoEvents(); |
| 4301 | |
| 4302 | // Transfer touch focus to the second window |
| 4303 | TransferFunction f = GetParam(); |
| 4304 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4305 | ASSERT_TRUE(success); |
| 4306 | |
| 4307 | // The first window gets cancel and the second gets down |
| 4308 | firstWindow->consumeMotionCancel(); |
| 4309 | secondWindow->consumeMotionDown(); |
| 4310 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4311 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4312 | |
| 4313 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4314 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4315 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4316 | // The first window gets no events and the second gets up |
| 4317 | firstWindow->assertNoEvents(); |
| 4318 | secondWindow->consumeMotionUp(); |
| 4319 | wallpaper1->assertNoEvents(); |
| 4320 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 4321 | } |
| 4322 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4323 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 4324 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 4325 | // for the case where there are multiple pointers split across several windows. |
| 4326 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 4327 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4328 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4329 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4330 | return dispatcher->transferTouch(destChannelToken, |
| 4331 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4332 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4333 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4334 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4335 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4336 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4337 | })); |
| 4338 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4339 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4340 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4341 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4342 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4343 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4344 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4345 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4346 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4347 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4348 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4349 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4350 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4351 | |
| 4352 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4353 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4354 | |
| 4355 | PointF pointInFirst = {300, 200}; |
| 4356 | PointF pointInSecond = {300, 600}; |
| 4357 | |
| 4358 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4359 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4360 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4361 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4362 | // Only the first window should get the down event |
| 4363 | firstWindow->consumeMotionDown(); |
| 4364 | secondWindow->assertNoEvents(); |
| 4365 | |
| 4366 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4367 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4368 | ADISPLAY_ID_DEFAULT, |
| 4369 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4370 | // The first window gets a move and the second a down |
| 4371 | firstWindow->consumeMotionMove(); |
| 4372 | secondWindow->consumeMotionDown(); |
| 4373 | |
| 4374 | // Transfer touch focus to the second window |
| 4375 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 4376 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 4377 | firstWindow->consumeMotionCancel(); |
| 4378 | secondWindow->consumeMotionPointerDown(1); |
| 4379 | |
| 4380 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4381 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4382 | ADISPLAY_ID_DEFAULT, |
| 4383 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4384 | // The first window gets nothing and the second gets pointer up |
| 4385 | firstWindow->assertNoEvents(); |
| 4386 | secondWindow->consumeMotionPointerUp(1); |
| 4387 | |
| 4388 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4389 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4390 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4391 | // The first window gets nothing and the second gets up |
| 4392 | firstWindow->assertNoEvents(); |
| 4393 | secondWindow->consumeMotionUp(); |
| 4394 | } |
| 4395 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4396 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 4397 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 4398 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 4399 | // window should get nothing. |
| 4400 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 4401 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4402 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4403 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4404 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4405 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4406 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4407 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4408 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4409 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4410 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4411 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4412 | |
| 4413 | // Add the windows to the dispatcher |
| 4414 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4415 | |
| 4416 | PointF pointInFirst = {300, 200}; |
| 4417 | PointF pointInSecond = {300, 600}; |
| 4418 | |
| 4419 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4420 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4421 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4422 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4423 | // Only the first window should get the down event |
| 4424 | firstWindow->consumeMotionDown(); |
| 4425 | secondWindow->assertNoEvents(); |
| 4426 | |
| 4427 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4428 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4429 | ADISPLAY_ID_DEFAULT, |
| 4430 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4431 | // The first window gets a move and the second a down |
| 4432 | firstWindow->consumeMotionMove(); |
| 4433 | secondWindow->consumeMotionDown(); |
| 4434 | |
| 4435 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4436 | const bool transferred = |
| 4437 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4438 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 4439 | ASSERT_FALSE(transferred); |
| 4440 | firstWindow->assertNoEvents(); |
| 4441 | secondWindow->assertNoEvents(); |
| 4442 | |
| 4443 | // The rest of the dispatch should proceed as normal |
| 4444 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4445 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4446 | ADISPLAY_ID_DEFAULT, |
| 4447 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4448 | // The first window gets MOVE and the second gets pointer up |
| 4449 | firstWindow->consumeMotionMove(); |
| 4450 | secondWindow->consumeMotionUp(); |
| 4451 | |
| 4452 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4453 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4454 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4455 | // The first window gets nothing and the second gets up |
| 4456 | firstWindow->consumeMotionUp(); |
| 4457 | secondWindow->assertNoEvents(); |
| 4458 | } |
| 4459 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4460 | // This case will create two windows and one mirrored window on the default display and mirror |
| 4461 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 4462 | // the windows info of second display before default display. |
| 4463 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 4464 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4465 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4466 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4467 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4468 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4469 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4470 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4471 | |
| 4472 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4473 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4474 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4475 | |
| 4476 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4477 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4478 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4479 | |
| 4480 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4481 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4482 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4483 | |
| 4484 | // Update window info, let it find window handle of second display first. |
| 4485 | mDispatcher->setInputWindows( |
| 4486 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4487 | {ADISPLAY_ID_DEFAULT, |
| 4488 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4489 | |
| 4490 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4491 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4492 | {50, 50})) |
| 4493 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4494 | |
| 4495 | // Window should receive motion event. |
| 4496 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4497 | |
| 4498 | // Transfer touch focus |
| 4499 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 4500 | secondWindowInPrimary->getToken())); |
| 4501 | // The first window gets cancel. |
| 4502 | firstWindowInPrimary->consumeMotionCancel(); |
| 4503 | secondWindowInPrimary->consumeMotionDown(); |
| 4504 | |
| 4505 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4506 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4507 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 4508 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4509 | firstWindowInPrimary->assertNoEvents(); |
| 4510 | secondWindowInPrimary->consumeMotionMove(); |
| 4511 | |
| 4512 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4513 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4514 | {150, 50})) |
| 4515 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4516 | firstWindowInPrimary->assertNoEvents(); |
| 4517 | secondWindowInPrimary->consumeMotionUp(); |
| 4518 | } |
| 4519 | |
| 4520 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 4521 | // 'transferTouch' api. |
| 4522 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 4523 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4524 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4525 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4526 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4527 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4528 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4529 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4530 | |
| 4531 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 4532 | firstWindowInPrimary->clone(application, mDispatcher, ADISPLAY_ID_DEFAULT); |
| 4533 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4534 | |
| 4535 | sp<FakeWindowHandle> firstWindowInSecondary = |
| 4536 | firstWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4537 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4538 | |
| 4539 | sp<FakeWindowHandle> secondWindowInSecondary = |
| 4540 | secondWindowInPrimary->clone(application, mDispatcher, SECOND_DISPLAY_ID); |
| 4541 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4542 | |
| 4543 | // Update window info, let it find window handle of second display first. |
| 4544 | mDispatcher->setInputWindows( |
| 4545 | {{SECOND_DISPLAY_ID, {firstWindowInSecondary, secondWindowInSecondary}}, |
| 4546 | {ADISPLAY_ID_DEFAULT, |
| 4547 | {mirrorWindowInPrimary, firstWindowInPrimary, secondWindowInPrimary}}}); |
| 4548 | |
| 4549 | // Touch on second display. |
| 4550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4551 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {50, 50})) |
| 4552 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4553 | |
| 4554 | // Window should receive motion event. |
| 4555 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4556 | |
| 4557 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 4558 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 4559 | |
| 4560 | // The first window gets cancel. |
| 4561 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 4562 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 4563 | |
| 4564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4565 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4566 | SECOND_DISPLAY_ID, {150, 50})) |
| 4567 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4568 | firstWindowInPrimary->assertNoEvents(); |
| 4569 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 4570 | |
| 4571 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4572 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
| 4573 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4574 | firstWindowInPrimary->assertNoEvents(); |
| 4575 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 4576 | } |
| 4577 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4578 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4579 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4580 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4581 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4582 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4583 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4584 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4585 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4586 | |
| 4587 | window->consumeFocusEvent(true); |
| 4588 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4589 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4590 | |
| 4591 | // Window should receive key down event. |
| 4592 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4593 | |
| 4594 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame^] | 4595 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4596 | mFakePolicy->assertUserActivityPoked(); |
| 4597 | } |
| 4598 | |
| 4599 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 4600 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4601 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4602 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4603 | |
| 4604 | window->setDisableUserActivity(true); |
| 4605 | window->setFocusable(true); |
| 4606 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4607 | setFocusedWindow(window); |
| 4608 | |
| 4609 | window->consumeFocusEvent(true); |
| 4610 | |
| 4611 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4612 | |
| 4613 | // Window should receive key down event. |
| 4614 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4615 | |
| 4616 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame^] | 4617 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 4618 | mFakePolicy->assertUserActivityNotPoked(); |
| 4619 | } |
| 4620 | |
| 4621 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 4622 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4623 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4624 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4625 | |
| 4626 | window->setFocusable(true); |
| 4627 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4628 | setFocusedWindow(window); |
| 4629 | |
| 4630 | window->consumeFocusEvent(true); |
| 4631 | |
| 4632 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4633 | mDispatcher->waitForIdle(); |
| 4634 | |
| 4635 | // System key is not passed down |
| 4636 | window->assertNoEvents(); |
| 4637 | |
| 4638 | // Should have poked user activity |
| 4639 | mFakePolicy->assertUserActivityPoked(); |
| 4640 | } |
| 4641 | |
| 4642 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 4643 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4644 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4645 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4646 | |
| 4647 | window->setFocusable(true); |
| 4648 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4649 | setFocusedWindow(window); |
| 4650 | |
| 4651 | window->consumeFocusEvent(true); |
| 4652 | |
| 4653 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4654 | mDispatcher->waitForIdle(); |
| 4655 | |
| 4656 | // System key is not passed down |
| 4657 | window->assertNoEvents(); |
| 4658 | |
| 4659 | // Should have poked user activity |
| 4660 | mFakePolicy->assertUserActivityPoked(); |
| 4661 | } |
| 4662 | |
| 4663 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 4664 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4665 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4666 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4667 | |
| 4668 | window->setDisableUserActivity(true); |
| 4669 | window->setFocusable(true); |
| 4670 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4671 | setFocusedWindow(window); |
| 4672 | |
| 4673 | window->consumeFocusEvent(true); |
| 4674 | |
| 4675 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4676 | mDispatcher->waitForIdle(); |
| 4677 | |
| 4678 | // System key is not passed down |
| 4679 | window->assertNoEvents(); |
| 4680 | |
| 4681 | // Should have poked user activity |
| 4682 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4683 | } |
| 4684 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4685 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 4686 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4687 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4688 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4689 | |
| 4690 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4691 | |
| 4692 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4693 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4694 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 4695 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4696 | |
| 4697 | window->consumeMotionEvent( |
| 4698 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4699 | |
| 4700 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame^] | 4701 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4702 | mFakePolicy->assertUserActivityPoked(); |
| 4703 | } |
| 4704 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4705 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4706 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4707 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4708 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4709 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4710 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4711 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4712 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4713 | mDispatcher->waitForIdle(); |
| 4714 | |
| 4715 | window->assertNoEvents(); |
| 4716 | } |
| 4717 | |
| 4718 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4719 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4720 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4721 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4722 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4723 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4724 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4725 | |
| 4726 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4727 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4728 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4729 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4730 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4731 | |
| 4732 | // Window should receive only the motion event |
| 4733 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4734 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4735 | } |
| 4736 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4737 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4738 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4739 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4740 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4741 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4742 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4743 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4744 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4745 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4746 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4747 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4748 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4749 | |
| 4750 | // Add the windows to the dispatcher |
| 4751 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4752 | |
| 4753 | PointF pointInFirst = {300, 200}; |
| 4754 | PointF pointInSecond = {300, 600}; |
| 4755 | |
| 4756 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4757 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4758 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4759 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4760 | // Only the first window should get the down event |
| 4761 | firstWindow->consumeMotionDown(); |
| 4762 | secondWindow->assertNoEvents(); |
| 4763 | |
| 4764 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4765 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4766 | ADISPLAY_ID_DEFAULT, |
| 4767 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4768 | // The first window gets a move and the second a down |
| 4769 | firstWindow->consumeMotionMove(); |
| 4770 | secondWindow->consumeMotionDown(); |
| 4771 | |
| 4772 | // Send pointer cancel to the second window |
| 4773 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4774 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4775 | {pointInFirst, pointInSecond}); |
| 4776 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4777 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4778 | // The first window gets move and the second gets cancel. |
| 4779 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4780 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4781 | |
| 4782 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4783 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4784 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4785 | // The first window gets up and the second gets nothing. |
| 4786 | firstWindow->consumeMotionUp(); |
| 4787 | secondWindow->assertNoEvents(); |
| 4788 | } |
| 4789 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4790 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 4791 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4792 | |
| 4793 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4794 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4795 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4796 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 4797 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 4798 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 4799 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4800 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4801 | window->assertNoEvents(); |
| 4802 | mDispatcher->waitForIdle(); |
| 4803 | } |
| 4804 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4805 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4806 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4807 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4808 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4809 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4810 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4811 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4814 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 4815 | |
| 4816 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4817 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 4818 | expectedFlags); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4819 | } |
| 4820 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4821 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 4822 | |
| 4823 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 4824 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4825 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4826 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4827 | expectedDisplayId, expectedFlags); |
| 4828 | } |
| 4829 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4830 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4831 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4832 | expectedDisplayId, expectedFlags); |
| 4833 | } |
| 4834 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4835 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4836 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4837 | expectedDisplayId, expectedFlags); |
| 4838 | } |
| 4839 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4840 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4841 | mInputReceiver->consumeMotionEvent( |
| 4842 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4843 | WithDisplayId(expectedDisplayId), |
| 4844 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4847 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 4848 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 4849 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4850 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4851 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4854 | MotionEvent* consumeMotion() { |
| 4855 | InputEvent* event = mInputReceiver->consume(); |
| 4856 | if (!event) { |
| 4857 | ADD_FAILURE() << "No event was produced"; |
| 4858 | return nullptr; |
| 4859 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4860 | if (event->getType() != InputEventType::MOTION) { |
| 4861 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4862 | return nullptr; |
| 4863 | } |
| 4864 | return static_cast<MotionEvent*>(event); |
| 4865 | } |
| 4866 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4867 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 4868 | |
| 4869 | private: |
| 4870 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4871 | }; |
| 4872 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4873 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 4874 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4875 | /** |
| 4876 | * Two entities that receive touch: A window, and a global monitor. |
| 4877 | * The touch goes to the window, and then the window disappears. |
| 4878 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 4879 | * for the monitor, as well. |
| 4880 | * 1. foregroundWindow |
| 4881 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 4882 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4883 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4884 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4885 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4886 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4887 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4888 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4889 | |
| 4890 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4892 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4893 | {100, 200})) |
| 4894 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4895 | |
| 4896 | // Both the foreground window and the global monitor should receive the touch down |
| 4897 | window->consumeMotionDown(); |
| 4898 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4899 | |
| 4900 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4901 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4902 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4903 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4904 | |
| 4905 | window->consumeMotionMove(); |
| 4906 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4907 | |
| 4908 | // Now the foreground window goes away |
| 4909 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4910 | window->consumeMotionCancel(); |
| 4911 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 4912 | |
| 4913 | // If more events come in, there will be no more foreground window to send them to. This will |
| 4914 | // cause a cancel for the monitor, as well. |
| 4915 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4916 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4917 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 4918 | << "Injection should fail because the window was removed"; |
| 4919 | window->assertNoEvents(); |
| 4920 | // Global monitor now gets the cancel |
| 4921 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4922 | } |
| 4923 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4924 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4925 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4926 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4927 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4928 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4929 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4930 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4931 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4932 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4933 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4934 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4935 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4936 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4939 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 4940 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4941 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4942 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4943 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4944 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4945 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4946 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4947 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4948 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4949 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4950 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4951 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4952 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4953 | // Pilfer pointers from the monitor. |
| 4954 | // This should not do anything and the window should continue to receive events. |
| 4955 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4956 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4957 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4958 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4959 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4960 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4961 | |
| 4962 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4963 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4964 | } |
| 4965 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4966 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4967 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4968 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4969 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4970 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4971 | window->setWindowOffset(20, 40); |
| 4972 | window->setWindowTransform(0, 1, -1, 0); |
| 4973 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4974 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4975 | |
| 4976 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4977 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4978 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4979 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4980 | MotionEvent* event = monitor.consumeMotion(); |
| 4981 | // Even though window has transform, gesture monitor must not. |
| 4982 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 4983 | } |
| 4984 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4985 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4986 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4987 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4988 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4989 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4990 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4991 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 4992 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4995 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4996 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4997 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4998 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4999 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5000 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5001 | |
| 5002 | NotifyMotionArgs motionArgs = |
| 5003 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5004 | ADISPLAY_ID_DEFAULT); |
| 5005 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5006 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5007 | // Window should receive motion down event. |
| 5008 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5009 | |
| 5010 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5011 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5012 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5013 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5014 | motionArgs.pointerCoords[0].getX() - 10); |
| 5015 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5016 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5017 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5018 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5019 | } |
| 5020 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5021 | /** |
| 5022 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 5023 | * the device default right away. In the test scenario, we check both the default value, |
| 5024 | * and the action of enabling / disabling. |
| 5025 | */ |
| 5026 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5027 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5028 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5029 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5030 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5031 | |
| 5032 | // Set focused application. |
| 5033 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5034 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5035 | |
| 5036 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5037 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5038 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5039 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5040 | |
| 5041 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5042 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5043 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5044 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5045 | |
| 5046 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5047 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5048 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5049 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5050 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5051 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5052 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5053 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5054 | |
| 5055 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5056 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5057 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5058 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5059 | |
| 5060 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5061 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5062 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5063 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5064 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5065 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5066 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5067 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5068 | |
| 5069 | window->assertNoEvents(); |
| 5070 | } |
| 5071 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5072 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5073 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5074 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5075 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5076 | |
| 5077 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5078 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5079 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5080 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5081 | setFocusedWindow(window); |
| 5082 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5083 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5084 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5085 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5086 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5087 | |
| 5088 | InputEvent* event = window->consume(); |
| 5089 | ASSERT_NE(event, nullptr); |
| 5090 | |
| 5091 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5092 | ASSERT_NE(verified, nullptr); |
| 5093 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 5094 | |
| 5095 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 5096 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 5097 | ASSERT_EQ(keyArgs.source, verified->source); |
| 5098 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 5099 | |
| 5100 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 5101 | |
| 5102 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5103 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5104 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5105 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 5106 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 5107 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 5108 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 5109 | } |
| 5110 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5111 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5112 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5113 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5114 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5115 | |
| 5116 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5117 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5118 | ui::Transform transform; |
| 5119 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5120 | |
| 5121 | gui::DisplayInfo displayInfo; |
| 5122 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5123 | displayInfo.transform = transform; |
| 5124 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5125 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5126 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5127 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5128 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5129 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5130 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5131 | |
| 5132 | InputEvent* event = window->consume(); |
| 5133 | ASSERT_NE(event, nullptr); |
| 5134 | |
| 5135 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5136 | ASSERT_NE(verified, nullptr); |
| 5137 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 5138 | |
| 5139 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 5140 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 5141 | EXPECT_EQ(motionArgs.source, verified->source); |
| 5142 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 5143 | |
| 5144 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 5145 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5146 | const vec2 rawXY = |
| 5147 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 5148 | motionArgs.pointerCoords[0].getXYValue()); |
| 5149 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 5150 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5151 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5152 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5153 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5154 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 5155 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 5156 | } |
| 5157 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5158 | /** |
| 5159 | * Ensure that separate calls to sign the same data are generating the same key. |
| 5160 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 5161 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 5162 | * tests. |
| 5163 | */ |
| 5164 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 5165 | KeyEvent event = getTestKeyEvent(); |
| 5166 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5167 | |
| 5168 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 5169 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5170 | ASSERT_EQ(hmac1, hmac2); |
| 5171 | } |
| 5172 | |
| 5173 | /** |
| 5174 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 5175 | */ |
| 5176 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 5177 | KeyEvent event = getTestKeyEvent(); |
| 5178 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5179 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 5180 | |
| 5181 | verifiedEvent.deviceId += 1; |
| 5182 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5183 | |
| 5184 | verifiedEvent.source += 1; |
| 5185 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5186 | |
| 5187 | verifiedEvent.eventTimeNanos += 1; |
| 5188 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5189 | |
| 5190 | verifiedEvent.displayId += 1; |
| 5191 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5192 | |
| 5193 | verifiedEvent.action += 1; |
| 5194 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5195 | |
| 5196 | verifiedEvent.downTimeNanos += 1; |
| 5197 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5198 | |
| 5199 | verifiedEvent.flags += 1; |
| 5200 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5201 | |
| 5202 | verifiedEvent.keyCode += 1; |
| 5203 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5204 | |
| 5205 | verifiedEvent.scanCode += 1; |
| 5206 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5207 | |
| 5208 | verifiedEvent.metaState += 1; |
| 5209 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5210 | |
| 5211 | verifiedEvent.repeatCount += 1; |
| 5212 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5213 | } |
| 5214 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5215 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 5216 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5217 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5218 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5219 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5220 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5221 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5222 | |
| 5223 | // Top window is also focusable but is not granted focus. |
| 5224 | windowTop->setFocusable(true); |
| 5225 | windowSecond->setFocusable(true); |
| 5226 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5227 | setFocusedWindow(windowSecond); |
| 5228 | |
| 5229 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5230 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5231 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5232 | |
| 5233 | // Focused window should receive event. |
| 5234 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5235 | windowTop->assertNoEvents(); |
| 5236 | } |
| 5237 | |
| 5238 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5239 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5240 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5241 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5242 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5243 | |
| 5244 | window->setFocusable(true); |
| 5245 | // Release channel for window is no longer valid. |
| 5246 | window->releaseChannel(); |
| 5247 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5248 | setFocusedWindow(window); |
| 5249 | |
| 5250 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5251 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5252 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5253 | |
| 5254 | // window channel is invalid, so it should not receive any input event. |
| 5255 | window->assertNoEvents(); |
| 5256 | } |
| 5257 | |
| 5258 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5259 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5260 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5261 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5262 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5263 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5264 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5265 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5266 | setFocusedWindow(window); |
| 5267 | |
| 5268 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5269 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5270 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5271 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5272 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5273 | window->assertNoEvents(); |
| 5274 | } |
| 5275 | |
| 5276 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5277 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5278 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5279 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5280 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5281 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5282 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5283 | |
| 5284 | windowTop->setFocusable(true); |
| 5285 | windowSecond->setFocusable(true); |
| 5286 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5287 | setFocusedWindow(windowTop); |
| 5288 | windowTop->consumeFocusEvent(true); |
| 5289 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5290 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
| 5291 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5292 | windowSecond->consumeFocusEvent(true); |
| 5293 | windowTop->consumeFocusEvent(false); |
| 5294 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5296 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5297 | |
| 5298 | // Focused window should receive event. |
| 5299 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5300 | } |
| 5301 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5302 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5303 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5304 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5305 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5306 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5307 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5308 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5309 | |
| 5310 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5311 | windowSecond->setFocusable(false); |
| 5312 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5313 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5314 | setFocusedWindow(windowTop); |
| 5315 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5316 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5317 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5318 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5319 | |
| 5320 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5321 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5322 | windowSecond->assertNoEvents(); |
| 5323 | } |
| 5324 | |
| 5325 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5326 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5327 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5328 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5329 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5330 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5331 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5332 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5333 | |
| 5334 | window->setFocusable(true); |
| 5335 | previousFocusedWindow->setFocusable(true); |
| 5336 | window->setVisible(false); |
| 5337 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5338 | setFocusedWindow(previousFocusedWindow); |
| 5339 | previousFocusedWindow->consumeFocusEvent(true); |
| 5340 | |
| 5341 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5342 | setFocusedWindow(window); |
| 5343 | previousFocusedWindow->consumeFocusEvent(false); |
| 5344 | |
| 5345 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5346 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5347 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5348 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5349 | |
| 5350 | // Window does not get focus event or key down. |
| 5351 | window->assertNoEvents(); |
| 5352 | |
| 5353 | // Window becomes visible. |
| 5354 | window->setVisible(true); |
| 5355 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5356 | |
| 5357 | // Window receives focus event. |
| 5358 | window->consumeFocusEvent(true); |
| 5359 | // Focused window receives key down. |
| 5360 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5361 | } |
| 5362 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5363 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5364 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5365 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5366 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5367 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5368 | |
| 5369 | // window is granted focus. |
| 5370 | window->setFocusable(true); |
| 5371 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5372 | setFocusedWindow(window); |
| 5373 | window->consumeFocusEvent(true); |
| 5374 | |
| 5375 | // When a display is removed window loses focus. |
| 5376 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5377 | window->consumeFocusEvent(false); |
| 5378 | } |
| 5379 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5380 | /** |
| 5381 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5382 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5383 | * of the 'slipperyEnterWindow'. |
| 5384 | * |
| 5385 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5386 | * a way so that the touched location is no longer covered by the top window. |
| 5387 | * |
| 5388 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5389 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5390 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5391 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5392 | * with ACTION_DOWN). |
| 5393 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5394 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5395 | * |
| 5396 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5397 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5398 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5399 | * |
| 5400 | * In this test, we ensure that the event received by the bottom window has |
| 5401 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5402 | */ |
| 5403 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5404 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 5405 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5406 | |
| 5407 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5408 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5409 | |
| 5410 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5411 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5412 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5413 | // Make sure this one overlaps the bottom window |
| 5414 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5415 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5416 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5417 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5418 | |
| 5419 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5420 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5421 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5422 | |
| 5423 | mDispatcher->setInputWindows( |
| 5424 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5425 | |
| 5426 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5427 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5428 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5429 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5430 | slipperyExitWindow->consumeMotionDown(); |
| 5431 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5432 | mDispatcher->setInputWindows( |
| 5433 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5434 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5435 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 5436 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5437 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5438 | |
| 5439 | slipperyExitWindow->consumeMotionCancel(); |
| 5440 | |
| 5441 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5442 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5443 | } |
| 5444 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 5445 | /** |
| 5446 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 5447 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 5448 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 5449 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 5450 | * reproduces a crash. |
| 5451 | */ |
| 5452 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 5453 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5454 | |
| 5455 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 5456 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5457 | leftSlipperyWindow->setSlippery(true); |
| 5458 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5459 | |
| 5460 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 5461 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5462 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 5463 | rightDropTouchesWindow->setDropInput(true); |
| 5464 | |
| 5465 | mDispatcher->setInputWindows( |
| 5466 | {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}}); |
| 5467 | |
| 5468 | // Start touch in the left window |
| 5469 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5470 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5471 | .build()); |
| 5472 | leftSlipperyWindow->consumeMotionDown(); |
| 5473 | |
| 5474 | // And move it into the right window |
| 5475 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5476 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5477 | .build()); |
| 5478 | |
| 5479 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 5480 | // The left window continues to receive the gesture. |
| 5481 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5482 | rightDropTouchesWindow->assertNoEvents(); |
| 5483 | } |
| 5484 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5485 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5486 | protected: |
| 5487 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5488 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5489 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5490 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5491 | sp<FakeWindowHandle> mWindow; |
| 5492 | |
| 5493 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5494 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5495 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5496 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 5497 | mDispatcher->requestRefreshConfiguration(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5498 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 5499 | ASSERT_EQ(OK, mDispatcher->start()); |
| 5500 | |
| 5501 | setUpWindow(); |
| 5502 | } |
| 5503 | |
| 5504 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5505 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5506 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5507 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5508 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5509 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5510 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5511 | mWindow->consumeFocusEvent(true); |
| 5512 | } |
| 5513 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5514 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5515 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5516 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5517 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5518 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5519 | |
| 5520 | // Window should receive key down event. |
| 5521 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5522 | } |
| 5523 | |
| 5524 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 5525 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 5526 | InputEvent* repeatEvent = mWindow->consume(); |
| 5527 | ASSERT_NE(nullptr, repeatEvent); |
| 5528 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5529 | ASSERT_EQ(InputEventType::KEY, repeatEvent->getType()); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5530 | |
| 5531 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 5532 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 5533 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 5534 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 5535 | } |
| 5536 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5537 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5538 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5539 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5540 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5541 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5542 | |
| 5543 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5544 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5545 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5546 | } |
| 5547 | }; |
| 5548 | |
| 5549 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5550 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5551 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5552 | expectKeyRepeatOnce(repeatCount); |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5557 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5558 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5559 | expectKeyRepeatOnce(repeatCount); |
| 5560 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5561 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5562 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5563 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5564 | expectKeyRepeatOnce(repeatCount); |
| 5565 | } |
| 5566 | } |
| 5567 | |
| 5568 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5569 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5570 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5571 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5572 | mWindow->assertNoEvents(); |
| 5573 | } |
| 5574 | |
| 5575 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5576 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5577 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5578 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5579 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5580 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5581 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5582 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5583 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 5584 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5585 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5586 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5587 | mWindow->assertNoEvents(); |
| 5588 | } |
| 5589 | |
| 5590 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5591 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5592 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5593 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5594 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5595 | // 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] | 5596 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5597 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5598 | mWindow->assertNoEvents(); |
| 5599 | } |
| 5600 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5601 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 5602 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5603 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5604 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5605 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 5606 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 5607 | mWindow->assertNoEvents(); |
| 5608 | } |
| 5609 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5610 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5611 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5612 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5613 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5614 | InputEvent* repeatEvent = mWindow->consume(); |
| 5615 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5616 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 5617 | IdGenerator::getSource(repeatEvent->getId())); |
| 5618 | } |
| 5619 | } |
| 5620 | |
| 5621 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5622 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5623 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5624 | |
| 5625 | std::unordered_set<int32_t> idSet; |
| 5626 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5627 | InputEvent* repeatEvent = mWindow->consume(); |
| 5628 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5629 | int32_t id = repeatEvent->getId(); |
| 5630 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 5631 | idSet.insert(id); |
| 5632 | } |
| 5633 | } |
| 5634 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5635 | /* Test InputDispatcher for MultiDisplay */ |
| 5636 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 5637 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5638 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5639 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5640 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5641 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5642 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5643 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5644 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5645 | // Set focus window for primary display, but focused display would be second one. |
| 5646 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5647 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5648 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5649 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5650 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5651 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5652 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5653 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5654 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5655 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5656 | // Set focus display to second one. |
| 5657 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 5658 | // Set focus window for second display. |
| 5659 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5660 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5661 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5662 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5663 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5664 | } |
| 5665 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5666 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5667 | InputDispatcherTest::TearDown(); |
| 5668 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5669 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5670 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5671 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5672 | windowInSecondary.clear(); |
| 5673 | } |
| 5674 | |
| 5675 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5676 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5677 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5678 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5679 | sp<FakeWindowHandle> windowInSecondary; |
| 5680 | }; |
| 5681 | |
| 5682 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 5683 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5685 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5686 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5687 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5688 | windowInSecondary->assertNoEvents(); |
| 5689 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5690 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5691 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5692 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5693 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5694 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5695 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5696 | } |
| 5697 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5698 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5699 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5700 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5701 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5702 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5703 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5704 | windowInSecondary->assertNoEvents(); |
| 5705 | |
| 5706 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5708 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5709 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5710 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5711 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5712 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5713 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5714 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5715 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5716 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5717 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5718 | |
| 5719 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5720 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5721 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5722 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5723 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5724 | windowInSecondary->assertNoEvents(); |
| 5725 | } |
| 5726 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5727 | // Test per-display input monitors for motion event. |
| 5728 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5729 | FakeMonitorReceiver monitorInPrimary = |
| 5730 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5731 | FakeMonitorReceiver monitorInSecondary = |
| 5732 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5733 | |
| 5734 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5735 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5736 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5737 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5738 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5739 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5740 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5741 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5742 | |
| 5743 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5744 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5745 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5746 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5747 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5748 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5749 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5750 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5751 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 5752 | // Lift up the touch from the second display |
| 5753 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5754 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5755 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5756 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5757 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 5758 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5759 | // Test inject a non-pointer motion event. |
| 5760 | // If specific a display, it will dispatch to the focused window of particular display, |
| 5761 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5762 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5763 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 5764 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5765 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5766 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5767 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5768 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | // Test per-display input monitors for key event. |
| 5772 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5773 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5774 | FakeMonitorReceiver monitorInPrimary = |
| 5775 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5776 | FakeMonitorReceiver monitorInSecondary = |
| 5777 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5778 | |
| 5779 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5780 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5781 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5782 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5783 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5784 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5785 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5786 | } |
| 5787 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5788 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 5789 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5790 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5791 | secondWindowInPrimary->setFocusable(true); |
| 5792 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 5793 | setFocusedWindow(secondWindowInPrimary); |
| 5794 | windowInPrimary->consumeFocusEvent(false); |
| 5795 | secondWindowInPrimary->consumeFocusEvent(true); |
| 5796 | |
| 5797 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5798 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 5799 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5800 | windowInPrimary->assertNoEvents(); |
| 5801 | windowInSecondary->assertNoEvents(); |
| 5802 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5803 | } |
| 5804 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 5805 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 5806 | FakeMonitorReceiver monitorInPrimary = |
| 5807 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5808 | FakeMonitorReceiver monitorInSecondary = |
| 5809 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 5810 | |
| 5811 | // Test touch down on primary display. |
| 5812 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5813 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5814 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5815 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5816 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5817 | |
| 5818 | // Test touch down on second display. |
| 5819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5820 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5821 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5822 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5823 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 5824 | |
| 5825 | // Trigger cancel touch. |
| 5826 | mDispatcher->cancelCurrentTouch(); |
| 5827 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5828 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5829 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5830 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5831 | |
| 5832 | // Test inject a move motion event, no window/monitor should receive the event. |
| 5833 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5834 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5835 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5836 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5837 | windowInPrimary->assertNoEvents(); |
| 5838 | monitorInPrimary.assertNoEvents(); |
| 5839 | |
| 5840 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5841 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5842 | SECOND_DISPLAY_ID, {110, 200})) |
| 5843 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5844 | windowInSecondary->assertNoEvents(); |
| 5845 | monitorInSecondary.assertNoEvents(); |
| 5846 | } |
| 5847 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5848 | class InputFilterTest : public InputDispatcherTest { |
| 5849 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5850 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 5851 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5852 | NotifyMotionArgs motionArgs; |
| 5853 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5854 | motionArgs = |
| 5855 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5856 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5857 | motionArgs = |
| 5858 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5859 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5860 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5861 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5862 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 5863 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5864 | } else { |
| 5865 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5866 | } |
| 5867 | } |
| 5868 | |
| 5869 | void testNotifyKey(bool expectToBeFiltered) { |
| 5870 | NotifyKeyArgs keyArgs; |
| 5871 | |
| 5872 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5873 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5874 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5875 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5876 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5877 | |
| 5878 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 5879 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5880 | } else { |
| 5881 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5882 | } |
| 5883 | } |
| 5884 | }; |
| 5885 | |
| 5886 | // Test InputFilter for MotionEvent |
| 5887 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 5888 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 5889 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5890 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5891 | |
| 5892 | // Enable InputFilter |
| 5893 | mDispatcher->setInputFilterEnabled(true); |
| 5894 | // Test touch on both primary and second display, and check if both events are filtered. |
| 5895 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 5896 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 5897 | |
| 5898 | // Disable InputFilter |
| 5899 | mDispatcher->setInputFilterEnabled(false); |
| 5900 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 5901 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5902 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5903 | } |
| 5904 | |
| 5905 | // Test InputFilter for KeyEvent |
| 5906 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 5907 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 5908 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5909 | |
| 5910 | // Enable InputFilter |
| 5911 | mDispatcher->setInputFilterEnabled(true); |
| 5912 | // Send a key event, and check if it is filtered. |
| 5913 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 5914 | |
| 5915 | // Disable InputFilter |
| 5916 | mDispatcher->setInputFilterEnabled(false); |
| 5917 | // Send a key event, and check if it isn't filtered. |
| 5918 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5919 | } |
| 5920 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5921 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 5922 | // logical display coordinate space. |
| 5923 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 5924 | ui::Transform firstDisplayTransform; |
| 5925 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5926 | ui::Transform secondDisplayTransform; |
| 5927 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 5928 | |
| 5929 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 5930 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 5931 | displayInfos[0].transform = firstDisplayTransform; |
| 5932 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 5933 | displayInfos[1].transform = secondDisplayTransform; |
| 5934 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5935 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5936 | |
| 5937 | // Enable InputFilter |
| 5938 | mDispatcher->setInputFilterEnabled(true); |
| 5939 | |
| 5940 | // Ensure the correct transforms are used for the displays. |
| 5941 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 5942 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 5943 | } |
| 5944 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5945 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 5946 | protected: |
| 5947 | virtual void SetUp() override { |
| 5948 | InputDispatcherTest::SetUp(); |
| 5949 | |
| 5950 | /** |
| 5951 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 5952 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 5953 | * on. |
| 5954 | */ |
| 5955 | mDispatcher->setInputFilterEnabled(true); |
| 5956 | |
| 5957 | std::shared_ptr<InputApplicationHandle> application = |
| 5958 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5959 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 5960 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5961 | |
| 5962 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5963 | mWindow->setFocusable(true); |
| 5964 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5965 | setFocusedWindow(mWindow); |
| 5966 | mWindow->consumeFocusEvent(true); |
| 5967 | } |
| 5968 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5969 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5970 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5971 | KeyEvent event; |
| 5972 | |
| 5973 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5974 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 5975 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5976 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5977 | const int32_t additionalPolicyFlags = |
| 5978 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 5979 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5980 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5981 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5982 | policyFlags | additionalPolicyFlags)); |
| 5983 | |
| 5984 | InputEvent* received = mWindow->consume(); |
| 5985 | ASSERT_NE(nullptr, received); |
| 5986 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5987 | ASSERT_EQ(received->getType(), InputEventType::KEY); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5988 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 5989 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 5990 | } |
| 5991 | |
| 5992 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5993 | int32_t flags) { |
| 5994 | MotionEvent event; |
| 5995 | PointerProperties pointerProperties[1]; |
| 5996 | PointerCoords pointerCoords[1]; |
| 5997 | pointerProperties[0].clear(); |
| 5998 | pointerProperties[0].id = 0; |
| 5999 | pointerCoords[0].clear(); |
| 6000 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 6001 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 6002 | |
| 6003 | ui::Transform identityTransform; |
| 6004 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6005 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 6006 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 6007 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 6008 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 6009 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 6010 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6011 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 6012 | |
| 6013 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 6014 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6015 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6016 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 6017 | policyFlags | additionalPolicyFlags)); |
| 6018 | |
| 6019 | InputEvent* received = mWindow->consume(); |
| 6020 | ASSERT_NE(nullptr, received); |
| 6021 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6022 | ASSERT_EQ(received->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6023 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 6024 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6025 | } |
| 6026 | |
| 6027 | private: |
| 6028 | sp<FakeWindowHandle> mWindow; |
| 6029 | }; |
| 6030 | |
| 6031 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6032 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 6033 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 6034 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6035 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 6036 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6037 | } |
| 6038 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6039 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6040 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6041 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6042 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 6043 | } |
| 6044 | |
| 6045 | TEST_F(InputFilterInjectionPolicyTest, |
| 6046 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 6047 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6048 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6049 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
| 6052 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6053 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 6054 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6055 | } |
| 6056 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6057 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6058 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6059 | InputDispatcherTest::SetUp(); |
| 6060 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6061 | std::shared_ptr<FakeApplicationHandle> application = |
| 6062 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6063 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6064 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6065 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6066 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6067 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6068 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6069 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6070 | |
| 6071 | // Set focused application. |
| 6072 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6073 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6074 | |
| 6075 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6076 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6077 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6078 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6079 | } |
| 6080 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6081 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6082 | InputDispatcherTest::TearDown(); |
| 6083 | |
| 6084 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6085 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | protected: |
| 6089 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6090 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6091 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6092 | }; |
| 6093 | |
| 6094 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6095 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 6096 | // the onPointerDownOutsideFocus callback. |
| 6097 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6098 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6099 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6100 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6101 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6102 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6103 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6104 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6105 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 6106 | } |
| 6107 | |
| 6108 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 6109 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 6110 | // onPointerDownOutsideFocus callback. |
| 6111 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6113 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6114 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6115 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6116 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6117 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6118 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6119 | } |
| 6120 | |
| 6121 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 6122 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 6123 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6124 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6125 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6126 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6127 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6128 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6129 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6130 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6131 | } |
| 6132 | |
| 6133 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6134 | // DOWN on the window that already has focus. Ensure no window received the |
| 6135 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6136 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6137 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6138 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6139 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6140 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6141 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6142 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6143 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6144 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6145 | } |
| 6146 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6147 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 6148 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 6149 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 6150 | const MotionEvent event = |
| 6151 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6152 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 6153 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6154 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 6155 | .build(); |
| 6156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 6157 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6158 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6159 | |
| 6160 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6161 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 6162 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 6163 | mUnfocusedWindow->assertNoEvents(); |
| 6164 | } |
| 6165 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6166 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 6167 | // windows that point to the same client token. |
| 6168 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 6169 | virtual void SetUp() override { |
| 6170 | InputDispatcherTest::SetUp(); |
| 6171 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6172 | std::shared_ptr<FakeApplicationHandle> application = |
| 6173 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6174 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 6175 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6176 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 6177 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6178 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 6179 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6180 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 6181 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6182 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6183 | } |
| 6184 | |
| 6185 | protected: |
| 6186 | sp<FakeWindowHandle> mWindow1; |
| 6187 | sp<FakeWindowHandle> mWindow2; |
| 6188 | |
| 6189 | // 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] | 6190 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 6191 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 6192 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 6196 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6197 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6198 | InputEvent* event = window->consume(); |
| 6199 | |
| 6200 | ASSERT_NE(nullptr, event) << name.c_str() |
| 6201 | << ": consumer should have returned non-NULL event."; |
| 6202 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6203 | ASSERT_EQ(InputEventType::MOTION, event->getType()) |
| 6204 | << name.c_str() << ": expected MotionEvent, got " << *event; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6205 | |
| 6206 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6207 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 6208 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6209 | |
| 6210 | for (size_t i = 0; i < points.size(); i++) { |
| 6211 | float expectedX = points[i].x; |
| 6212 | float expectedY = points[i].y; |
| 6213 | |
| 6214 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 6215 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 6216 | << ", got " << motionEvent.getX(i); |
| 6217 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 6218 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 6219 | << ", got " << motionEvent.getY(i); |
| 6220 | } |
| 6221 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6222 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6223 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6224 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6225 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 6226 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6227 | |
| 6228 | // Always consume from window1 since it's the window that has the InputReceiver |
| 6229 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 6230 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6231 | }; |
| 6232 | |
| 6233 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 6234 | // Touch Window 1 |
| 6235 | PointF touchedPoint = {10, 10}; |
| 6236 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6237 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6238 | |
| 6239 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6240 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6241 | |
| 6242 | // Touch Window 2 |
| 6243 | touchedPoint = {150, 150}; |
| 6244 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6245 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6246 | } |
| 6247 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6248 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 6249 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6250 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6251 | |
| 6252 | // Touch Window 1 |
| 6253 | PointF touchedPoint = {10, 10}; |
| 6254 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6255 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6256 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6257 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6258 | |
| 6259 | // Touch Window 2 |
| 6260 | touchedPoint = {150, 150}; |
| 6261 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6262 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 6263 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6264 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6265 | // Update the transform so rotation is set |
| 6266 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6267 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 6268 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6269 | } |
| 6270 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6271 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6272 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6273 | |
| 6274 | // Touch Window 1 |
| 6275 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6276 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6277 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6278 | |
| 6279 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6280 | touchedPoints.push_back(PointF{150, 150}); |
| 6281 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6282 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6283 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6284 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6285 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6286 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6287 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6288 | // Update the transform so rotation is set for Window 2 |
| 6289 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6290 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6291 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6292 | } |
| 6293 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6294 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6295 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6296 | |
| 6297 | // Touch Window 1 |
| 6298 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6299 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6300 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6301 | |
| 6302 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6303 | touchedPoints.push_back(PointF{150, 150}); |
| 6304 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6305 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6306 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6307 | |
| 6308 | // Move both windows |
| 6309 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6310 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6311 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6312 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6313 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6314 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6315 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6316 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6317 | expectedPoints.pop_back(); |
| 6318 | |
| 6319 | // Touch Window 2 |
| 6320 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6321 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6322 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6323 | |
| 6324 | // Move both windows |
| 6325 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6326 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6327 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6328 | |
| 6329 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 6333 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 6334 | |
| 6335 | // Touch Window 1 |
| 6336 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6337 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6338 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6339 | |
| 6340 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6341 | touchedPoints.push_back(PointF{150, 150}); |
| 6342 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6343 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6344 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6345 | |
| 6346 | // Move both windows |
| 6347 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6348 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6349 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6350 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6351 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 6354 | /** |
| 6355 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 6356 | * same input channel. |
| 6357 | */ |
| 6358 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 6359 | mWindow1->setSlippery(true); |
| 6360 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6361 | |
| 6362 | // Touch down in window 1 |
| 6363 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6364 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6365 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 6366 | |
| 6367 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 6368 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 6369 | // getting generated. |
| 6370 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6371 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6372 | |
| 6373 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 6374 | } |
| 6375 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 6376 | /** |
| 6377 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 6378 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 6379 | * that the pointer is hovering over may have a different transform. |
| 6380 | */ |
| 6381 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
| 6382 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6383 | |
| 6384 | // Start hover in window 1 |
| 6385 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN, |
| 6386 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6387 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 6388 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
| 6389 | |
| 6390 | // Move hover to window 2. |
| 6391 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6392 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6393 | |
| 6394 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 6395 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 6396 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 6397 | } |
| 6398 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6399 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 6400 | virtual void SetUp() override { |
| 6401 | InputDispatcherTest::SetUp(); |
| 6402 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6403 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6404 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6405 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 6406 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6407 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6408 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6409 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6410 | |
| 6411 | // Set focused application. |
| 6412 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 6413 | |
| 6414 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6415 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6416 | mWindow->consumeFocusEvent(true); |
| 6417 | } |
| 6418 | |
| 6419 | virtual void TearDown() override { |
| 6420 | InputDispatcherTest::TearDown(); |
| 6421 | mWindow.clear(); |
| 6422 | } |
| 6423 | |
| 6424 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6425 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6426 | sp<FakeWindowHandle> mWindow; |
| 6427 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 6428 | |
| 6429 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6430 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6431 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6432 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6433 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6434 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6435 | WINDOW_LOCATION)); |
| 6436 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6437 | |
| 6438 | sp<FakeWindowHandle> addSpyWindow() { |
| 6439 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6440 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6441 | spy->setTrustedOverlay(true); |
| 6442 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6443 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6444 | spy->setDispatchingTimeout(30ms); |
| 6445 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 6446 | return spy; |
| 6447 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6448 | }; |
| 6449 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6450 | // Send a tap and respond, which should not cause an ANR. |
| 6451 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 6452 | tapOnWindow(); |
| 6453 | mWindow->consumeMotionDown(); |
| 6454 | mWindow->consumeMotionUp(); |
| 6455 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6456 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6457 | } |
| 6458 | |
| 6459 | // Send a regular key and respond, which should not cause an ANR. |
| 6460 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6461 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6462 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6463 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6464 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6465 | } |
| 6466 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6467 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 6468 | mWindow->setFocusable(false); |
| 6469 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6470 | mWindow->consumeFocusEvent(false); |
| 6471 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6472 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6473 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6474 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6475 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6477 | // Key will not go to window because we have no focused window. |
| 6478 | // The 'no focused window' ANR timer should start instead. |
| 6479 | |
| 6480 | // Now, the focused application goes away. |
| 6481 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 6482 | // The key should get dropped and there should be no ANR. |
| 6483 | |
| 6484 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6485 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6486 | } |
| 6487 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6488 | // 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] | 6489 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 6490 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6491 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6492 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6493 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6494 | WINDOW_LOCATION)); |
| 6495 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6496 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 6497 | ASSERT_TRUE(sequenceNum); |
| 6498 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6499 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6500 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6501 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6502 | mWindow->consumeMotionEvent( |
| 6503 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6504 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6505 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6506 | } |
| 6507 | |
| 6508 | // Send a key to the app and have the app not respond right away. |
| 6509 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 6510 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6512 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 6513 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6514 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6515 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6516 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6517 | } |
| 6518 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6519 | // We have a focused application, but no focused window |
| 6520 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6521 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6522 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6523 | mWindow->consumeFocusEvent(false); |
| 6524 | |
| 6525 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6526 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6527 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6528 | WINDOW_LOCATION)); |
| 6529 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 6530 | mDispatcher->waitForIdle(); |
| 6531 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6532 | |
| 6533 | // Once a focused event arrives, we get an ANR for this application |
| 6534 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6535 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6536 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6537 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6538 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6539 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6540 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6541 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6542 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6543 | } |
| 6544 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6545 | /** |
| 6546 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 6547 | * there will not be an ANR. |
| 6548 | */ |
| 6549 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 6550 | mWindow->setFocusable(false); |
| 6551 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6552 | mWindow->consumeFocusEvent(false); |
| 6553 | |
| 6554 | KeyEvent event; |
| 6555 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 6556 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 6557 | |
| 6558 | // Define a valid key down event that is stale (too old). |
| 6559 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 6560 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6561 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6562 | |
| 6563 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 6564 | |
| 6565 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6566 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6567 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 6568 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 6569 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 6570 | << "Injection should fail because the event is stale"; |
| 6571 | |
| 6572 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6573 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6574 | mWindow->assertNoEvents(); |
| 6575 | } |
| 6576 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6577 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6578 | // Make sure that we don't notify policy twice about the same ANR. |
| 6579 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6580 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6581 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6582 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6583 | |
| 6584 | // Once a focused event arrives, we get an ANR for this application |
| 6585 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6586 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6587 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6588 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6589 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6590 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6591 | const std::chrono::duration appTimeout = |
| 6592 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6593 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6594 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6595 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6596 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 6597 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6598 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6599 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 6600 | // '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] | 6601 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6602 | } |
| 6603 | |
| 6604 | // We have a focused application, but no focused window |
| 6605 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6606 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6607 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6608 | mWindow->consumeFocusEvent(false); |
| 6609 | |
| 6610 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6611 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6612 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6613 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6614 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6615 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6616 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6617 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6618 | |
| 6619 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6620 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6621 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6622 | mWindow->assertNoEvents(); |
| 6623 | } |
| 6624 | |
| 6625 | /** |
| 6626 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 6627 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 6628 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 6629 | * the ANR mechanism should still work. |
| 6630 | * |
| 6631 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 6632 | * DOWN event, while not responding on the second one. |
| 6633 | */ |
| 6634 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 6635 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6636 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6637 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6638 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6639 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6640 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6641 | |
| 6642 | // Now send ACTION_UP, with identical timestamp |
| 6643 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6644 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6645 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6646 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6647 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6648 | |
| 6649 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 6650 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6651 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6652 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6653 | } |
| 6654 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6655 | // A spy window can receive an ANR |
| 6656 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 6657 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 6658 | |
| 6659 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6660 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6661 | WINDOW_LOCATION)); |
| 6662 | mWindow->consumeMotionDown(); |
| 6663 | |
| 6664 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 6665 | ASSERT_TRUE(sequenceNum); |
| 6666 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6667 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6668 | |
| 6669 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6670 | spy->consumeMotionEvent( |
| 6671 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6672 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6673 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | // 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] | 6677 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6678 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 6679 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6680 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6682 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6683 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6685 | |
| 6686 | // Stuck on the ACTION_UP |
| 6687 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6688 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6689 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6690 | // 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] | 6691 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6692 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6693 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6694 | |
| 6695 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6696 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6697 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6698 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6699 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6700 | } |
| 6701 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6702 | // 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] | 6703 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6704 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 6705 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6706 | |
| 6707 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6708 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6709 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6710 | |
| 6711 | mWindow->consumeMotionDown(); |
| 6712 | // Stuck on the ACTION_UP |
| 6713 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6714 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6715 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6716 | // 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] | 6717 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6718 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6719 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6720 | |
| 6721 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6722 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6723 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6724 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6725 | spy->assertNoEvents(); |
| 6726 | } |
| 6727 | |
| 6728 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 6729 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 6730 | |
| 6731 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6732 | |
| 6733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6734 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6735 | WINDOW_LOCATION)); |
| 6736 | |
| 6737 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6738 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 6739 | ASSERT_TRUE(consumeSeq); |
| 6740 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6741 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6742 | |
| 6743 | monitor.finishEvent(*consumeSeq); |
| 6744 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6745 | |
| 6746 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6747 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6748 | } |
| 6749 | |
| 6750 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 6751 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 6752 | // get an ANR again. |
| 6753 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 6754 | // 2. consume all pending events (= queue becomes healthy again) |
| 6755 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 6756 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 6757 | tapOnWindow(); |
| 6758 | |
| 6759 | mWindow->consumeMotionDown(); |
| 6760 | // Block on ACTION_UP |
| 6761 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6762 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6763 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 6764 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6765 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6766 | mWindow->assertNoEvents(); |
| 6767 | |
| 6768 | tapOnWindow(); |
| 6769 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6770 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6771 | mWindow->consumeMotionUp(); |
| 6772 | |
| 6773 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6774 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6775 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6776 | mWindow->assertNoEvents(); |
| 6777 | } |
| 6778 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6779 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 6780 | // it. |
| 6781 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6782 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6783 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6784 | WINDOW_LOCATION)); |
| 6785 | |
| 6786 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6787 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6788 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6789 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 6790 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6791 | // 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] | 6792 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6793 | mWindow->consumeMotionEvent( |
| 6794 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6795 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6796 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6797 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6798 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | /** |
| 6802 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6803 | * not to to the focused window until the motion is processed. |
| 6804 | * |
| 6805 | * Warning!!! |
| 6806 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6807 | * and the injection timeout that we specify when injecting the key. |
| 6808 | * We must have the injection timeout (10ms) be smaller than |
| 6809 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6810 | * |
| 6811 | * If that value changes, this test should also change. |
| 6812 | */ |
| 6813 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 6814 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6815 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6816 | |
| 6817 | tapOnWindow(); |
| 6818 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6819 | ASSERT_TRUE(downSequenceNum); |
| 6820 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6821 | ASSERT_TRUE(upSequenceNum); |
| 6822 | // Don't finish the events yet, and send a key |
| 6823 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 6824 | // window even if motions are still being processed. But because the injection timeout is short, |
| 6825 | // we will receive INJECTION_TIMED_OUT as the result. |
| 6826 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6827 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6828 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6829 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6830 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6831 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6832 | // and the key remains pending, waiting for the touch events to be processed |
| 6833 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6834 | ASSERT_FALSE(keySequenceNum); |
| 6835 | |
| 6836 | std::this_thread::sleep_for(500ms); |
| 6837 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 6838 | // to the focused window, even though we have not yet finished the motion event |
| 6839 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6840 | mWindow->finishEvent(*downSequenceNum); |
| 6841 | mWindow->finishEvent(*upSequenceNum); |
| 6842 | } |
| 6843 | |
| 6844 | /** |
| 6845 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6846 | * not go to the focused window until the motion is processed. |
| 6847 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 6848 | * focused window right away. |
| 6849 | */ |
| 6850 | TEST_F(InputDispatcherSingleWindowAnr, |
| 6851 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 6852 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6853 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6854 | |
| 6855 | tapOnWindow(); |
| 6856 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6857 | ASSERT_TRUE(downSequenceNum); |
| 6858 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6859 | ASSERT_TRUE(upSequenceNum); |
| 6860 | // Don't finish the events yet, and send a key |
| 6861 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6862 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6863 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6864 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6865 | // At this point, key is still pending, and should not be sent to the application yet. |
| 6866 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6867 | ASSERT_FALSE(keySequenceNum); |
| 6868 | |
| 6869 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 6870 | tapOnWindow(); |
| 6871 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 6872 | // the other events yet. We can finish events in any order. |
| 6873 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 6874 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 6875 | mWindow->consumeMotionDown(); |
| 6876 | mWindow->consumeMotionUp(); |
| 6877 | mWindow->assertNoEvents(); |
| 6878 | } |
| 6879 | |
| 6880 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 6881 | virtual void SetUp() override { |
| 6882 | InputDispatcherTest::SetUp(); |
| 6883 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6884 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6885 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6886 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 6887 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6888 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6889 | // 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] | 6890 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6891 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6892 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 6893 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6894 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6895 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6896 | |
| 6897 | // Set focused application. |
| 6898 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6899 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6900 | |
| 6901 | // Expect one focus window exist in display. |
| 6902 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6903 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6904 | mFocusedWindow->consumeFocusEvent(true); |
| 6905 | } |
| 6906 | |
| 6907 | virtual void TearDown() override { |
| 6908 | InputDispatcherTest::TearDown(); |
| 6909 | |
| 6910 | mUnfocusedWindow.clear(); |
| 6911 | mFocusedWindow.clear(); |
| 6912 | } |
| 6913 | |
| 6914 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6915 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6916 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 6917 | sp<FakeWindowHandle> mFocusedWindow; |
| 6918 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 6919 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 6920 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 6921 | |
| 6922 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 6923 | |
| 6924 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 6925 | |
| 6926 | private: |
| 6927 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6928 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6929 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6930 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6931 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6932 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6933 | location)); |
| 6934 | } |
| 6935 | }; |
| 6936 | |
| 6937 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 6938 | // should be ANR'd first. |
| 6939 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6940 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6941 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6942 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6943 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6944 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6945 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6946 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6947 | // We consumed all events, so no ANR |
| 6948 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6949 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6950 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6951 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6952 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6953 | FOCUSED_WINDOW_LOCATION)); |
| 6954 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6955 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6956 | |
| 6957 | const std::chrono::duration timeout = |
| 6958 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6959 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6960 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 6961 | // sequence to make it consistent |
| 6962 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6963 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6964 | mFocusedWindow->consumeMotionDown(); |
| 6965 | // This cancel is generated because the connection was unresponsive |
| 6966 | mFocusedWindow->consumeMotionCancel(); |
| 6967 | mFocusedWindow->assertNoEvents(); |
| 6968 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6969 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6970 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6971 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6972 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 6976 | // it doesn't matter which order they should have ANR. |
| 6977 | // But we should receive ANR for both. |
| 6978 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 6979 | // Set the timeout for unfocused window to match the focused window |
| 6980 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 6981 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6982 | |
| 6983 | tapOnFocusedWindow(); |
| 6984 | // 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] | 6985 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 6986 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 6987 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6988 | |
| 6989 | // 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] | 6990 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 6991 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 6992 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 6993 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6994 | |
| 6995 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6996 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6997 | |
| 6998 | mFocusedWindow->consumeMotionDown(); |
| 6999 | mFocusedWindow->consumeMotionUp(); |
| 7000 | mUnfocusedWindow->consumeMotionOutside(); |
| 7001 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7002 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 7003 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 7004 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7005 | |
| 7006 | // Both applications should be marked as responsive, in any order |
| 7007 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 7008 | mFocusedWindow->getToken() == responsiveToken2); |
| 7009 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 7010 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 7011 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7012 | } |
| 7013 | |
| 7014 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 7015 | // We should also log an error to account for the dropped event (not tested here). |
| 7016 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 7017 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 7018 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7019 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7020 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7021 | // Receive the events, but don't respond |
| 7022 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 7023 | ASSERT_TRUE(downEventSequenceNum); |
| 7024 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 7025 | ASSERT_TRUE(upEventSequenceNum); |
| 7026 | const std::chrono::duration timeout = |
| 7027 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7028 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7029 | |
| 7030 | // Tap once again |
| 7031 | // 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] | 7032 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7033 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7034 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7035 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7036 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7037 | FOCUSED_WINDOW_LOCATION)); |
| 7038 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 7039 | // valid touch target |
| 7040 | mUnfocusedWindow->assertNoEvents(); |
| 7041 | |
| 7042 | // Consume the first tap |
| 7043 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 7044 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 7045 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7046 | // The second tap did not go to the focused window |
| 7047 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7048 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7049 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7050 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7051 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7052 | } |
| 7053 | |
| 7054 | // If you tap outside of all windows, there will not be ANR |
| 7055 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7056 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7057 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7058 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 7059 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7060 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7061 | } |
| 7062 | |
| 7063 | // Since the focused window is paused, tapping on it should not produce any events |
| 7064 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 7065 | mFocusedWindow->setPaused(true); |
| 7066 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7067 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7068 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7069 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7070 | FOCUSED_WINDOW_LOCATION)); |
| 7071 | |
| 7072 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 7073 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7074 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 7075 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7076 | |
| 7077 | mFocusedWindow->assertNoEvents(); |
| 7078 | mUnfocusedWindow->assertNoEvents(); |
| 7079 | } |
| 7080 | |
| 7081 | /** |
| 7082 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7083 | * not to to the focused window until the motion is processed. |
| 7084 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 7085 | * |
| 7086 | * Warning!!! |
| 7087 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7088 | * and the injection timeout that we specify when injecting the key. |
| 7089 | * We must have the injection timeout (10ms) be smaller than |
| 7090 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7091 | * |
| 7092 | * If that value changes, this test should also change. |
| 7093 | */ |
| 7094 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 7095 | // Set a long ANR timeout to prevent it from triggering |
| 7096 | mFocusedWindow->setDispatchingTimeout(2s); |
| 7097 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7098 | |
| 7099 | tapOnUnfocusedWindow(); |
| 7100 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7101 | ASSERT_TRUE(downSequenceNum); |
| 7102 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7103 | ASSERT_TRUE(upSequenceNum); |
| 7104 | // Don't finish the events yet, and send a key |
| 7105 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 7106 | // window even if motions are still being processed. |
| 7107 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7108 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7109 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7110 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7111 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7112 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7113 | // and the key remains pending, waiting for the touch events to be processed |
| 7114 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 7115 | ASSERT_FALSE(keySequenceNum); |
| 7116 | |
| 7117 | // 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] | 7118 | mFocusedWindow->setFocusable(false); |
| 7119 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7120 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7121 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7122 | |
| 7123 | // Focus events should precede the key events |
| 7124 | mUnfocusedWindow->consumeFocusEvent(true); |
| 7125 | mFocusedWindow->consumeFocusEvent(false); |
| 7126 | |
| 7127 | // Finish the tap events, which should unblock dispatcher |
| 7128 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 7129 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 7130 | |
| 7131 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 7132 | // can finally go to the newly focused "mUnfocusedWindow". |
| 7133 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7134 | mFocusedWindow->assertNoEvents(); |
| 7135 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7136 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7137 | } |
| 7138 | |
| 7139 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 7140 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 7141 | // The other window should not be affected by that. |
| 7142 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 7143 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7144 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7145 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7146 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7147 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7148 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7149 | |
| 7150 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7151 | mDispatcher->notifyMotion( |
| 7152 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7153 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7154 | |
| 7155 | const std::chrono::duration timeout = |
| 7156 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7157 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7158 | |
| 7159 | mUnfocusedWindow->consumeMotionDown(); |
| 7160 | mFocusedWindow->consumeMotionDown(); |
| 7161 | // Focused window may or may not receive ACTION_MOVE |
| 7162 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 7163 | InputEvent* event; |
| 7164 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 7165 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 7166 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 7167 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7168 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7169 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 7170 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 7171 | mFocusedWindow->consumeMotionCancel(); |
| 7172 | } else { |
| 7173 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 7174 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7175 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7176 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7177 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7178 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7179 | mUnfocusedWindow->assertNoEvents(); |
| 7180 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7181 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7182 | } |
| 7183 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7184 | /** |
| 7185 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 7186 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 7187 | * |
| 7188 | * If the user touches another application during this time, the key should be dropped. |
| 7189 | * Next, if a new focused window comes in, without toggling the focused application, |
| 7190 | * then no ANR should occur. |
| 7191 | * |
| 7192 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 7193 | * but in some cases the policy may not update the focused application. |
| 7194 | */ |
| 7195 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 7196 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 7197 | std::make_shared<FakeApplicationHandle>(); |
| 7198 | focusedApplication->setDispatchingTimeout(60ms); |
| 7199 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 7200 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 7201 | mFocusedWindow->setFocusable(false); |
| 7202 | |
| 7203 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7204 | mFocusedWindow->consumeFocusEvent(false); |
| 7205 | |
| 7206 | // Send a key. The ANR timer should start because there is no focused window. |
| 7207 | // 'focusedApplication' will get blamed if this timer completes. |
| 7208 | // 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] | 7209 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7210 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7211 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 7212 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7214 | |
| 7215 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 7216 | // then the injected touches won't cause the focused event to get dropped. |
| 7217 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 7218 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 7219 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 7220 | // For this test, it means that the key would get delivered to the window once it becomes |
| 7221 | // focused. |
| 7222 | std::this_thread::sleep_for(10ms); |
| 7223 | |
| 7224 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7225 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7226 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7227 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7228 | |
| 7229 | // We do not consume the motion right away, because that would require dispatcher to first |
| 7230 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 7231 | // Set the focused window first. |
| 7232 | mFocusedWindow->setFocusable(true); |
| 7233 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7234 | setFocusedWindow(mFocusedWindow); |
| 7235 | mFocusedWindow->consumeFocusEvent(true); |
| 7236 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 7237 | // to another application. This could be a bug / behaviour in the policy. |
| 7238 | |
| 7239 | mUnfocusedWindow->consumeMotionDown(); |
| 7240 | |
| 7241 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7242 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 7243 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 7244 | } |
| 7245 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7246 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 7247 | // that has feature NO_INPUT_CHANNEL. |
| 7248 | // Layout: |
| 7249 | // Top (closest to user) |
| 7250 | // mNoInputWindow (above all windows) |
| 7251 | // mBottomWindow |
| 7252 | // Bottom (furthest from user) |
| 7253 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 7254 | virtual void SetUp() override { |
| 7255 | InputDispatcherTest::SetUp(); |
| 7256 | |
| 7257 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7258 | mNoInputWindow = |
| 7259 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7260 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7261 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7262 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7263 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7264 | // It's perfectly valid for this window to not have an associated input channel |
| 7265 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7266 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 7267 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7268 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7269 | |
| 7270 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7271 | } |
| 7272 | |
| 7273 | protected: |
| 7274 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 7275 | sp<FakeWindowHandle> mNoInputWindow; |
| 7276 | sp<FakeWindowHandle> mBottomWindow; |
| 7277 | }; |
| 7278 | |
| 7279 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 7280 | PointF touchedPoint = {10, 10}; |
| 7281 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7282 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7283 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7284 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7285 | |
| 7286 | mNoInputWindow->assertNoEvents(); |
| 7287 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 7288 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 7289 | // and therefore should prevent mBottomWindow from receiving touches |
| 7290 | mBottomWindow->assertNoEvents(); |
| 7291 | } |
| 7292 | |
| 7293 | /** |
| 7294 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 7295 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 7296 | */ |
| 7297 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 7298 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7299 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7300 | "Window with input channel and NO_INPUT_CHANNEL", |
| 7301 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7302 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7303 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7304 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7305 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7306 | |
| 7307 | PointF touchedPoint = {10, 10}; |
| 7308 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7309 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7310 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7311 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7312 | |
| 7313 | mNoInputWindow->assertNoEvents(); |
| 7314 | mBottomWindow->assertNoEvents(); |
| 7315 | } |
| 7316 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7317 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7318 | protected: |
| 7319 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7320 | sp<FakeWindowHandle> mWindow; |
| 7321 | sp<FakeWindowHandle> mMirror; |
| 7322 | |
| 7323 | virtual void SetUp() override { |
| 7324 | InputDispatcherTest::SetUp(); |
| 7325 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7326 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7327 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7328 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7329 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7330 | mWindow->setFocusable(true); |
| 7331 | mMirror->setFocusable(true); |
| 7332 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7333 | } |
| 7334 | }; |
| 7335 | |
| 7336 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7337 | // Request focus on a mirrored window |
| 7338 | setFocusedWindow(mMirror); |
| 7339 | |
| 7340 | // window gets focused |
| 7341 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7343 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7344 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7345 | } |
| 7346 | |
| 7347 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7348 | // focusable. |
| 7349 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7350 | setFocusedWindow(mMirror); |
| 7351 | |
| 7352 | // window gets focused |
| 7353 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7354 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7355 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7356 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7358 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7359 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7360 | |
| 7361 | mMirror->setFocusable(false); |
| 7362 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7363 | |
| 7364 | // window loses focus since one of the windows associated with the token in not focusable |
| 7365 | mWindow->consumeFocusEvent(false); |
| 7366 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7367 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7368 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7369 | mWindow->assertNoEvents(); |
| 7370 | } |
| 7371 | |
| 7372 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7373 | // invisible. |
| 7374 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 7375 | setFocusedWindow(mMirror); |
| 7376 | |
| 7377 | // window gets focused |
| 7378 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7379 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7380 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7381 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7382 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7383 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7384 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7385 | |
| 7386 | mMirror->setVisible(false); |
| 7387 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7388 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7389 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7390 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7391 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7392 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7393 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7394 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7395 | |
| 7396 | mWindow->setVisible(false); |
| 7397 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7398 | |
| 7399 | // window loses focus only after all windows associated with the token become invisible. |
| 7400 | mWindow->consumeFocusEvent(false); |
| 7401 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7402 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7403 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7404 | mWindow->assertNoEvents(); |
| 7405 | } |
| 7406 | |
| 7407 | // A focused & mirrored window remains focused until both windows are removed. |
| 7408 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7409 | setFocusedWindow(mMirror); |
| 7410 | |
| 7411 | // window gets focused |
| 7412 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7413 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7414 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7415 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7416 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7417 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7418 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7419 | |
| 7420 | // single window is removed but the window token remains focused |
| 7421 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7422 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7423 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7424 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7425 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7427 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7428 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7429 | |
| 7430 | // Both windows are removed |
| 7431 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7432 | mWindow->consumeFocusEvent(false); |
| 7433 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7434 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7435 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7436 | mWindow->assertNoEvents(); |
| 7437 | } |
| 7438 | |
| 7439 | // Focus request can be pending until one window becomes visible. |
| 7440 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7441 | // Request focus on an invisible mirror. |
| 7442 | mWindow->setVisible(false); |
| 7443 | mMirror->setVisible(false); |
| 7444 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7445 | setFocusedWindow(mMirror); |
| 7446 | |
| 7447 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7448 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7449 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7450 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7451 | |
| 7452 | mMirror->setVisible(true); |
| 7453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7454 | |
| 7455 | // window gets focused |
| 7456 | mWindow->consumeFocusEvent(true); |
| 7457 | // window gets the pending key event |
| 7458 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7459 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7460 | |
| 7461 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7462 | protected: |
| 7463 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7464 | sp<FakeWindowHandle> mWindow; |
| 7465 | sp<FakeWindowHandle> mSecondWindow; |
| 7466 | |
| 7467 | void SetUp() override { |
| 7468 | InputDispatcherTest::SetUp(); |
| 7469 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7470 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7471 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7472 | mSecondWindow = |
| 7473 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7474 | mSecondWindow->setFocusable(true); |
| 7475 | |
| 7476 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7477 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7478 | |
| 7479 | setFocusedWindow(mWindow); |
| 7480 | mWindow->consumeFocusEvent(true); |
| 7481 | } |
| 7482 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7483 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7484 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7485 | } |
| 7486 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7487 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7488 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7489 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7490 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7491 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7492 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7493 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7494 | } |
| 7495 | }; |
| 7496 | |
| 7497 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7498 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7499 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7500 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7501 | mSecondWindow->assertNoEvents(); |
| 7502 | |
| 7503 | // Ensure that capture can be enabled from the focus window. |
| 7504 | requestAndVerifyPointerCapture(mWindow, true); |
| 7505 | |
| 7506 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7507 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7508 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7509 | |
| 7510 | // Ensure that capture can be disabled from the window with capture. |
| 7511 | requestAndVerifyPointerCapture(mWindow, false); |
| 7512 | } |
| 7513 | |
| 7514 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7515 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7516 | |
| 7517 | setFocusedWindow(mSecondWindow); |
| 7518 | |
| 7519 | // Ensure that the capture disabled event was sent first. |
| 7520 | mWindow->consumeCaptureEvent(false); |
| 7521 | mWindow->consumeFocusEvent(false); |
| 7522 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7523 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7524 | |
| 7525 | // 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] | 7526 | notifyPointerCaptureChanged({}); |
| 7527 | notifyPointerCaptureChanged(request); |
| 7528 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7529 | mWindow->assertNoEvents(); |
| 7530 | mSecondWindow->assertNoEvents(); |
| 7531 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7532 | } |
| 7533 | |
| 7534 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7535 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7536 | |
| 7537 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7538 | notifyPointerCaptureChanged({}); |
| 7539 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7540 | |
| 7541 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7542 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7543 | mWindow->consumeCaptureEvent(false); |
| 7544 | mWindow->assertNoEvents(); |
| 7545 | } |
| 7546 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7547 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7548 | requestAndVerifyPointerCapture(mWindow, true); |
| 7549 | |
| 7550 | // The first window loses focus. |
| 7551 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7552 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7553 | mWindow->consumeCaptureEvent(false); |
| 7554 | |
| 7555 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7556 | // arrives. |
| 7557 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7558 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7559 | |
| 7560 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7561 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7562 | |
| 7563 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7564 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7565 | |
| 7566 | mSecondWindow->consumeFocusEvent(true); |
| 7567 | mSecondWindow->consumeCaptureEvent(true); |
| 7568 | } |
| 7569 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7570 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7571 | // App repeatedly enables and disables capture. |
| 7572 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7573 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7574 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7575 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7576 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7577 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7578 | |
| 7579 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7580 | // first request is now stale, this should do nothing. |
| 7581 | notifyPointerCaptureChanged(firstRequest); |
| 7582 | mWindow->assertNoEvents(); |
| 7583 | |
| 7584 | // InputReader notifies that the second request was enabled. |
| 7585 | notifyPointerCaptureChanged(secondRequest); |
| 7586 | mWindow->consumeCaptureEvent(true); |
| 7587 | } |
| 7588 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7589 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7590 | requestAndVerifyPointerCapture(mWindow, true); |
| 7591 | |
| 7592 | // App toggles pointer capture off and on. |
| 7593 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7594 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7595 | |
| 7596 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7597 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7598 | |
| 7599 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7600 | // preceding "disable" request. |
| 7601 | notifyPointerCaptureChanged(enableRequest); |
| 7602 | |
| 7603 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7604 | // any notifications. |
| 7605 | mWindow->assertNoEvents(); |
| 7606 | } |
| 7607 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7608 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7609 | protected: |
| 7610 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7611 | |
| 7612 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7613 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7614 | |
| 7615 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7616 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7617 | |
| 7618 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7619 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7620 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7621 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7622 | MAXIMUM_OBSCURING_OPACITY); |
| 7623 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7624 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7625 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7626 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7627 | |
| 7628 | sp<FakeWindowHandle> mTouchWindow; |
| 7629 | |
| 7630 | virtual void SetUp() override { |
| 7631 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7632 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7633 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7634 | } |
| 7635 | |
| 7636 | virtual void TearDown() override { |
| 7637 | InputDispatcherTest::TearDown(); |
| 7638 | mTouchWindow.clear(); |
| 7639 | } |
| 7640 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7641 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 7642 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7643 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7644 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7645 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7646 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7647 | return window; |
| 7648 | } |
| 7649 | |
| 7650 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 7651 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7652 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7653 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7654 | // Generate an arbitrary PID based on the UID |
| 7655 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 7656 | return window; |
| 7657 | } |
| 7658 | |
| 7659 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7660 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7661 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7662 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7663 | } |
| 7664 | }; |
| 7665 | |
| 7666 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7667 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7668 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7669 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7670 | |
| 7671 | touch(); |
| 7672 | |
| 7673 | mTouchWindow->assertNoEvents(); |
| 7674 | } |
| 7675 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7676 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7677 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7678 | const sp<FakeWindowHandle>& w = |
| 7679 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7680 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7681 | |
| 7682 | touch(); |
| 7683 | |
| 7684 | mTouchWindow->assertNoEvents(); |
| 7685 | } |
| 7686 | |
| 7687 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7688 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7689 | const sp<FakeWindowHandle>& w = |
| 7690 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7691 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7692 | |
| 7693 | touch(); |
| 7694 | |
| 7695 | w->assertNoEvents(); |
| 7696 | } |
| 7697 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7698 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7699 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7700 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7701 | |
| 7702 | touch(); |
| 7703 | |
| 7704 | mTouchWindow->consumeAnyMotionDown(); |
| 7705 | } |
| 7706 | |
| 7707 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7708 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7709 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7710 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7711 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7712 | |
| 7713 | touch({PointF{100, 100}}); |
| 7714 | |
| 7715 | mTouchWindow->consumeAnyMotionDown(); |
| 7716 | } |
| 7717 | |
| 7718 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7719 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7720 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7721 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7722 | |
| 7723 | touch(); |
| 7724 | |
| 7725 | mTouchWindow->consumeAnyMotionDown(); |
| 7726 | } |
| 7727 | |
| 7728 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 7729 | const sp<FakeWindowHandle>& w = |
| 7730 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7731 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7732 | |
| 7733 | touch(); |
| 7734 | |
| 7735 | mTouchWindow->consumeAnyMotionDown(); |
| 7736 | } |
| 7737 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7738 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 7739 | const sp<FakeWindowHandle>& w = |
| 7740 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7741 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7742 | |
| 7743 | touch(); |
| 7744 | |
| 7745 | w->assertNoEvents(); |
| 7746 | } |
| 7747 | |
| 7748 | /** |
| 7749 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 7750 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 7751 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 7752 | */ |
| 7753 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7754 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 7755 | const sp<FakeWindowHandle>& w = |
| 7756 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7757 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7758 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7759 | |
| 7760 | touch(); |
| 7761 | |
| 7762 | w->consumeMotionOutside(); |
| 7763 | } |
| 7764 | |
| 7765 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 7766 | const sp<FakeWindowHandle>& w = |
| 7767 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7768 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7769 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7770 | |
| 7771 | touch(); |
| 7772 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7773 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7774 | } |
| 7775 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7776 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7777 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7778 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7779 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7780 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7781 | |
| 7782 | touch(); |
| 7783 | |
| 7784 | mTouchWindow->consumeAnyMotionDown(); |
| 7785 | } |
| 7786 | |
| 7787 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 7788 | const sp<FakeWindowHandle>& w = |
| 7789 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7790 | MAXIMUM_OBSCURING_OPACITY); |
| 7791 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7792 | |
| 7793 | touch(); |
| 7794 | |
| 7795 | mTouchWindow->consumeAnyMotionDown(); |
| 7796 | } |
| 7797 | |
| 7798 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7799 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7800 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7801 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7802 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7803 | |
| 7804 | touch(); |
| 7805 | |
| 7806 | mTouchWindow->assertNoEvents(); |
| 7807 | } |
| 7808 | |
| 7809 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 7810 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 7811 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7812 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7813 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7814 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7815 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7816 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7817 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7818 | |
| 7819 | touch(); |
| 7820 | |
| 7821 | mTouchWindow->assertNoEvents(); |
| 7822 | } |
| 7823 | |
| 7824 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 7825 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 7826 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7827 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7828 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7829 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7830 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7831 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7832 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7833 | |
| 7834 | touch(); |
| 7835 | |
| 7836 | mTouchWindow->consumeAnyMotionDown(); |
| 7837 | } |
| 7838 | |
| 7839 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7840 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 7841 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7842 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7843 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7844 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7845 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7846 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7847 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7848 | |
| 7849 | touch(); |
| 7850 | |
| 7851 | mTouchWindow->consumeAnyMotionDown(); |
| 7852 | } |
| 7853 | |
| 7854 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 7855 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7856 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7857 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7858 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7859 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7860 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7861 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7862 | |
| 7863 | touch(); |
| 7864 | |
| 7865 | mTouchWindow->assertNoEvents(); |
| 7866 | } |
| 7867 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7868 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7869 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 7870 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7871 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7872 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7873 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7874 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7875 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7876 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7877 | |
| 7878 | touch(); |
| 7879 | |
| 7880 | mTouchWindow->assertNoEvents(); |
| 7881 | } |
| 7882 | |
| 7883 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7884 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 7885 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7886 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7887 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7888 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7889 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7890 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7891 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7892 | |
| 7893 | touch(); |
| 7894 | |
| 7895 | mTouchWindow->consumeAnyMotionDown(); |
| 7896 | } |
| 7897 | |
| 7898 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 7899 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7900 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7901 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7902 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7903 | |
| 7904 | touch(); |
| 7905 | |
| 7906 | mTouchWindow->consumeAnyMotionDown(); |
| 7907 | } |
| 7908 | |
| 7909 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 7910 | const sp<FakeWindowHandle>& w = |
| 7911 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7912 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7913 | |
| 7914 | touch(); |
| 7915 | |
| 7916 | mTouchWindow->consumeAnyMotionDown(); |
| 7917 | } |
| 7918 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7919 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7920 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 7921 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7922 | const sp<FakeWindowHandle>& w = |
| 7923 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 7924 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7925 | |
| 7926 | touch(); |
| 7927 | |
| 7928 | mTouchWindow->assertNoEvents(); |
| 7929 | } |
| 7930 | |
| 7931 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 7932 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7933 | const sp<FakeWindowHandle>& w = |
| 7934 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 7935 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7936 | |
| 7937 | touch(); |
| 7938 | |
| 7939 | mTouchWindow->consumeAnyMotionDown(); |
| 7940 | } |
| 7941 | |
| 7942 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7943 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 7944 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 7945 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7946 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7947 | OPACITY_ABOVE_THRESHOLD); |
| 7948 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7949 | |
| 7950 | touch(); |
| 7951 | |
| 7952 | mTouchWindow->consumeAnyMotionDown(); |
| 7953 | } |
| 7954 | |
| 7955 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7956 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 7957 | const sp<FakeWindowHandle>& w1 = |
| 7958 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7959 | OPACITY_BELOW_THRESHOLD); |
| 7960 | const sp<FakeWindowHandle>& w2 = |
| 7961 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7962 | OPACITY_BELOW_THRESHOLD); |
| 7963 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7964 | |
| 7965 | touch(); |
| 7966 | |
| 7967 | mTouchWindow->assertNoEvents(); |
| 7968 | } |
| 7969 | |
| 7970 | /** |
| 7971 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 7972 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 7973 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 7974 | */ |
| 7975 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7976 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 7977 | const sp<FakeWindowHandle>& wB = |
| 7978 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7979 | OPACITY_BELOW_THRESHOLD); |
| 7980 | const sp<FakeWindowHandle>& wC = |
| 7981 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7982 | OPACITY_BELOW_THRESHOLD); |
| 7983 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7984 | |
| 7985 | touch(); |
| 7986 | |
| 7987 | mTouchWindow->assertNoEvents(); |
| 7988 | } |
| 7989 | |
| 7990 | /** |
| 7991 | * This test is testing that a window from a different UID but with same application token doesn't |
| 7992 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 7993 | */ |
| 7994 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7995 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 7996 | const sp<FakeWindowHandle>& w = |
| 7997 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7998 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7999 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 8000 | |
| 8001 | touch(); |
| 8002 | |
| 8003 | mTouchWindow->consumeAnyMotionDown(); |
| 8004 | } |
| 8005 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8006 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 8007 | protected: |
| 8008 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8009 | sp<FakeWindowHandle> mWindow; |
| 8010 | sp<FakeWindowHandle> mSecondWindow; |
| 8011 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8012 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8013 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 8014 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8015 | |
| 8016 | void SetUp() override { |
| 8017 | InputDispatcherTest::SetUp(); |
| 8018 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8019 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8020 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8021 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8022 | mSecondWindow = |
| 8023 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8024 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8025 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8026 | mSpyWindow = |
| 8027 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8028 | mSpyWindow->setSpy(true); |
| 8029 | mSpyWindow->setTrustedOverlay(true); |
| 8030 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 8031 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8032 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8033 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8034 | } |
| 8035 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8036 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 8037 | switch (fromSource) { |
| 8038 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 8039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8040 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8041 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8042 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8043 | break; |
| 8044 | case AINPUT_SOURCE_STYLUS: |
| 8045 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8046 | injectMotionEvent( |
| 8047 | mDispatcher, |
| 8048 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8049 | AINPUT_SOURCE_STYLUS) |
| 8050 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8051 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8052 | .x(50) |
| 8053 | .y(50)) |
| 8054 | .build())); |
| 8055 | break; |
| 8056 | case AINPUT_SOURCE_MOUSE: |
| 8057 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8058 | injectMotionEvent( |
| 8059 | mDispatcher, |
| 8060 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8061 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8062 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8063 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8064 | .x(50) |
| 8065 | .y(50)) |
| 8066 | .build())); |
| 8067 | break; |
| 8068 | default: |
| 8069 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 8070 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8071 | |
| 8072 | // Window should receive motion event. |
| 8073 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8074 | // Spy window should also receive motion event |
| 8075 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
| 8078 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 8079 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 8080 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8081 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8082 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8083 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8084 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8085 | |
| 8086 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8087 | mDragWindow = |
| 8088 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8089 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8090 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8091 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8092 | |
| 8093 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8094 | bool transferred = |
| 8095 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8096 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8097 | if (transferred) { |
| 8098 | mWindow->consumeMotionCancel(); |
| 8099 | mDragWindow->consumeMotionDown(); |
| 8100 | } |
| 8101 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8102 | } |
| 8103 | }; |
| 8104 | |
| 8105 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8106 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8107 | |
| 8108 | // Move on window. |
| 8109 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8110 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8111 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8112 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8113 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8114 | mWindow->consumeDragEvent(false, 50, 50); |
| 8115 | mSecondWindow->assertNoEvents(); |
| 8116 | |
| 8117 | // Move to another window. |
| 8118 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8119 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8120 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8121 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8122 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8123 | mWindow->consumeDragEvent(true, 150, 50); |
| 8124 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8125 | |
| 8126 | // Move back to original window. |
| 8127 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8128 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8129 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8130 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8131 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8132 | mWindow->consumeDragEvent(false, 50, 50); |
| 8133 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 8134 | |
| 8135 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8136 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8137 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8138 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8139 | mWindow->assertNoEvents(); |
| 8140 | mSecondWindow->assertNoEvents(); |
| 8141 | } |
| 8142 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8143 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8144 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8145 | |
| 8146 | // No cancel event after drag start |
| 8147 | mSpyWindow->assertNoEvents(); |
| 8148 | |
| 8149 | const MotionEvent secondFingerDownEvent = |
| 8150 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8151 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8152 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8153 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8154 | .build(); |
| 8155 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8156 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8157 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8158 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8159 | |
| 8160 | // Receives cancel for first pointer after next pointer down |
| 8161 | mSpyWindow->consumeMotionCancel(); |
| 8162 | mSpyWindow->consumeMotionDown(); |
| 8163 | |
| 8164 | mSpyWindow->assertNoEvents(); |
| 8165 | } |
| 8166 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8167 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8168 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8169 | |
| 8170 | // Move on window. |
| 8171 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8172 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8173 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8174 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8175 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8176 | mWindow->consumeDragEvent(false, 50, 50); |
| 8177 | mSecondWindow->assertNoEvents(); |
| 8178 | |
| 8179 | // Move to another window. |
| 8180 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8181 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8182 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8183 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8184 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8185 | mWindow->consumeDragEvent(true, 150, 50); |
| 8186 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8187 | |
| 8188 | // drop to another window. |
| 8189 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8190 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8191 | {150, 50})) |
| 8192 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8193 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8194 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8195 | mWindow->assertNoEvents(); |
| 8196 | mSecondWindow->assertNoEvents(); |
| 8197 | } |
| 8198 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8199 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8200 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8201 | |
| 8202 | // Move on window and keep button pressed. |
| 8203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8204 | injectMotionEvent(mDispatcher, |
| 8205 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8206 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8207 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8208 | .build())) |
| 8209 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8210 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8211 | mWindow->consumeDragEvent(false, 50, 50); |
| 8212 | mSecondWindow->assertNoEvents(); |
| 8213 | |
| 8214 | // Move to another window and release button, expect to drop item. |
| 8215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8216 | injectMotionEvent(mDispatcher, |
| 8217 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8218 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8219 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8220 | .build())) |
| 8221 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8222 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8223 | mWindow->assertNoEvents(); |
| 8224 | mSecondWindow->assertNoEvents(); |
| 8225 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8226 | |
| 8227 | // nothing to the window. |
| 8228 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8229 | injectMotionEvent(mDispatcher, |
| 8230 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 8231 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8232 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8233 | .build())) |
| 8234 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8235 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8236 | mWindow->assertNoEvents(); |
| 8237 | mSecondWindow->assertNoEvents(); |
| 8238 | } |
| 8239 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8240 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8241 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8242 | |
| 8243 | // Set second window invisible. |
| 8244 | mSecondWindow->setVisible(false); |
| 8245 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 8246 | |
| 8247 | // Move on window. |
| 8248 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8249 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8250 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8251 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8252 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8253 | mWindow->consumeDragEvent(false, 50, 50); |
| 8254 | mSecondWindow->assertNoEvents(); |
| 8255 | |
| 8256 | // Move to another window. |
| 8257 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8258 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8259 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8260 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8261 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8262 | mWindow->consumeDragEvent(true, 150, 50); |
| 8263 | mSecondWindow->assertNoEvents(); |
| 8264 | |
| 8265 | // drop to another window. |
| 8266 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8267 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8268 | {150, 50})) |
| 8269 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8270 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8271 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 8272 | mWindow->assertNoEvents(); |
| 8273 | mSecondWindow->assertNoEvents(); |
| 8274 | } |
| 8275 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8276 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8277 | // Ensure window could track pointerIds if it didn't support split touch. |
| 8278 | mWindow->setPreventSplitting(true); |
| 8279 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8281 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8282 | {50, 50})) |
| 8283 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8284 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8285 | |
| 8286 | const MotionEvent secondFingerDownEvent = |
| 8287 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8288 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8289 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8290 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8291 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8292 | .build(); |
| 8293 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8294 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8295 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8296 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8297 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8298 | |
| 8299 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8300 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8301 | } |
| 8302 | |
| 8303 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 8304 | // First down on second window. |
| 8305 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8306 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8307 | {150, 50})) |
| 8308 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8309 | |
| 8310 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8311 | |
| 8312 | // Second down on first window. |
| 8313 | const MotionEvent secondFingerDownEvent = |
| 8314 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8315 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8316 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8317 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8318 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8319 | .build(); |
| 8320 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8321 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8322 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8323 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8324 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8325 | |
| 8326 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8327 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8328 | |
| 8329 | // Move on window. |
| 8330 | const MotionEvent secondFingerMoveEvent = |
| 8331 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8332 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8333 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8334 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8335 | .build(); |
| 8336 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8337 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8338 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8339 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8340 | mWindow->consumeDragEvent(false, 50, 50); |
| 8341 | mSecondWindow->consumeMotionMove(); |
| 8342 | |
| 8343 | // Release the drag pointer should perform drop. |
| 8344 | const MotionEvent secondFingerUpEvent = |
| 8345 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8346 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8347 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8348 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8349 | .build(); |
| 8350 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8351 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8352 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8353 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8354 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 8355 | mWindow->assertNoEvents(); |
| 8356 | mSecondWindow->consumeMotionMove(); |
| 8357 | } |
| 8358 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8359 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8360 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8361 | |
| 8362 | // Update window of second display. |
| 8363 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8364 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8365 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8366 | |
| 8367 | // Let second display has a touch state. |
| 8368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8369 | injectMotionEvent(mDispatcher, |
| 8370 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8371 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8372 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8373 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8374 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8375 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8376 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8377 | // Update window again. |
| 8378 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8379 | |
| 8380 | // Move on window. |
| 8381 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8382 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8383 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8384 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8385 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8386 | mWindow->consumeDragEvent(false, 50, 50); |
| 8387 | mSecondWindow->assertNoEvents(); |
| 8388 | |
| 8389 | // Move to another window. |
| 8390 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8391 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8392 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8393 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8394 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8395 | mWindow->consumeDragEvent(true, 150, 50); |
| 8396 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8397 | |
| 8398 | // drop to another window. |
| 8399 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8400 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8401 | {150, 50})) |
| 8402 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8403 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8404 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8405 | mWindow->assertNoEvents(); |
| 8406 | mSecondWindow->assertNoEvents(); |
| 8407 | } |
| 8408 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8409 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8410 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8411 | // Move on window. |
| 8412 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8413 | injectMotionEvent(mDispatcher, |
| 8414 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8415 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8416 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8417 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8418 | .x(50) |
| 8419 | .y(50)) |
| 8420 | .build())) |
| 8421 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8422 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8423 | mWindow->consumeDragEvent(false, 50, 50); |
| 8424 | mSecondWindow->assertNoEvents(); |
| 8425 | |
| 8426 | // Move to another window. |
| 8427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8428 | injectMotionEvent(mDispatcher, |
| 8429 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8430 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8431 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8432 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8433 | .x(150) |
| 8434 | .y(50)) |
| 8435 | .build())) |
| 8436 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8437 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8438 | mWindow->consumeDragEvent(true, 150, 50); |
| 8439 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8440 | |
| 8441 | // drop to another window. |
| 8442 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8443 | injectMotionEvent(mDispatcher, |
| 8444 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8445 | .buttonState(0) |
| 8446 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8447 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8448 | .x(150) |
| 8449 | .y(50)) |
| 8450 | .build())) |
| 8451 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8452 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8453 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8454 | mWindow->assertNoEvents(); |
| 8455 | mSecondWindow->assertNoEvents(); |
| 8456 | } |
| 8457 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8458 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8459 | |
| 8460 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8461 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8462 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8463 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8464 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8465 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8466 | window->setFocusable(true); |
| 8467 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8468 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8469 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8470 | |
| 8471 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8472 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8473 | window->assertNoEvents(); |
| 8474 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8475 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8476 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8477 | window->assertNoEvents(); |
| 8478 | |
| 8479 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8480 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8481 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8482 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8483 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8484 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8485 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8486 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8487 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8488 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8489 | window->assertNoEvents(); |
| 8490 | } |
| 8491 | |
| 8492 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8493 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8494 | std::make_shared<FakeApplicationHandle>(); |
| 8495 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8496 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8497 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8498 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8499 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8500 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8501 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8502 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8503 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8504 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8505 | window->setOwnerInfo(222, 222); |
| 8506 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8507 | window->setFocusable(true); |
| 8508 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8509 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8510 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8511 | |
| 8512 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8513 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8514 | window->assertNoEvents(); |
| 8515 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8516 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8517 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8518 | window->assertNoEvents(); |
| 8519 | |
| 8520 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8521 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8522 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8523 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8524 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8525 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8526 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8527 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8528 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8529 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8530 | window->assertNoEvents(); |
| 8531 | } |
| 8532 | |
| 8533 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8534 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8535 | std::make_shared<FakeApplicationHandle>(); |
| 8536 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8537 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8538 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8539 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8540 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8541 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8542 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8543 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8544 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8545 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8546 | window->setOwnerInfo(222, 222); |
| 8547 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8548 | window->setFocusable(true); |
| 8549 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8550 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8551 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8552 | |
| 8553 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8554 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8555 | window->assertNoEvents(); |
| 8556 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8557 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8558 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8559 | window->assertNoEvents(); |
| 8560 | |
| 8561 | // When the window is no longer obscured because it went on top, it should get input |
| 8562 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8563 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8564 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8565 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8566 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8567 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8568 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8569 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8570 | window->assertNoEvents(); |
| 8571 | } |
| 8572 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8573 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8574 | protected: |
| 8575 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8576 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8577 | sp<FakeWindowHandle> mWindow; |
| 8578 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8579 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8580 | |
| 8581 | void SetUp() override { |
| 8582 | InputDispatcherTest::SetUp(); |
| 8583 | |
| 8584 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8585 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8586 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8587 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8588 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8589 | mSecondWindow = |
| 8590 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8591 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8592 | mThirdWindow = |
| 8593 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8594 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8595 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8596 | |
| 8597 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8598 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8599 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8600 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8601 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8602 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8603 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8604 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8605 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8606 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8607 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8608 | mThirdWindow->assertNoEvents(); |
| 8609 | } |
| 8610 | |
| 8611 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8612 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8613 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8614 | SECOND_DISPLAY_ID)) { |
| 8615 | mWindow->assertNoEvents(); |
| 8616 | mSecondWindow->assertNoEvents(); |
| 8617 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8618 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8619 | } |
| 8620 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8621 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 8622 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8623 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8624 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8625 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8626 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8627 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8628 | } |
| 8629 | }; |
| 8630 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8631 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8632 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8633 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8634 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8635 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8636 | } |
| 8637 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8638 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8639 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8640 | int32_t ownerPid = windowInfo.ownerPid; |
| 8641 | int32_t ownerUid = windowInfo.ownerUid; |
| 8642 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 8643 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8644 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8645 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8646 | mWindow->assertNoEvents(); |
| 8647 | mSecondWindow->assertNoEvents(); |
| 8648 | } |
| 8649 | |
| 8650 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8651 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8652 | int32_t ownerPid = windowInfo.ownerPid; |
| 8653 | int32_t ownerUid = windowInfo.ownerUid; |
| 8654 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8655 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8656 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8657 | } |
| 8658 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8659 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8660 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8661 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8662 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8663 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8664 | mWindow->assertNoEvents(); |
| 8665 | mSecondWindow->assertNoEvents(); |
| 8666 | } |
| 8667 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8668 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8669 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8670 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8671 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8672 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8673 | mWindow->assertNoEvents(); |
| 8674 | mSecondWindow->assertNoEvents(); |
| 8675 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8676 | } |
| 8677 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8678 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8679 | // Interact with the window first. |
| 8680 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8681 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8682 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8683 | |
| 8684 | // Then remove focus. |
| 8685 | mWindow->setFocusable(false); |
| 8686 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8687 | |
| 8688 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8689 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8690 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8691 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8692 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8693 | } |
| 8694 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8695 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8696 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8697 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8698 | std::shared_ptr<FakeApplicationHandle> application = |
| 8699 | std::make_shared<FakeApplicationHandle>(); |
| 8700 | std::string name = "Fake Spy "; |
| 8701 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8702 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8703 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8704 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8705 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8706 | return spy; |
| 8707 | } |
| 8708 | |
| 8709 | sp<FakeWindowHandle> createForeground() { |
| 8710 | std::shared_ptr<FakeApplicationHandle> application = |
| 8711 | std::make_shared<FakeApplicationHandle>(); |
| 8712 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8713 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 8714 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8715 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8716 | return window; |
| 8717 | } |
| 8718 | |
| 8719 | private: |
| 8720 | int mSpyCount{0}; |
| 8721 | }; |
| 8722 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8723 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8724 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8725 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 8726 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8727 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 8728 | ScopedSilentDeath _silentDeath; |
| 8729 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8730 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8731 | spy->setTrustedOverlay(false); |
| 8732 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 8733 | ".* not a trusted overlay"); |
| 8734 | } |
| 8735 | |
| 8736 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8737 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 8738 | */ |
| 8739 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8740 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8741 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 8742 | |
| 8743 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8744 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8745 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8746 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8747 | } |
| 8748 | |
| 8749 | /** |
| 8750 | * Verify the order in which different input windows receive events. The touched foreground window |
| 8751 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 8752 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 8753 | * receive events before ones belows it. |
| 8754 | * |
| 8755 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 8756 | * spy1, spy2, window, spy3. |
| 8757 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 8758 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 8759 | * window. |
| 8760 | */ |
| 8761 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 8762 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8763 | auto spy1 = createSpy(); |
| 8764 | auto spy2 = createSpy(); |
| 8765 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8766 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 8767 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 8768 | const size_t numChannels = channels.size(); |
| 8769 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 8770 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8771 | if (!epollFd.ok()) { |
| 8772 | FAIL() << "Failed to create epoll fd"; |
| 8773 | } |
| 8774 | |
| 8775 | for (size_t i = 0; i < numChannels; i++) { |
| 8776 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 8777 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 8778 | FAIL() << "Failed to add fd to epoll"; |
| 8779 | } |
| 8780 | } |
| 8781 | |
| 8782 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8783 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8784 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8785 | |
| 8786 | std::vector<size_t> eventOrder; |
| 8787 | std::vector<struct epoll_event> events(numChannels); |
| 8788 | for (;;) { |
| 8789 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 8790 | (100ms).count()); |
| 8791 | if (nFds < 0) { |
| 8792 | FAIL() << "Failed to call epoll_wait"; |
| 8793 | } |
| 8794 | if (nFds == 0) { |
| 8795 | break; // epoll_wait timed out |
| 8796 | } |
| 8797 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 8798 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 8799 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8800 | channels[i]->consumeMotionDown(); |
| 8801 | } |
| 8802 | } |
| 8803 | |
| 8804 | // Verify the order in which the events were received. |
| 8805 | EXPECT_EQ(3u, eventOrder.size()); |
| 8806 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 8807 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 8808 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 8809 | } |
| 8810 | |
| 8811 | /** |
| 8812 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 8813 | */ |
| 8814 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 8815 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8816 | auto spy = createSpy(); |
| 8817 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8818 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8819 | |
| 8820 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8821 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8823 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8824 | spy->assertNoEvents(); |
| 8825 | } |
| 8826 | |
| 8827 | /** |
| 8828 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 8829 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 8830 | * to the window. |
| 8831 | */ |
| 8832 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 8833 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8834 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8835 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 8836 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8837 | |
| 8838 | // Inject an event outside the spy window's touchable region. |
| 8839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8840 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8841 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8842 | window->consumeMotionDown(); |
| 8843 | spy->assertNoEvents(); |
| 8844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8845 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8846 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8847 | window->consumeMotionUp(); |
| 8848 | spy->assertNoEvents(); |
| 8849 | |
| 8850 | // Inject an event inside the spy window's touchable region. |
| 8851 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8852 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8853 | {5, 10})) |
| 8854 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8855 | window->consumeMotionDown(); |
| 8856 | spy->consumeMotionDown(); |
| 8857 | } |
| 8858 | |
| 8859 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8860 | * 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] | 8861 | * 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] | 8862 | */ |
| 8863 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 8864 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8865 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8866 | auto spy = createSpy(); |
| 8867 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8868 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8869 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 8870 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8871 | |
| 8872 | // Inject an event outside the spy window's frame and touchable region. |
| 8873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8874 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8875 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8876 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8877 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8878 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8879 | } |
| 8880 | |
| 8881 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8882 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 8883 | * pointers that are down within its bounds. |
| 8884 | */ |
| 8885 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 8886 | auto windowLeft = createForeground(); |
| 8887 | windowLeft->setFrame({0, 0, 100, 200}); |
| 8888 | auto windowRight = createForeground(); |
| 8889 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8890 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8891 | spy->setFrame({0, 0, 200, 200}); |
| 8892 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 8893 | |
| 8894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8895 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8896 | {50, 50})) |
| 8897 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8898 | windowLeft->consumeMotionDown(); |
| 8899 | spy->consumeMotionDown(); |
| 8900 | |
| 8901 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8902 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8903 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8904 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8905 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8906 | .build(); |
| 8907 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8908 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8909 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8910 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8911 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8912 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8913 | } |
| 8914 | |
| 8915 | /** |
| 8916 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 8917 | * the spy should receive the second pointer with ACTION_DOWN. |
| 8918 | */ |
| 8919 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 8920 | auto window = createForeground(); |
| 8921 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8922 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8923 | spyRight->setFrame({100, 0, 200, 200}); |
| 8924 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 8925 | |
| 8926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8927 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8928 | {50, 50})) |
| 8929 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8930 | window->consumeMotionDown(); |
| 8931 | spyRight->assertNoEvents(); |
| 8932 | |
| 8933 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8934 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8935 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8936 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8937 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8938 | .build(); |
| 8939 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8940 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8941 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8942 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8943 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8944 | spyRight->consumeMotionDown(); |
| 8945 | } |
| 8946 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8947 | /** |
| 8948 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 8949 | * windows should be allowed to control split touch. |
| 8950 | */ |
| 8951 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8952 | // 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] | 8953 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8954 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8955 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8956 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8957 | auto window = createForeground(); |
| 8958 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8959 | |
| 8960 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8961 | |
| 8962 | // First finger down, no window touched. |
| 8963 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8964 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8965 | {100, 200})) |
| 8966 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8967 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8968 | window->assertNoEvents(); |
| 8969 | |
| 8970 | // Second finger down on window, the window should receive touch down. |
| 8971 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8972 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8973 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8974 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8975 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8976 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8977 | .build(); |
| 8978 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8979 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8980 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8981 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8982 | |
| 8983 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8984 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8985 | } |
| 8986 | |
| 8987 | /** |
| 8988 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 8989 | * do not receive key events. |
| 8990 | */ |
| 8991 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8992 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8993 | spy->setFocusable(false); |
| 8994 | |
| 8995 | auto window = createForeground(); |
| 8996 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8997 | setFocusedWindow(window); |
| 8998 | window->consumeFocusEvent(true); |
| 8999 | |
| 9000 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 9001 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9002 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9003 | |
| 9004 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 9005 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9006 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9007 | |
| 9008 | spy->assertNoEvents(); |
| 9009 | } |
| 9010 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9011 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 9012 | |
| 9013 | /** |
| 9014 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 9015 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 9016 | */ |
| 9017 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 9018 | auto window = createForeground(); |
| 9019 | auto spy1 = createSpy(); |
| 9020 | auto spy2 = createSpy(); |
| 9021 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 9022 | |
| 9023 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9024 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9025 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9026 | window->consumeMotionDown(); |
| 9027 | spy1->consumeMotionDown(); |
| 9028 | spy2->consumeMotionDown(); |
| 9029 | |
| 9030 | // Pilfer pointers from the second spy window. |
| 9031 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 9032 | spy2->assertNoEvents(); |
| 9033 | spy1->consumeMotionCancel(); |
| 9034 | window->consumeMotionCancel(); |
| 9035 | |
| 9036 | // The rest of the gesture should only be sent to the second spy window. |
| 9037 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9038 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 9039 | ADISPLAY_ID_DEFAULT)) |
| 9040 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9041 | spy2->consumeMotionMove(); |
| 9042 | spy1->assertNoEvents(); |
| 9043 | window->assertNoEvents(); |
| 9044 | } |
| 9045 | |
| 9046 | /** |
| 9047 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 9048 | * in the middle of the gesture. |
| 9049 | */ |
| 9050 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 9051 | auto window = createForeground(); |
| 9052 | auto spy = createSpy(); |
| 9053 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9054 | |
| 9055 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9056 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9057 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9058 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9059 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9060 | |
| 9061 | window->releaseChannel(); |
| 9062 | |
| 9063 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9064 | |
| 9065 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9066 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9067 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9068 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9069 | } |
| 9070 | |
| 9071 | /** |
| 9072 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 9073 | * the spy, but not to any other windows. |
| 9074 | */ |
| 9075 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 9076 | auto spy = createSpy(); |
| 9077 | auto window = createForeground(); |
| 9078 | |
| 9079 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9080 | |
| 9081 | // First finger down on the window and the spy. |
| 9082 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9083 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9084 | {100, 200})) |
| 9085 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9086 | spy->consumeMotionDown(); |
| 9087 | window->consumeMotionDown(); |
| 9088 | |
| 9089 | // Spy window pilfers the pointers. |
| 9090 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9091 | window->consumeMotionCancel(); |
| 9092 | |
| 9093 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 9094 | const MotionEvent secondFingerDownEvent = |
| 9095 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9096 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9097 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9098 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9099 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9100 | .build(); |
| 9101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9102 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9103 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9104 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9105 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9106 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9107 | |
| 9108 | // Third finger goes down outside all windows, so injection should fail. |
| 9109 | const MotionEvent thirdFingerDownEvent = |
| 9110 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9111 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9112 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9113 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9114 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 9115 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9116 | .build(); |
| 9117 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 9118 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9119 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9120 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9121 | |
| 9122 | spy->assertNoEvents(); |
| 9123 | window->assertNoEvents(); |
| 9124 | } |
| 9125 | |
| 9126 | /** |
| 9127 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 9128 | */ |
| 9129 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 9130 | auto spy = createSpy(); |
| 9131 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9132 | auto window = createForeground(); |
| 9133 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9134 | |
| 9135 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9136 | |
| 9137 | // First finger down on the window only |
| 9138 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9139 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9140 | {150, 150})) |
| 9141 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9142 | window->consumeMotionDown(); |
| 9143 | |
| 9144 | // Second finger down on the spy and window |
| 9145 | const MotionEvent secondFingerDownEvent = |
| 9146 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9147 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9148 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9149 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9150 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9151 | .build(); |
| 9152 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9153 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9154 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9155 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9156 | spy->consumeMotionDown(); |
| 9157 | window->consumeMotionPointerDown(1); |
| 9158 | |
| 9159 | // Third finger down on the spy and window |
| 9160 | const MotionEvent thirdFingerDownEvent = |
| 9161 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9162 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9163 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9164 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9165 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 9166 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9167 | .build(); |
| 9168 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9169 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9170 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9171 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9172 | spy->consumeMotionPointerDown(1); |
| 9173 | window->consumeMotionPointerDown(2); |
| 9174 | |
| 9175 | // Spy window pilfers the pointers. |
| 9176 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9177 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9178 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9179 | |
| 9180 | spy->assertNoEvents(); |
| 9181 | window->assertNoEvents(); |
| 9182 | } |
| 9183 | |
| 9184 | /** |
| 9185 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 9186 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 9187 | * window, then that window should receive ACTION_CANCEL. |
| 9188 | */ |
| 9189 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 9190 | auto spy = createSpy(); |
| 9191 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9192 | auto window = createForeground(); |
| 9193 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9194 | |
| 9195 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9196 | |
| 9197 | // First finger down on both spy and window |
| 9198 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9199 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9200 | {10, 10})) |
| 9201 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9202 | window->consumeMotionDown(); |
| 9203 | spy->consumeMotionDown(); |
| 9204 | |
| 9205 | // Second finger down on the spy and window |
| 9206 | const MotionEvent secondFingerDownEvent = |
| 9207 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9208 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9209 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9210 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9211 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9212 | .build(); |
| 9213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9214 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9215 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9216 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9217 | spy->consumeMotionPointerDown(1); |
| 9218 | window->consumeMotionPointerDown(1); |
| 9219 | |
| 9220 | // Spy window pilfers the pointers. |
| 9221 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9222 | window->consumeMotionCancel(); |
| 9223 | |
| 9224 | spy->assertNoEvents(); |
| 9225 | window->assertNoEvents(); |
| 9226 | } |
| 9227 | |
| 9228 | /** |
| 9229 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 9230 | * be sent to other windows |
| 9231 | */ |
| 9232 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 9233 | auto spy = createSpy(); |
| 9234 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9235 | auto window = createForeground(); |
| 9236 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9237 | |
| 9238 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9239 | |
| 9240 | // First finger down on both window and spy |
| 9241 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9242 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9243 | {10, 10})) |
| 9244 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9245 | window->consumeMotionDown(); |
| 9246 | spy->consumeMotionDown(); |
| 9247 | |
| 9248 | // Spy window pilfers the pointers. |
| 9249 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9250 | window->consumeMotionCancel(); |
| 9251 | |
| 9252 | // Second finger down on the window only |
| 9253 | const MotionEvent secondFingerDownEvent = |
| 9254 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9255 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9256 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9257 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9258 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9259 | .build(); |
| 9260 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9261 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9262 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9263 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9264 | window->consumeMotionDown(); |
| 9265 | window->assertNoEvents(); |
| 9266 | |
| 9267 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 9268 | spy->consumeMotionMove(); |
| 9269 | spy->assertNoEvents(); |
| 9270 | } |
| 9271 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9272 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 9273 | public: |
| 9274 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 9275 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9276 | std::make_shared<FakeApplicationHandle>(); |
| 9277 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9278 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 9279 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9280 | overlay->setFocusable(false); |
| 9281 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9282 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9283 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9284 | overlay->setTrustedOverlay(true); |
| 9285 | |
| 9286 | std::shared_ptr<FakeApplicationHandle> application = |
| 9287 | std::make_shared<FakeApplicationHandle>(); |
| 9288 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9289 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9290 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9291 | window->setFocusable(true); |
| 9292 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9293 | |
| 9294 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9295 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9296 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9297 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9298 | return {std::move(overlay), std::move(window)}; |
| 9299 | } |
| 9300 | |
| 9301 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9302 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9303 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9304 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9305 | } |
| 9306 | |
| 9307 | void sendStylusEvent(int32_t action) { |
| 9308 | NotifyMotionArgs motionArgs = |
| 9309 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9310 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9311 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9312 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9313 | } |
| 9314 | }; |
| 9315 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9316 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9317 | |
| 9318 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 9319 | ScopedSilentDeath _silentDeath; |
| 9320 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9321 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9322 | overlay->setTrustedOverlay(false); |
| 9323 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9324 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9325 | ".* not a trusted overlay"); |
| 9326 | } |
| 9327 | |
| 9328 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9329 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9330 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9331 | |
| 9332 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9333 | overlay->consumeMotionDown(); |
| 9334 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9335 | overlay->consumeMotionUp(); |
| 9336 | |
| 9337 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9338 | window->consumeMotionDown(); |
| 9339 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9340 | window->consumeMotionUp(); |
| 9341 | |
| 9342 | overlay->assertNoEvents(); |
| 9343 | window->assertNoEvents(); |
| 9344 | } |
| 9345 | |
| 9346 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9347 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9348 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9349 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9350 | |
| 9351 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9352 | overlay->consumeMotionDown(); |
| 9353 | window->consumeMotionDown(); |
| 9354 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9355 | overlay->consumeMotionUp(); |
| 9356 | window->consumeMotionUp(); |
| 9357 | |
| 9358 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9359 | window->consumeMotionDown(); |
| 9360 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9361 | window->consumeMotionUp(); |
| 9362 | |
| 9363 | overlay->assertNoEvents(); |
| 9364 | window->assertNoEvents(); |
| 9365 | } |
| 9366 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9367 | /** |
| 9368 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9369 | * The scenario is as follows: |
| 9370 | * - The stylus interceptor overlay is configured as a spy window. |
| 9371 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9372 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9373 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9374 | */ |
| 9375 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9376 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9377 | overlay->setSpy(true); |
| 9378 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9379 | |
| 9380 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9381 | overlay->consumeMotionDown(); |
| 9382 | window->consumeMotionDown(); |
| 9383 | |
| 9384 | // The interceptor pilfers the pointers. |
| 9385 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9386 | window->consumeMotionCancel(); |
| 9387 | |
| 9388 | // The interceptor configures itself so that it is no longer a spy. |
| 9389 | overlay->setSpy(false); |
| 9390 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9391 | |
| 9392 | // It continues to receive the rest of the stylus gesture. |
| 9393 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9394 | overlay->consumeMotionMove(); |
| 9395 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9396 | overlay->consumeMotionUp(); |
| 9397 | |
| 9398 | window->assertNoEvents(); |
| 9399 | } |
| 9400 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9401 | struct User { |
| 9402 | int32_t mPid; |
| 9403 | int32_t mUid; |
| 9404 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9405 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9406 | |
| 9407 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 9408 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9409 | |
| 9410 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9411 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9412 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9413 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9414 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9415 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9416 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9417 | } |
| 9418 | |
| 9419 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9420 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9421 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9422 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9423 | mPolicyFlags); |
| 9424 | } |
| 9425 | |
| 9426 | sp<FakeWindowHandle> createWindow() const { |
| 9427 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9428 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9429 | sp<FakeWindowHandle> window = |
| 9430 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9431 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9432 | window->setOwnerInfo(mPid, mUid); |
| 9433 | return window; |
| 9434 | } |
| 9435 | }; |
| 9436 | |
| 9437 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9438 | |
| 9439 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 9440 | auto owner = User(mDispatcher, 10, 11); |
| 9441 | auto window = owner.createWindow(); |
| 9442 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9443 | |
| 9444 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9445 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9446 | window->consumeMotionDown(); |
| 9447 | |
| 9448 | setFocusedWindow(window); |
| 9449 | window->consumeFocusEvent(true); |
| 9450 | |
| 9451 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9452 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9453 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9454 | } |
| 9455 | |
| 9456 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 9457 | auto owner = User(mDispatcher, 10, 11); |
| 9458 | auto window = owner.createWindow(); |
| 9459 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9460 | |
| 9461 | auto rando = User(mDispatcher, 20, 21); |
| 9462 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9463 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9464 | |
| 9465 | setFocusedWindow(window); |
| 9466 | window->consumeFocusEvent(true); |
| 9467 | |
| 9468 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9469 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9470 | window->assertNoEvents(); |
| 9471 | } |
| 9472 | |
| 9473 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 9474 | auto owner = User(mDispatcher, 10, 11); |
| 9475 | auto window = owner.createWindow(); |
| 9476 | auto spy = owner.createWindow(); |
| 9477 | spy->setSpy(true); |
| 9478 | spy->setTrustedOverlay(true); |
| 9479 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9480 | |
| 9481 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9482 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9483 | spy->consumeMotionDown(); |
| 9484 | window->consumeMotionDown(); |
| 9485 | } |
| 9486 | |
| 9487 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 9488 | auto owner = User(mDispatcher, 10, 11); |
| 9489 | auto window = owner.createWindow(); |
| 9490 | |
| 9491 | auto rando = User(mDispatcher, 20, 21); |
| 9492 | auto randosSpy = rando.createWindow(); |
| 9493 | randosSpy->setSpy(true); |
| 9494 | randosSpy->setTrustedOverlay(true); |
| 9495 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9496 | |
| 9497 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9498 | // not receive the event. |
| 9499 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9500 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9501 | randosSpy->assertNoEvents(); |
| 9502 | window->consumeMotionDown(); |
| 9503 | } |
| 9504 | |
| 9505 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 9506 | auto owner = User(mDispatcher, 10, 11); |
| 9507 | auto window = owner.createWindow(); |
| 9508 | |
| 9509 | auto rando = User(mDispatcher, 20, 21); |
| 9510 | auto randosSpy = rando.createWindow(); |
| 9511 | randosSpy->setSpy(true); |
| 9512 | randosSpy->setTrustedOverlay(true); |
| 9513 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9514 | |
| 9515 | // A user that has injection permission can inject into any window. |
| 9516 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9517 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9518 | ADISPLAY_ID_DEFAULT)); |
| 9519 | randosSpy->consumeMotionDown(); |
| 9520 | window->consumeMotionDown(); |
| 9521 | |
| 9522 | setFocusedWindow(randosSpy); |
| 9523 | randosSpy->consumeFocusEvent(true); |
| 9524 | |
| 9525 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9526 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9527 | window->assertNoEvents(); |
| 9528 | } |
| 9529 | |
| 9530 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 9531 | auto owner = User(mDispatcher, 10, 11); |
| 9532 | auto window = owner.createWindow(); |
| 9533 | |
| 9534 | auto rando = User(mDispatcher, 20, 21); |
| 9535 | auto randosWindow = rando.createWindow(); |
| 9536 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9537 | randosWindow->setWatchOutsideTouch(true); |
| 9538 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9539 | |
| 9540 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 9541 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9542 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9543 | window->consumeMotionDown(); |
| 9544 | randosWindow->consumeMotionOutside(); |
| 9545 | } |
| 9546 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9547 | } // namespace android::inputdispatcher |