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 |
| 4595 | mFakePolicy->assertUserActivityPoked(); |
| 4596 | } |
| 4597 | |
| 4598 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 4599 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4600 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4601 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4602 | |
| 4603 | window->setDisableUserActivity(true); |
| 4604 | window->setFocusable(true); |
| 4605 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4606 | setFocusedWindow(window); |
| 4607 | |
| 4608 | window->consumeFocusEvent(true); |
| 4609 | |
| 4610 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4611 | |
| 4612 | // Window should receive key down event. |
| 4613 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4614 | |
| 4615 | // Should have poked user activity |
| 4616 | mFakePolicy->assertUserActivityNotPoked(); |
| 4617 | } |
| 4618 | |
| 4619 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 4620 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4621 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4622 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4623 | |
| 4624 | window->setFocusable(true); |
| 4625 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4626 | setFocusedWindow(window); |
| 4627 | |
| 4628 | window->consumeFocusEvent(true); |
| 4629 | |
| 4630 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4631 | mDispatcher->waitForIdle(); |
| 4632 | |
| 4633 | // System key is not passed down |
| 4634 | window->assertNoEvents(); |
| 4635 | |
| 4636 | // Should have poked user activity |
| 4637 | mFakePolicy->assertUserActivityPoked(); |
| 4638 | } |
| 4639 | |
| 4640 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 4641 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4642 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4643 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4644 | |
| 4645 | window->setFocusable(true); |
| 4646 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4647 | setFocusedWindow(window); |
| 4648 | |
| 4649 | window->consumeFocusEvent(true); |
| 4650 | |
| 4651 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4652 | mDispatcher->waitForIdle(); |
| 4653 | |
| 4654 | // System key is not passed down |
| 4655 | window->assertNoEvents(); |
| 4656 | |
| 4657 | // Should have poked user activity |
| 4658 | mFakePolicy->assertUserActivityPoked(); |
| 4659 | } |
| 4660 | |
| 4661 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 4662 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4663 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4664 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4665 | |
| 4666 | window->setDisableUserActivity(true); |
| 4667 | window->setFocusable(true); |
| 4668 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4669 | setFocusedWindow(window); |
| 4670 | |
| 4671 | window->consumeFocusEvent(true); |
| 4672 | |
| 4673 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 4674 | mDispatcher->waitForIdle(); |
| 4675 | |
| 4676 | // System key is not passed down |
| 4677 | window->assertNoEvents(); |
| 4678 | |
| 4679 | // Should have poked user activity |
| 4680 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4681 | } |
| 4682 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 4683 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 4684 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4685 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4686 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4687 | |
| 4688 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4689 | |
| 4690 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4691 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4692 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 4693 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4694 | |
| 4695 | window->consumeMotionEvent( |
| 4696 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4697 | |
| 4698 | // Should have poked user activity |
| 4699 | mFakePolicy->assertUserActivityPoked(); |
| 4700 | } |
| 4701 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4702 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4704 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4705 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4706 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4707 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4708 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4709 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4710 | mDispatcher->waitForIdle(); |
| 4711 | |
| 4712 | window->assertNoEvents(); |
| 4713 | } |
| 4714 | |
| 4715 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 4716 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4717 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4718 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4719 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4720 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4721 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4722 | |
| 4723 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4724 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4725 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4726 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4727 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4728 | |
| 4729 | // Window should receive only the motion event |
| 4730 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4731 | window->assertNoEvents(); // Key event or focus event will not be received |
| 4732 | } |
| 4733 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4734 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 4735 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4736 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4737 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4738 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4739 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4740 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4741 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4742 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4743 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4744 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4745 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4746 | |
| 4747 | // Add the windows to the dispatcher |
| 4748 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
| 4749 | |
| 4750 | PointF pointInFirst = {300, 200}; |
| 4751 | PointF pointInSecond = {300, 600}; |
| 4752 | |
| 4753 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4754 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4755 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4756 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4757 | // Only the first window should get the down event |
| 4758 | firstWindow->consumeMotionDown(); |
| 4759 | secondWindow->assertNoEvents(); |
| 4760 | |
| 4761 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4762 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4763 | ADISPLAY_ID_DEFAULT, |
| 4764 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4765 | // The first window gets a move and the second a down |
| 4766 | firstWindow->consumeMotionMove(); |
| 4767 | secondWindow->consumeMotionDown(); |
| 4768 | |
| 4769 | // Send pointer cancel to the second window |
| 4770 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4771 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4772 | {pointInFirst, pointInSecond}); |
| 4773 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4774 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4775 | // The first window gets move and the second gets cancel. |
| 4776 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4777 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 4778 | |
| 4779 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4780 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 4781 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 4782 | // The first window gets up and the second gets nothing. |
| 4783 | firstWindow->consumeMotionUp(); |
| 4784 | secondWindow->assertNoEvents(); |
| 4785 | } |
| 4786 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4787 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 4788 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4789 | |
| 4790 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4791 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4792 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4793 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 4794 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 4795 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 4796 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4797 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 4798 | window->assertNoEvents(); |
| 4799 | mDispatcher->waitForIdle(); |
| 4800 | } |
| 4801 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4802 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4803 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4804 | FakeMonitorReceiver(const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4805 | int32_t displayId) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4806 | base::Result<std::unique_ptr<InputChannel>> channel = |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 4807 | dispatcher->createInputMonitor(displayId, name, MONITOR_PID); |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 4808 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4811 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 4812 | |
| 4813 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4814 | mInputReceiver->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 4815 | expectedFlags); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4816 | } |
| 4817 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 4818 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 4819 | |
| 4820 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 4821 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4822 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4823 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4824 | expectedDisplayId, expectedFlags); |
| 4825 | } |
| 4826 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4827 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4828 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4829 | expectedDisplayId, expectedFlags); |
| 4830 | } |
| 4831 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4832 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4833 | mInputReceiver->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4834 | expectedDisplayId, expectedFlags); |
| 4835 | } |
| 4836 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4837 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4838 | mInputReceiver->consumeMotionEvent( |
| 4839 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4840 | WithDisplayId(expectedDisplayId), |
| 4841 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4844 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 4845 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 4846 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4847 | mInputReceiver->consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 4848 | /*expectedFlags=*/0); |
Arthur Hung | fbfa572 | 2021-11-16 02:45:54 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4851 | MotionEvent* consumeMotion() { |
| 4852 | InputEvent* event = mInputReceiver->consume(); |
| 4853 | if (!event) { |
| 4854 | ADD_FAILURE() << "No event was produced"; |
| 4855 | return nullptr; |
| 4856 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 4857 | if (event->getType() != InputEventType::MOTION) { |
| 4858 | ADD_FAILURE() << "Expected MotionEvent, got " << *event; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4859 | return nullptr; |
| 4860 | } |
| 4861 | return static_cast<MotionEvent*>(event); |
| 4862 | } |
| 4863 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4864 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 4865 | |
| 4866 | private: |
| 4867 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4868 | }; |
| 4869 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4870 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 4871 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4872 | /** |
| 4873 | * Two entities that receive touch: A window, and a global monitor. |
| 4874 | * The touch goes to the window, and then the window disappears. |
| 4875 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 4876 | * for the monitor, as well. |
| 4877 | * 1. foregroundWindow |
| 4878 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 4879 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4880 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4881 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4882 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4883 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4884 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4885 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 4886 | |
| 4887 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4889 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4890 | {100, 200})) |
| 4891 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4892 | |
| 4893 | // Both the foreground window and the global monitor should receive the touch down |
| 4894 | window->consumeMotionDown(); |
| 4895 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4896 | |
| 4897 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4898 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4899 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 4900 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4901 | |
| 4902 | window->consumeMotionMove(); |
| 4903 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4904 | |
| 4905 | // Now the foreground window goes away |
| 4906 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 4907 | window->consumeMotionCancel(); |
| 4908 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 4909 | |
| 4910 | // If more events come in, there will be no more foreground window to send them to. This will |
| 4911 | // cause a cancel for the monitor, as well. |
| 4912 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 4913 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4914 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 4915 | << "Injection should fail because the window was removed"; |
| 4916 | window->assertNoEvents(); |
| 4917 | // Global monitor now gets the cancel |
| 4918 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 4919 | } |
| 4920 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4921 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4922 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4923 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4924 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4925 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4926 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4927 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4928 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4929 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4930 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4931 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4932 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4933 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4936 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
| 4937 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4938 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4939 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4940 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4941 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4942 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4943 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4944 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4945 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4946 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 4947 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4948 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4949 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4950 | // Pilfer pointers from the monitor. |
| 4951 | // This should not do anything and the window should continue to receive events. |
| 4952 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4953 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4954 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4955 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4956 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4957 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4958 | |
| 4959 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 4960 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4963 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4964 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4965 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4966 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4967 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 4968 | window->setWindowOffset(20, 40); |
| 4969 | window->setWindowTransform(0, 1, -1, 0); |
| 4970 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4971 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 4972 | |
| 4973 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4974 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 4975 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4976 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4977 | MotionEvent* event = monitor.consumeMotion(); |
| 4978 | // Even though window has transform, gesture monitor must not. |
| 4979 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 4980 | } |
| 4981 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4982 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4983 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4984 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4985 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4986 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4987 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 4988 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 4989 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4992 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4993 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4994 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4995 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4996 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 4997 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 4998 | |
| 4999 | NotifyMotionArgs motionArgs = |
| 5000 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5001 | ADISPLAY_ID_DEFAULT); |
| 5002 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5003 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5004 | // Window should receive motion down event. |
| 5005 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5006 | |
| 5007 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5008 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5009 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5010 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5011 | motionArgs.pointerCoords[0].getX() - 10); |
| 5012 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5013 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5014 | window->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5015 | /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 5016 | } |
| 5017 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5018 | /** |
| 5019 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 5020 | * the device default right away. In the test scenario, we check both the default value, |
| 5021 | * and the action of enabling / disabling. |
| 5022 | */ |
| 5023 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5024 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5025 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5026 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5027 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5028 | |
| 5029 | // Set focused application. |
| 5030 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5031 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5032 | |
| 5033 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5034 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5035 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5036 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5037 | |
| 5038 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5039 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5040 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5041 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5042 | |
| 5043 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5044 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5045 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5046 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5047 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5048 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5049 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5050 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5051 | |
| 5052 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5053 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5054 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5055 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5056 | |
| 5057 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 5058 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5059 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 5060 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5061 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5062 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5063 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5064 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5065 | |
| 5066 | window->assertNoEvents(); |
| 5067 | } |
| 5068 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5069 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5070 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5071 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5072 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5073 | |
| 5074 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5075 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5076 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5077 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5078 | setFocusedWindow(window); |
| 5079 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5080 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5081 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5082 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 5083 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5084 | |
| 5085 | InputEvent* event = window->consume(); |
| 5086 | ASSERT_NE(event, nullptr); |
| 5087 | |
| 5088 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5089 | ASSERT_NE(verified, nullptr); |
| 5090 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 5091 | |
| 5092 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 5093 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 5094 | ASSERT_EQ(keyArgs.source, verified->source); |
| 5095 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 5096 | |
| 5097 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 5098 | |
| 5099 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5100 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5101 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 5102 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 5103 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 5104 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 5105 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 5106 | } |
| 5107 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5108 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5109 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5110 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5111 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5112 | |
| 5113 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5114 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5115 | ui::Transform transform; |
| 5116 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5117 | |
| 5118 | gui::DisplayInfo displayInfo; |
| 5119 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5120 | displayInfo.transform = transform; |
| 5121 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5122 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5123 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5124 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5125 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5126 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5127 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5128 | |
| 5129 | InputEvent* event = window->consume(); |
| 5130 | ASSERT_NE(event, nullptr); |
| 5131 | |
| 5132 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 5133 | ASSERT_NE(verified, nullptr); |
| 5134 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 5135 | |
| 5136 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 5137 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 5138 | EXPECT_EQ(motionArgs.source, verified->source); |
| 5139 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 5140 | |
| 5141 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 5142 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 5143 | const vec2 rawXY = |
| 5144 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 5145 | motionArgs.pointerCoords[0].getXYValue()); |
| 5146 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 5147 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5148 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5149 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 5150 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 5151 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 5152 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 5153 | } |
| 5154 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 5155 | /** |
| 5156 | * Ensure that separate calls to sign the same data are generating the same key. |
| 5157 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 5158 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 5159 | * tests. |
| 5160 | */ |
| 5161 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 5162 | KeyEvent event = getTestKeyEvent(); |
| 5163 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5164 | |
| 5165 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 5166 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 5167 | ASSERT_EQ(hmac1, hmac2); |
| 5168 | } |
| 5169 | |
| 5170 | /** |
| 5171 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 5172 | */ |
| 5173 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 5174 | KeyEvent event = getTestKeyEvent(); |
| 5175 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 5176 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 5177 | |
| 5178 | verifiedEvent.deviceId += 1; |
| 5179 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5180 | |
| 5181 | verifiedEvent.source += 1; |
| 5182 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5183 | |
| 5184 | verifiedEvent.eventTimeNanos += 1; |
| 5185 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5186 | |
| 5187 | verifiedEvent.displayId += 1; |
| 5188 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5189 | |
| 5190 | verifiedEvent.action += 1; |
| 5191 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5192 | |
| 5193 | verifiedEvent.downTimeNanos += 1; |
| 5194 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5195 | |
| 5196 | verifiedEvent.flags += 1; |
| 5197 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5198 | |
| 5199 | verifiedEvent.keyCode += 1; |
| 5200 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5201 | |
| 5202 | verifiedEvent.scanCode += 1; |
| 5203 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5204 | |
| 5205 | verifiedEvent.metaState += 1; |
| 5206 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5207 | |
| 5208 | verifiedEvent.repeatCount += 1; |
| 5209 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 5210 | } |
| 5211 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5212 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 5213 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5214 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5215 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5216 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5217 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5218 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5219 | |
| 5220 | // Top window is also focusable but is not granted focus. |
| 5221 | windowTop->setFocusable(true); |
| 5222 | windowSecond->setFocusable(true); |
| 5223 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5224 | setFocusedWindow(windowSecond); |
| 5225 | |
| 5226 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5227 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5228 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5229 | |
| 5230 | // Focused window should receive event. |
| 5231 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5232 | windowTop->assertNoEvents(); |
| 5233 | } |
| 5234 | |
| 5235 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 5236 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5237 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5238 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5239 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5240 | |
| 5241 | window->setFocusable(true); |
| 5242 | // Release channel for window is no longer valid. |
| 5243 | window->releaseChannel(); |
| 5244 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5245 | setFocusedWindow(window); |
| 5246 | |
| 5247 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5248 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5249 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5250 | |
| 5251 | // window channel is invalid, so it should not receive any input event. |
| 5252 | window->assertNoEvents(); |
| 5253 | } |
| 5254 | |
| 5255 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 5256 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5257 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5258 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5259 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5260 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5261 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5262 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5263 | setFocusedWindow(window); |
| 5264 | |
| 5265 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5266 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 5267 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5268 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5269 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5270 | window->assertNoEvents(); |
| 5271 | } |
| 5272 | |
| 5273 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 5274 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5275 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5276 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5277 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5278 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5279 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5280 | |
| 5281 | windowTop->setFocusable(true); |
| 5282 | windowSecond->setFocusable(true); |
| 5283 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 5284 | setFocusedWindow(windowTop); |
| 5285 | windowTop->consumeFocusEvent(true); |
| 5286 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5287 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
| 5288 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5289 | windowSecond->consumeFocusEvent(true); |
| 5290 | windowTop->consumeFocusEvent(false); |
| 5291 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5292 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5293 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5294 | |
| 5295 | // Focused window should receive event. |
| 5296 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 5297 | } |
| 5298 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5299 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5300 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5301 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5302 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5303 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5304 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5305 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5306 | |
| 5307 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5308 | windowSecond->setFocusable(false); |
| 5309 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5310 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5311 | setFocusedWindow(windowTop); |
| 5312 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5313 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5314 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5315 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5316 | |
| 5317 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 5318 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5319 | windowSecond->assertNoEvents(); |
| 5320 | } |
| 5321 | |
| 5322 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 5323 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5324 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5325 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5326 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5327 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 5328 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5329 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5330 | |
| 5331 | window->setFocusable(true); |
| 5332 | previousFocusedWindow->setFocusable(true); |
| 5333 | window->setVisible(false); |
| 5334 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 5335 | setFocusedWindow(previousFocusedWindow); |
| 5336 | previousFocusedWindow->consumeFocusEvent(true); |
| 5337 | |
| 5338 | // Requesting focus on invisible window takes focus from currently focused window. |
| 5339 | setFocusedWindow(window); |
| 5340 | previousFocusedWindow->consumeFocusEvent(false); |
| 5341 | |
| 5342 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5343 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5344 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 5345 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5346 | |
| 5347 | // Window does not get focus event or key down. |
| 5348 | window->assertNoEvents(); |
| 5349 | |
| 5350 | // Window becomes visible. |
| 5351 | window->setVisible(true); |
| 5352 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5353 | |
| 5354 | // Window receives focus event. |
| 5355 | window->consumeFocusEvent(true); |
| 5356 | // Focused window receives key down. |
| 5357 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5358 | } |
| 5359 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5360 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 5361 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5362 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5363 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 5364 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5365 | |
| 5366 | // window is granted focus. |
| 5367 | window->setFocusable(true); |
| 5368 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 5369 | setFocusedWindow(window); |
| 5370 | window->consumeFocusEvent(true); |
| 5371 | |
| 5372 | // When a display is removed window loses focus. |
| 5373 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 5374 | window->consumeFocusEvent(false); |
| 5375 | } |
| 5376 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5377 | /** |
| 5378 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 5379 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 5380 | * of the 'slipperyEnterWindow'. |
| 5381 | * |
| 5382 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 5383 | * a way so that the touched location is no longer covered by the top window. |
| 5384 | * |
| 5385 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 5386 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 5387 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 5388 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 5389 | * with ACTION_DOWN). |
| 5390 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 5391 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 5392 | * |
| 5393 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 5394 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 5395 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 5396 | * |
| 5397 | * In this test, we ensure that the event received by the bottom window has |
| 5398 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 5399 | */ |
| 5400 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 5401 | constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; |
| 5402 | constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5403 | |
| 5404 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5405 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5406 | |
| 5407 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5408 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 5409 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5410 | // Make sure this one overlaps the bottom window |
| 5411 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 5412 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 5413 | // one. Windows with the same owner are not considered to be occluding each other. |
| 5414 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 5415 | |
| 5416 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5417 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5418 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5419 | |
| 5420 | mDispatcher->setInputWindows( |
| 5421 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5422 | |
| 5423 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5424 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5425 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5426 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5427 | slipperyExitWindow->consumeMotionDown(); |
| 5428 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
| 5429 | mDispatcher->setInputWindows( |
| 5430 | {{ADISPLAY_ID_DEFAULT, {slipperyExitWindow, slipperyEnterWindow}}}); |
| 5431 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5432 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 5433 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5434 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5435 | |
| 5436 | slipperyExitWindow->consumeMotionCancel(); |
| 5437 | |
| 5438 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5439 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5440 | } |
| 5441 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 5442 | /** |
| 5443 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 5444 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 5445 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 5446 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 5447 | * reproduces a crash. |
| 5448 | */ |
| 5449 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 5450 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5451 | |
| 5452 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 5453 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 5454 | leftSlipperyWindow->setSlippery(true); |
| 5455 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5456 | |
| 5457 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 5458 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 5459 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 5460 | rightDropTouchesWindow->setDropInput(true); |
| 5461 | |
| 5462 | mDispatcher->setInputWindows( |
| 5463 | {{ADISPLAY_ID_DEFAULT, {leftSlipperyWindow, rightDropTouchesWindow}}}); |
| 5464 | |
| 5465 | // Start touch in the left window |
| 5466 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5467 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5468 | .build()); |
| 5469 | leftSlipperyWindow->consumeMotionDown(); |
| 5470 | |
| 5471 | // And move it into the right window |
| 5472 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5473 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5474 | .build()); |
| 5475 | |
| 5476 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 5477 | // The left window continues to receive the gesture. |
| 5478 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5479 | rightDropTouchesWindow->assertNoEvents(); |
| 5480 | } |
| 5481 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5482 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 5483 | protected: |
| 5484 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 5485 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 5486 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5487 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5488 | sp<FakeWindowHandle> mWindow; |
| 5489 | |
| 5490 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5491 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5492 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 5493 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Prabir Pradhan | 87112a7 | 2023-04-20 19:13:39 +0000 | [diff] [blame] | 5494 | mDispatcher->requestRefreshConfiguration(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5495 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 5496 | ASSERT_EQ(OK, mDispatcher->start()); |
| 5497 | |
| 5498 | setUpWindow(); |
| 5499 | } |
| 5500 | |
| 5501 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5502 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5503 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5504 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5505 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5506 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5507 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5508 | mWindow->consumeFocusEvent(true); |
| 5509 | } |
| 5510 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5511 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5512 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5513 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5514 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5515 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5516 | |
| 5517 | // Window should receive key down event. |
| 5518 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5519 | } |
| 5520 | |
| 5521 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 5522 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 5523 | InputEvent* repeatEvent = mWindow->consume(); |
| 5524 | ASSERT_NE(nullptr, repeatEvent); |
| 5525 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5526 | ASSERT_EQ(InputEventType::KEY, repeatEvent->getType()); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5527 | |
| 5528 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 5529 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 5530 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 5531 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 5532 | } |
| 5533 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5534 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5535 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5536 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5537 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5538 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5539 | |
| 5540 | // Window should receive key down event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5541 | mWindow->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5542 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5543 | } |
| 5544 | }; |
| 5545 | |
| 5546 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5547 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5548 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5549 | expectKeyRepeatOnce(repeatCount); |
| 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5554 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5555 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5556 | expectKeyRepeatOnce(repeatCount); |
| 5557 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5558 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5559 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5560 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5561 | expectKeyRepeatOnce(repeatCount); |
| 5562 | } |
| 5563 | } |
| 5564 | |
| 5565 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5566 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5567 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5568 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5569 | mWindow->assertNoEvents(); |
| 5570 | } |
| 5571 | |
| 5572 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5573 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5574 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5575 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5576 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5577 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5578 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5579 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5580 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 5581 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5582 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5583 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5584 | mWindow->assertNoEvents(); |
| 5585 | } |
| 5586 | |
| 5587 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5588 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 5589 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 5590 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 5591 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5592 | // 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] | 5593 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 5594 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5595 | mWindow->assertNoEvents(); |
| 5596 | } |
| 5597 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5598 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 5599 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5600 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5601 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 5602 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 5603 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 5604 | mWindow->assertNoEvents(); |
| 5605 | } |
| 5606 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5607 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5608 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5609 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5610 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5611 | InputEvent* repeatEvent = mWindow->consume(); |
| 5612 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5613 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 5614 | IdGenerator::getSource(repeatEvent->getId())); |
| 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 5619 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5620 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 5621 | |
| 5622 | std::unordered_set<int32_t> idSet; |
| 5623 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 5624 | InputEvent* repeatEvent = mWindow->consume(); |
| 5625 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 5626 | int32_t id = repeatEvent->getId(); |
| 5627 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 5628 | idSet.insert(id); |
| 5629 | } |
| 5630 | } |
| 5631 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5632 | /* Test InputDispatcher for MultiDisplay */ |
| 5633 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 5634 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5635 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5636 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5637 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5638 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5639 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5640 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5641 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5642 | // Set focus window for primary display, but focused display would be second one. |
| 5643 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5644 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5645 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5646 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5647 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5648 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5649 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5650 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5651 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5652 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5653 | // Set focus display to second one. |
| 5654 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 5655 | // Set focus window for second display. |
| 5656 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5657 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5658 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5659 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5660 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5661 | } |
| 5662 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 5663 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5664 | InputDispatcherTest::TearDown(); |
| 5665 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5666 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5667 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5668 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5669 | windowInSecondary.clear(); |
| 5670 | } |
| 5671 | |
| 5672 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5673 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5674 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5675 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5676 | sp<FakeWindowHandle> windowInSecondary; |
| 5677 | }; |
| 5678 | |
| 5679 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 5680 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5682 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5683 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5684 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5685 | windowInSecondary->assertNoEvents(); |
| 5686 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5687 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5688 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5689 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5690 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5691 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5692 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5693 | } |
| 5694 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5695 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5696 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5697 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5698 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5699 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5700 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 5701 | windowInSecondary->assertNoEvents(); |
| 5702 | |
| 5703 | // Test inject a key down without display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5705 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5706 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5707 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5708 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 5709 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 5710 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5711 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5712 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5713 | windowInSecondary->consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5714 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5715 | |
| 5716 | // Test inject a key down, should timeout because of no target window. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 5717 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDownNoRepeat(mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5718 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5719 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5720 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 5721 | windowInSecondary->assertNoEvents(); |
| 5722 | } |
| 5723 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5724 | // Test per-display input monitors for motion event. |
| 5725 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5726 | FakeMonitorReceiver monitorInPrimary = |
| 5727 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5728 | FakeMonitorReceiver monitorInSecondary = |
| 5729 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5730 | |
| 5731 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5732 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5733 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5734 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5735 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5736 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5737 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5738 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5739 | |
| 5740 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5741 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5742 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5743 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5744 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5745 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5746 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5747 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5748 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 5749 | // Lift up the touch from the second display |
| 5750 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5751 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5752 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5753 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5754 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 5755 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5756 | // Test inject a non-pointer motion event. |
| 5757 | // If specific a display, it will dispatch to the focused window of particular display, |
| 5758 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5759 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5760 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 5761 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5762 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5763 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5764 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5765 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | // Test per-display input monitors for key event. |
| 5769 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5770 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5771 | FakeMonitorReceiver monitorInPrimary = |
| 5772 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5773 | FakeMonitorReceiver monitorInSecondary = |
| 5774 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5775 | |
| 5776 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5777 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 5778 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5779 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5780 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 5781 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5782 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 5783 | } |
| 5784 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5785 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 5786 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5787 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5788 | secondWindowInPrimary->setFocusable(true); |
| 5789 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 5790 | setFocusedWindow(secondWindowInPrimary); |
| 5791 | windowInPrimary->consumeFocusEvent(false); |
| 5792 | secondWindowInPrimary->consumeFocusEvent(true); |
| 5793 | |
| 5794 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 5796 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5797 | windowInPrimary->assertNoEvents(); |
| 5798 | windowInSecondary->assertNoEvents(); |
| 5799 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5800 | } |
| 5801 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 5802 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 5803 | FakeMonitorReceiver monitorInPrimary = |
| 5804 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5805 | FakeMonitorReceiver monitorInSecondary = |
| 5806 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 5807 | |
| 5808 | // Test touch down on primary display. |
| 5809 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5810 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 5811 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5812 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5813 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5814 | |
| 5815 | // Test touch down on second display. |
| 5816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5817 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 5818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5819 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5820 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 5821 | |
| 5822 | // Trigger cancel touch. |
| 5823 | mDispatcher->cancelCurrentTouch(); |
| 5824 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5825 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5826 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5827 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5828 | |
| 5829 | // Test inject a move motion event, no window/monitor should receive the event. |
| 5830 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5831 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5832 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5833 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5834 | windowInPrimary->assertNoEvents(); |
| 5835 | monitorInPrimary.assertNoEvents(); |
| 5836 | |
| 5837 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5838 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5839 | SECOND_DISPLAY_ID, {110, 200})) |
| 5840 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 5841 | windowInSecondary->assertNoEvents(); |
| 5842 | monitorInSecondary.assertNoEvents(); |
| 5843 | } |
| 5844 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5845 | class InputFilterTest : public InputDispatcherTest { |
| 5846 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5847 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 5848 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5849 | NotifyMotionArgs motionArgs; |
| 5850 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5851 | motionArgs = |
| 5852 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5853 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5854 | motionArgs = |
| 5855 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5856 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5857 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5858 | if (expectToBeFiltered) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5859 | const auto xy = transform.transform(motionArgs.pointerCoords->getXYValue()); |
| 5860 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5861 | } else { |
| 5862 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5863 | } |
| 5864 | } |
| 5865 | |
| 5866 | void testNotifyKey(bool expectToBeFiltered) { |
| 5867 | NotifyKeyArgs keyArgs; |
| 5868 | |
| 5869 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5870 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5871 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5872 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5873 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5874 | |
| 5875 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 5876 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 5877 | } else { |
| 5878 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 5879 | } |
| 5880 | } |
| 5881 | }; |
| 5882 | |
| 5883 | // Test InputFilter for MotionEvent |
| 5884 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 5885 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 5886 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5887 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5888 | |
| 5889 | // Enable InputFilter |
| 5890 | mDispatcher->setInputFilterEnabled(true); |
| 5891 | // Test touch on both primary and second display, and check if both events are filtered. |
| 5892 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 5893 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 5894 | |
| 5895 | // Disable InputFilter |
| 5896 | mDispatcher->setInputFilterEnabled(false); |
| 5897 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 5898 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 5899 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 5900 | } |
| 5901 | |
| 5902 | // Test InputFilter for KeyEvent |
| 5903 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 5904 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 5905 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5906 | |
| 5907 | // Enable InputFilter |
| 5908 | mDispatcher->setInputFilterEnabled(true); |
| 5909 | // Send a key event, and check if it is filtered. |
| 5910 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 5911 | |
| 5912 | // Disable InputFilter |
| 5913 | mDispatcher->setInputFilterEnabled(false); |
| 5914 | // Send a key event, and check if it isn't filtered. |
| 5915 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 5916 | } |
| 5917 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5918 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 5919 | // logical display coordinate space. |
| 5920 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 5921 | ui::Transform firstDisplayTransform; |
| 5922 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5923 | ui::Transform secondDisplayTransform; |
| 5924 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 5925 | |
| 5926 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 5927 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 5928 | displayInfos[0].transform = firstDisplayTransform; |
| 5929 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 5930 | displayInfos[1].transform = secondDisplayTransform; |
| 5931 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5932 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 5933 | |
| 5934 | // Enable InputFilter |
| 5935 | mDispatcher->setInputFilterEnabled(true); |
| 5936 | |
| 5937 | // Ensure the correct transforms are used for the displays. |
| 5938 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true, firstDisplayTransform); |
| 5939 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true, secondDisplayTransform); |
| 5940 | } |
| 5941 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5942 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 5943 | protected: |
| 5944 | virtual void SetUp() override { |
| 5945 | InputDispatcherTest::SetUp(); |
| 5946 | |
| 5947 | /** |
| 5948 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 5949 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 5950 | * on. |
| 5951 | */ |
| 5952 | mDispatcher->setInputFilterEnabled(true); |
| 5953 | |
| 5954 | std::shared_ptr<InputApplicationHandle> application = |
| 5955 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5956 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 5957 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5958 | |
| 5959 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 5960 | mWindow->setFocusable(true); |
| 5961 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 5962 | setFocusedWindow(mWindow); |
| 5963 | mWindow->consumeFocusEvent(true); |
| 5964 | } |
| 5965 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5966 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5967 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5968 | KeyEvent event; |
| 5969 | |
| 5970 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5971 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 5972 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5973 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5974 | const int32_t additionalPolicyFlags = |
| 5975 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 5976 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5977 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 5978 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 5979 | policyFlags | additionalPolicyFlags)); |
| 5980 | |
| 5981 | InputEvent* received = mWindow->consume(); |
| 5982 | ASSERT_NE(nullptr, received); |
| 5983 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 5984 | ASSERT_EQ(received->getType(), InputEventType::KEY); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 5985 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*received); |
| 5986 | ASSERT_EQ(flags, keyEvent.getFlags()); |
| 5987 | } |
| 5988 | |
| 5989 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 5990 | int32_t flags) { |
| 5991 | MotionEvent event; |
| 5992 | PointerProperties pointerProperties[1]; |
| 5993 | PointerCoords pointerCoords[1]; |
| 5994 | pointerProperties[0].clear(); |
| 5995 | pointerProperties[0].id = 0; |
| 5996 | pointerCoords[0].clear(); |
| 5997 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 5998 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 5999 | |
| 6000 | ui::Transform identityTransform; |
| 6001 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6002 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 6003 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 6004 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 6005 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 6006 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 6007 | eventTime, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6008 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 6009 | |
| 6010 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 6011 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6012 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6013 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, |
| 6014 | policyFlags | additionalPolicyFlags)); |
| 6015 | |
| 6016 | InputEvent* received = mWindow->consume(); |
| 6017 | ASSERT_NE(nullptr, received); |
| 6018 | ASSERT_EQ(resolvedDeviceId, received->getDeviceId()); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6019 | ASSERT_EQ(received->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6020 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*received); |
| 6021 | ASSERT_EQ(flags, motionEvent.getFlags()); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6022 | } |
| 6023 | |
| 6024 | private: |
| 6025 | sp<FakeWindowHandle> mWindow; |
| 6026 | }; |
| 6027 | |
| 6028 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6029 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 6030 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 6031 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6032 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 6033 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6036 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6037 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6038 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6039 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 6040 | } |
| 6041 | |
| 6042 | TEST_F(InputFilterInjectionPolicyTest, |
| 6043 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 6044 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6045 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 6046 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6050 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 6051 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6054 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6055 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6056 | InputDispatcherTest::SetUp(); |
| 6057 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6058 | std::shared_ptr<FakeApplicationHandle> application = |
| 6059 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6060 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6061 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6062 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6063 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6064 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6065 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6066 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6067 | |
| 6068 | // Set focused application. |
| 6069 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6070 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6071 | |
| 6072 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6073 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6074 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6075 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6076 | } |
| 6077 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 6078 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6079 | InputDispatcherTest::TearDown(); |
| 6080 | |
| 6081 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6082 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6083 | } |
| 6084 | |
| 6085 | protected: |
| 6086 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6087 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6088 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6089 | }; |
| 6090 | |
| 6091 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6092 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 6093 | // the onPointerDownOutsideFocus callback. |
| 6094 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6095 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6096 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6097 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6098 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6099 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6100 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6101 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6102 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 6103 | } |
| 6104 | |
| 6105 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 6106 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 6107 | // onPointerDownOutsideFocus callback. |
| 6108 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6109 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6110 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6111 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6112 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6113 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6114 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6115 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6116 | } |
| 6117 | |
| 6118 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 6119 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 6120 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6121 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6122 | injectKeyDownNoRepeat(mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6123 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6124 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6125 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6126 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6127 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6128 | } |
| 6129 | |
| 6130 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 6131 | // DOWN on the window that already has focus. Ensure no window received the |
| 6132 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6133 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 6135 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 6136 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6137 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 6138 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6139 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 6140 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6141 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 6142 | } |
| 6143 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6144 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 6145 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 6146 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 6147 | const MotionEvent event = |
| 6148 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6149 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 6150 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 6151 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 6152 | .build(); |
| 6153 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event)) |
| 6154 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6155 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6156 | |
| 6157 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6158 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 6159 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 6160 | mUnfocusedWindow->assertNoEvents(); |
| 6161 | } |
| 6162 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6163 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 6164 | // windows that point to the same client token. |
| 6165 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 6166 | virtual void SetUp() override { |
| 6167 | InputDispatcherTest::SetUp(); |
| 6168 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6169 | std::shared_ptr<FakeApplicationHandle> application = |
| 6170 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6171 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 6172 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6173 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 6174 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6175 | mWindow2 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 2", |
| 6176 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6177 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 6178 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 6179 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6180 | } |
| 6181 | |
| 6182 | protected: |
| 6183 | sp<FakeWindowHandle> mWindow1; |
| 6184 | sp<FakeWindowHandle> mWindow2; |
| 6185 | |
| 6186 | // 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] | 6187 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 6188 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 6189 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 6193 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6194 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6195 | InputEvent* event = window->consume(); |
| 6196 | |
| 6197 | ASSERT_NE(nullptr, event) << name.c_str() |
| 6198 | << ": consumer should have returned non-NULL event."; |
| 6199 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6200 | ASSERT_EQ(InputEventType::MOTION, event->getType()) |
| 6201 | << name.c_str() << ": expected MotionEvent, got " << *event; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6202 | |
| 6203 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6204 | assertMotionAction(expectedAction, motionEvent.getAction()); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 6205 | ASSERT_EQ(points.size(), motionEvent.getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6206 | |
| 6207 | for (size_t i = 0; i < points.size(); i++) { |
| 6208 | float expectedX = points[i].x; |
| 6209 | float expectedY = points[i].y; |
| 6210 | |
| 6211 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 6212 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 6213 | << ", got " << motionEvent.getX(i); |
| 6214 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 6215 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 6216 | << ", got " << motionEvent.getY(i); |
| 6217 | } |
| 6218 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6219 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6220 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6221 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6222 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 6223 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6224 | |
| 6225 | // Always consume from window1 since it's the window that has the InputReceiver |
| 6226 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 6227 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6228 | }; |
| 6229 | |
| 6230 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 6231 | // Touch Window 1 |
| 6232 | PointF touchedPoint = {10, 10}; |
| 6233 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6234 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6235 | |
| 6236 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6237 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6238 | |
| 6239 | // Touch Window 2 |
| 6240 | touchedPoint = {150, 150}; |
| 6241 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6242 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6243 | } |
| 6244 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6245 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 6246 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6247 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6248 | |
| 6249 | // Touch Window 1 |
| 6250 | PointF touchedPoint = {10, 10}; |
| 6251 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6252 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6253 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6254 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6255 | |
| 6256 | // Touch Window 2 |
| 6257 | touchedPoint = {150, 150}; |
| 6258 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6259 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 6260 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6261 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6262 | // Update the transform so rotation is set |
| 6263 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6264 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 6265 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 6266 | } |
| 6267 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6268 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6269 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6270 | |
| 6271 | // Touch Window 1 |
| 6272 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6273 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6274 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6275 | |
| 6276 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6277 | touchedPoints.push_back(PointF{150, 150}); |
| 6278 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6279 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6280 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6281 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6282 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6283 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6284 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6285 | // Update the transform so rotation is set for Window 2 |
| 6286 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6287 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6288 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6289 | } |
| 6290 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6291 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6292 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 6293 | |
| 6294 | // Touch Window 1 |
| 6295 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6296 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6297 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6298 | |
| 6299 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6300 | touchedPoints.push_back(PointF{150, 150}); |
| 6301 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6302 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6303 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6304 | |
| 6305 | // Move both windows |
| 6306 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6307 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6308 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6309 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6310 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6311 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6312 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6313 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6314 | expectedPoints.pop_back(); |
| 6315 | |
| 6316 | // Touch Window 2 |
| 6317 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 6318 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6319 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6320 | |
| 6321 | // Move both windows |
| 6322 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6323 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6324 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6325 | |
| 6326 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6327 | } |
| 6328 | |
| 6329 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 6330 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 6331 | |
| 6332 | // Touch Window 1 |
| 6333 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 6334 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6335 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6336 | |
| 6337 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6338 | touchedPoints.push_back(PointF{150, 150}); |
| 6339 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6340 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6341 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6342 | |
| 6343 | // Move both windows |
| 6344 | touchedPoints = {{20, 20}, {175, 175}}; |
| 6345 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 6346 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 6347 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 6348 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 6351 | /** |
| 6352 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 6353 | * same input channel. |
| 6354 | */ |
| 6355 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 6356 | mWindow1->setSlippery(true); |
| 6357 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
| 6358 | |
| 6359 | // Touch down in window 1 |
| 6360 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6361 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 6362 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 6363 | |
| 6364 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 6365 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 6366 | // getting generated. |
| 6367 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6368 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 6369 | |
| 6370 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 6371 | } |
| 6372 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6373 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 6374 | virtual void SetUp() override { |
| 6375 | InputDispatcherTest::SetUp(); |
| 6376 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6377 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6378 | mApplication->setDispatchingTimeout(20ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6379 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 6380 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6381 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6382 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6383 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6384 | |
| 6385 | // Set focused application. |
| 6386 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 6387 | |
| 6388 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6389 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6390 | mWindow->consumeFocusEvent(true); |
| 6391 | } |
| 6392 | |
| 6393 | virtual void TearDown() override { |
| 6394 | InputDispatcherTest::TearDown(); |
| 6395 | mWindow.clear(); |
| 6396 | } |
| 6397 | |
| 6398 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6399 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6400 | sp<FakeWindowHandle> mWindow; |
| 6401 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 6402 | |
| 6403 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6404 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6405 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6406 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6407 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6408 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6409 | WINDOW_LOCATION)); |
| 6410 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6411 | |
| 6412 | sp<FakeWindowHandle> addSpyWindow() { |
| 6413 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6414 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6415 | spy->setTrustedOverlay(true); |
| 6416 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 6417 | spy->setSpy(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6418 | spy->setDispatchingTimeout(30ms); |
| 6419 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, mWindow}}}); |
| 6420 | return spy; |
| 6421 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6422 | }; |
| 6423 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6424 | // Send a tap and respond, which should not cause an ANR. |
| 6425 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 6426 | tapOnWindow(); |
| 6427 | mWindow->consumeMotionDown(); |
| 6428 | mWindow->consumeMotionUp(); |
| 6429 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6430 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6431 | } |
| 6432 | |
| 6433 | // Send a regular key and respond, which should not cause an ANR. |
| 6434 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6435 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6436 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6437 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6438 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6439 | } |
| 6440 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6441 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 6442 | mWindow->setFocusable(false); |
| 6443 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6444 | mWindow->consumeFocusEvent(false); |
| 6445 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6446 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6447 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6448 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 6449 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6450 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 6451 | // Key will not go to window because we have no focused window. |
| 6452 | // The 'no focused window' ANR timer should start instead. |
| 6453 | |
| 6454 | // Now, the focused application goes away. |
| 6455 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 6456 | // The key should get dropped and there should be no ANR. |
| 6457 | |
| 6458 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6459 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6460 | } |
| 6461 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6462 | // 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] | 6463 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 6464 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6465 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6466 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6467 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6468 | WINDOW_LOCATION)); |
| 6469 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6470 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 6471 | ASSERT_TRUE(sequenceNum); |
| 6472 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6473 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6474 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6475 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6476 | mWindow->consumeMotionEvent( |
| 6477 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6478 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6479 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | // Send a key to the app and have the app not respond right away. |
| 6483 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 6484 | // Inject a key, and don't respond - expect that ANR is called. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 6485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6486 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 6487 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6488 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6489 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 6490 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6491 | } |
| 6492 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6493 | // We have a focused application, but no focused window |
| 6494 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6495 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6496 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6497 | mWindow->consumeFocusEvent(false); |
| 6498 | |
| 6499 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6501 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6502 | WINDOW_LOCATION)); |
| 6503 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 6504 | mDispatcher->waitForIdle(); |
| 6505 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6506 | |
| 6507 | // Once a focused event arrives, we get an ANR for this application |
| 6508 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6509 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6510 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6511 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6512 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6513 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6514 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6515 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6516 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6517 | } |
| 6518 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6519 | /** |
| 6520 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 6521 | * there will not be an ANR. |
| 6522 | */ |
| 6523 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 6524 | mWindow->setFocusable(false); |
| 6525 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6526 | mWindow->consumeFocusEvent(false); |
| 6527 | |
| 6528 | KeyEvent event; |
| 6529 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 6530 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 6531 | |
| 6532 | // Define a valid key down event that is stale (too old). |
| 6533 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 6534 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6535 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6536 | |
| 6537 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 6538 | |
| 6539 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6540 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 6541 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 6542 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 6543 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 6544 | << "Injection should fail because the event is stale"; |
| 6545 | |
| 6546 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6547 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6548 | mWindow->assertNoEvents(); |
| 6549 | } |
| 6550 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6551 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6552 | // Make sure that we don't notify policy twice about the same ANR. |
| 6553 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6554 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6555 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6556 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6557 | |
| 6558 | // Once a focused event arrives, we get an ANR for this application |
| 6559 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 6560 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6561 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6562 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6563 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6564 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6565 | const std::chrono::duration appTimeout = |
| 6566 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6567 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6568 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6569 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6570 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 6571 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6572 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6573 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 6574 | // '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] | 6575 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6576 | } |
| 6577 | |
| 6578 | // We have a focused application, but no focused window |
| 6579 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
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); |
| 6583 | |
| 6584 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6585 | const InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6586 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6587 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6588 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6589 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 6590 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 6591 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6592 | |
| 6593 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6594 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6595 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6596 | mWindow->assertNoEvents(); |
| 6597 | } |
| 6598 | |
| 6599 | /** |
| 6600 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 6601 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 6602 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 6603 | * the ANR mechanism should still work. |
| 6604 | * |
| 6605 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 6606 | * DOWN event, while not responding on the second one. |
| 6607 | */ |
| 6608 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 6609 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6610 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6611 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6612 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6613 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6614 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6615 | |
| 6616 | // Now send ACTION_UP, with identical timestamp |
| 6617 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6618 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 6619 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 6620 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6621 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6622 | |
| 6623 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 6624 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6625 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6626 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6627 | } |
| 6628 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6629 | // A spy window can receive an ANR |
| 6630 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 6631 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 6632 | |
| 6633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6634 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6635 | WINDOW_LOCATION)); |
| 6636 | mWindow->consumeMotionDown(); |
| 6637 | |
| 6638 | std::optional<uint32_t> sequenceNum = spy->receiveEvent(); // ACTION_DOWN |
| 6639 | ASSERT_TRUE(sequenceNum); |
| 6640 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6641 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6642 | |
| 6643 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6644 | spy->consumeMotionEvent( |
| 6645 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6646 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6647 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6648 | } |
| 6649 | |
| 6650 | // 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] | 6651 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6652 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 6653 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6654 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6655 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6656 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6657 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6658 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6659 | |
| 6660 | // Stuck on the ACTION_UP |
| 6661 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6662 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6663 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6664 | // 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] | 6665 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6666 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6667 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6668 | |
| 6669 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 6670 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6671 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6672 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6673 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6674 | } |
| 6675 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6676 | // 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] | 6677 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6678 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 6679 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6680 | |
| 6681 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6682 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6683 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6684 | |
| 6685 | mWindow->consumeMotionDown(); |
| 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->consumeMotionUp(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(); |
| 6700 | } |
| 6701 | |
| 6702 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
| 6703 | mDispatcher->setMonitorDispatchingTimeoutForTest(30ms); |
| 6704 | |
| 6705 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6706 | |
| 6707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6708 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6709 | WINDOW_LOCATION)); |
| 6710 | |
| 6711 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6712 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 6713 | ASSERT_TRUE(consumeSeq); |
| 6714 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6715 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(30ms, monitor.getToken(), MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6716 | |
| 6717 | monitor.finishEvent(*consumeSeq); |
| 6718 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6719 | |
| 6720 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6721 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6722 | } |
| 6723 | |
| 6724 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 6725 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 6726 | // get an ANR again. |
| 6727 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 6728 | // 2. consume all pending events (= queue becomes healthy again) |
| 6729 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 6730 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 6731 | tapOnWindow(); |
| 6732 | |
| 6733 | mWindow->consumeMotionDown(); |
| 6734 | // Block on ACTION_UP |
| 6735 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6736 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6737 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 6738 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6739 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6740 | mWindow->assertNoEvents(); |
| 6741 | |
| 6742 | tapOnWindow(); |
| 6743 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6744 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6745 | mWindow->consumeMotionUp(); |
| 6746 | |
| 6747 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6748 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6749 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6750 | mWindow->assertNoEvents(); |
| 6751 | } |
| 6752 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6753 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 6754 | // it. |
| 6755 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6756 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6757 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6758 | WINDOW_LOCATION)); |
| 6759 | |
| 6760 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6761 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6762 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6763 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 6764 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6765 | // 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] | 6766 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 6767 | mWindow->consumeMotionEvent( |
| 6768 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6769 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6770 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6771 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6772 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6773 | } |
| 6774 | |
| 6775 | /** |
| 6776 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6777 | * not to to the focused window until the motion is processed. |
| 6778 | * |
| 6779 | * Warning!!! |
| 6780 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 6781 | * and the injection timeout that we specify when injecting the key. |
| 6782 | * We must have the injection timeout (10ms) be smaller than |
| 6783 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 6784 | * |
| 6785 | * If that value changes, this test should also change. |
| 6786 | */ |
| 6787 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 6788 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6789 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6790 | |
| 6791 | tapOnWindow(); |
| 6792 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6793 | ASSERT_TRUE(downSequenceNum); |
| 6794 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6795 | ASSERT_TRUE(upSequenceNum); |
| 6796 | // Don't finish the events yet, and send a key |
| 6797 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 6798 | // window even if motions are still being processed. But because the injection timeout is short, |
| 6799 | // we will receive INJECTION_TIMED_OUT as the result. |
| 6800 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6801 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6802 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6803 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 6804 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6805 | // Key will not be sent to the window, yet, because the window is still processing events |
| 6806 | // and the key remains pending, waiting for the touch events to be processed |
| 6807 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6808 | ASSERT_FALSE(keySequenceNum); |
| 6809 | |
| 6810 | std::this_thread::sleep_for(500ms); |
| 6811 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 6812 | // to the focused window, even though we have not yet finished the motion event |
| 6813 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6814 | mWindow->finishEvent(*downSequenceNum); |
| 6815 | mWindow->finishEvent(*upSequenceNum); |
| 6816 | } |
| 6817 | |
| 6818 | /** |
| 6819 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 6820 | * not go to the focused window until the motion is processed. |
| 6821 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 6822 | * focused window right away. |
| 6823 | */ |
| 6824 | TEST_F(InputDispatcherSingleWindowAnr, |
| 6825 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 6826 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 6827 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 6828 | |
| 6829 | tapOnWindow(); |
| 6830 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 6831 | ASSERT_TRUE(downSequenceNum); |
| 6832 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 6833 | ASSERT_TRUE(upSequenceNum); |
| 6834 | // Don't finish the events yet, and send a key |
| 6835 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6837 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 6838 | InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6839 | // At this point, key is still pending, and should not be sent to the application yet. |
| 6840 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 6841 | ASSERT_FALSE(keySequenceNum); |
| 6842 | |
| 6843 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 6844 | tapOnWindow(); |
| 6845 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 6846 | // the other events yet. We can finish events in any order. |
| 6847 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 6848 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 6849 | mWindow->consumeMotionDown(); |
| 6850 | mWindow->consumeMotionUp(); |
| 6851 | mWindow->assertNoEvents(); |
| 6852 | } |
| 6853 | |
| 6854 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 6855 | virtual void SetUp() override { |
| 6856 | InputDispatcherTest::SetUp(); |
| 6857 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6858 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6859 | mApplication->setDispatchingTimeout(10ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6860 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 6861 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6862 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6863 | // 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] | 6864 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6865 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6866 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 6867 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 6868 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6869 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6870 | |
| 6871 | // Set focused application. |
| 6872 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6873 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6874 | |
| 6875 | // Expect one focus window exist in display. |
| 6876 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6877 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6878 | mFocusedWindow->consumeFocusEvent(true); |
| 6879 | } |
| 6880 | |
| 6881 | virtual void TearDown() override { |
| 6882 | InputDispatcherTest::TearDown(); |
| 6883 | |
| 6884 | mUnfocusedWindow.clear(); |
| 6885 | mFocusedWindow.clear(); |
| 6886 | } |
| 6887 | |
| 6888 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6889 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6890 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 6891 | sp<FakeWindowHandle> mFocusedWindow; |
| 6892 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 6893 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 6894 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 6895 | |
| 6896 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 6897 | |
| 6898 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 6899 | |
| 6900 | private: |
| 6901 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6903 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6904 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6905 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6906 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6907 | location)); |
| 6908 | } |
| 6909 | }; |
| 6910 | |
| 6911 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 6912 | // should be ANR'd first. |
| 6913 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6914 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6915 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6916 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6917 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6918 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6919 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6920 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6921 | // We consumed all events, so no ANR |
| 6922 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6923 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 6924 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6926 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6927 | FOCUSED_WINDOW_LOCATION)); |
| 6928 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 6929 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6930 | |
| 6931 | const std::chrono::duration timeout = |
| 6932 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6933 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6934 | // Because we injected two DOWN events in a row, CANCEL is enqueued for the first event |
| 6935 | // sequence to make it consistent |
| 6936 | mFocusedWindow->consumeMotionCancel(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6937 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6938 | mFocusedWindow->consumeMotionDown(); |
| 6939 | // This cancel is generated because the connection was unresponsive |
| 6940 | mFocusedWindow->consumeMotionCancel(); |
| 6941 | mFocusedWindow->assertNoEvents(); |
| 6942 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6943 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6944 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 6945 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6946 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6947 | } |
| 6948 | |
| 6949 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 6950 | // it doesn't matter which order they should have ANR. |
| 6951 | // But we should receive ANR for both. |
| 6952 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 6953 | // Set the timeout for unfocused window to match the focused window |
| 6954 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 6955 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 6956 | |
| 6957 | tapOnFocusedWindow(); |
| 6958 | // 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] | 6959 | sp<IBinder> anrConnectionToken1, anrConnectionToken2; |
| 6960 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken1 = mFakePolicy->getUnresponsiveWindowToken(10ms)); |
| 6961 | ASSERT_NO_FATAL_FAILURE(anrConnectionToken2 = mFakePolicy->getUnresponsiveWindowToken(0ms)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6962 | |
| 6963 | // 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] | 6964 | ASSERT_TRUE(mFocusedWindow->getToken() == anrConnectionToken1 || |
| 6965 | mFocusedWindow->getToken() == anrConnectionToken2); |
| 6966 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrConnectionToken1 || |
| 6967 | mUnfocusedWindow->getToken() == anrConnectionToken2); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6968 | |
| 6969 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 6970 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6971 | |
| 6972 | mFocusedWindow->consumeMotionDown(); |
| 6973 | mFocusedWindow->consumeMotionUp(); |
| 6974 | mUnfocusedWindow->consumeMotionOutside(); |
| 6975 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 6976 | sp<IBinder> responsiveToken1, responsiveToken2; |
| 6977 | ASSERT_NO_FATAL_FAILURE(responsiveToken1 = mFakePolicy->getResponsiveWindowToken()); |
| 6978 | ASSERT_NO_FATAL_FAILURE(responsiveToken2 = mFakePolicy->getResponsiveWindowToken()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 6979 | |
| 6980 | // Both applications should be marked as responsive, in any order |
| 6981 | ASSERT_TRUE(mFocusedWindow->getToken() == responsiveToken1 || |
| 6982 | mFocusedWindow->getToken() == responsiveToken2); |
| 6983 | ASSERT_TRUE(mUnfocusedWindow->getToken() == responsiveToken1 || |
| 6984 | mUnfocusedWindow->getToken() == responsiveToken2); |
| 6985 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6986 | } |
| 6987 | |
| 6988 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 6989 | // We should also log an error to account for the dropped event (not tested here). |
| 6990 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 6991 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 6992 | tapOnFocusedWindow(); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 6993 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6994 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 6995 | // Receive the events, but don't respond |
| 6996 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 6997 | ASSERT_TRUE(downEventSequenceNum); |
| 6998 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 6999 | ASSERT_TRUE(upEventSequenceNum); |
| 7000 | const std::chrono::duration timeout = |
| 7001 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7002 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7003 | |
| 7004 | // Tap once again |
| 7005 | // 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] | 7006 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7007 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7008 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7009 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7010 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7011 | FOCUSED_WINDOW_LOCATION)); |
| 7012 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 7013 | // valid touch target |
| 7014 | mUnfocusedWindow->assertNoEvents(); |
| 7015 | |
| 7016 | // Consume the first tap |
| 7017 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 7018 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 7019 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7020 | // The second tap did not go to the focused window |
| 7021 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7022 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7023 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7024 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7025 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7026 | } |
| 7027 | |
| 7028 | // If you tap outside of all windows, there will not be ANR |
| 7029 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7030 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7031 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7032 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 7033 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7034 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7035 | } |
| 7036 | |
| 7037 | // Since the focused window is paused, tapping on it should not produce any events |
| 7038 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 7039 | mFocusedWindow->setPaused(true); |
| 7040 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 7041 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7042 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7043 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7044 | FOCUSED_WINDOW_LOCATION)); |
| 7045 | |
| 7046 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 7047 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7048 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 7049 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 7050 | |
| 7051 | mFocusedWindow->assertNoEvents(); |
| 7052 | mUnfocusedWindow->assertNoEvents(); |
| 7053 | } |
| 7054 | |
| 7055 | /** |
| 7056 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 7057 | * not to to the focused window until the motion is processed. |
| 7058 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 7059 | * |
| 7060 | * Warning!!! |
| 7061 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 7062 | * and the injection timeout that we specify when injecting the key. |
| 7063 | * We must have the injection timeout (10ms) be smaller than |
| 7064 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 7065 | * |
| 7066 | * If that value changes, this test should also change. |
| 7067 | */ |
| 7068 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 7069 | // Set a long ANR timeout to prevent it from triggering |
| 7070 | mFocusedWindow->setDispatchingTimeout(2s); |
| 7071 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7072 | |
| 7073 | tapOnUnfocusedWindow(); |
| 7074 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7075 | ASSERT_TRUE(downSequenceNum); |
| 7076 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 7077 | ASSERT_TRUE(upSequenceNum); |
| 7078 | // Don't finish the events yet, and send a key |
| 7079 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 7080 | // window even if motions are still being processed. |
| 7081 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7082 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7083 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7084 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7085 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7086 | // Key will not be sent to the window, yet, because the window is still processing events |
| 7087 | // and the key remains pending, waiting for the touch events to be processed |
| 7088 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 7089 | ASSERT_FALSE(keySequenceNum); |
| 7090 | |
| 7091 | // 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] | 7092 | mFocusedWindow->setFocusable(false); |
| 7093 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7094 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7095 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7096 | |
| 7097 | // Focus events should precede the key events |
| 7098 | mUnfocusedWindow->consumeFocusEvent(true); |
| 7099 | mFocusedWindow->consumeFocusEvent(false); |
| 7100 | |
| 7101 | // Finish the tap events, which should unblock dispatcher |
| 7102 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 7103 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 7104 | |
| 7105 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 7106 | // can finally go to the newly focused "mUnfocusedWindow". |
| 7107 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7108 | mFocusedWindow->assertNoEvents(); |
| 7109 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7110 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 7114 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 7115 | // The other window should not be affected by that. |
| 7116 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 7117 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7118 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7119 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7120 | {FOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7121 | mUnfocusedWindow->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7122 | ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7123 | |
| 7124 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7125 | mDispatcher->notifyMotion( |
| 7126 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7127 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7128 | |
| 7129 | const std::chrono::duration timeout = |
| 7130 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7131 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7132 | |
| 7133 | mUnfocusedWindow->consumeMotionDown(); |
| 7134 | mFocusedWindow->consumeMotionDown(); |
| 7135 | // Focused window may or may not receive ACTION_MOVE |
| 7136 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 7137 | InputEvent* event; |
| 7138 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 7139 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 7140 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 7141 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 7142 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7143 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 7144 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 7145 | mFocusedWindow->consumeMotionCancel(); |
| 7146 | } else { |
| 7147 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 7148 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7149 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 7150 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 7151 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7152 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7153 | mUnfocusedWindow->assertNoEvents(); |
| 7154 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 7155 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 7156 | } |
| 7157 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7158 | /** |
| 7159 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 7160 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 7161 | * |
| 7162 | * If the user touches another application during this time, the key should be dropped. |
| 7163 | * Next, if a new focused window comes in, without toggling the focused application, |
| 7164 | * then no ANR should occur. |
| 7165 | * |
| 7166 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 7167 | * but in some cases the policy may not update the focused application. |
| 7168 | */ |
| 7169 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 7170 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 7171 | std::make_shared<FakeApplicationHandle>(); |
| 7172 | focusedApplication->setDispatchingTimeout(60ms); |
| 7173 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 7174 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 7175 | mFocusedWindow->setFocusable(false); |
| 7176 | |
| 7177 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7178 | mFocusedWindow->consumeFocusEvent(false); |
| 7179 | |
| 7180 | // Send a key. The ANR timer should start because there is no focused window. |
| 7181 | // 'focusedApplication' will get blamed if this timer completes. |
| 7182 | // 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] | 7183 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7184 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7185 | InputEventInjectionSync::NONE, /*injectionTimeout=*/10ms, |
| 7186 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7188 | |
| 7189 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 7190 | // then the injected touches won't cause the focused event to get dropped. |
| 7191 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 7192 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 7193 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 7194 | // For this test, it means that the key would get delivered to the window once it becomes |
| 7195 | // focused. |
| 7196 | std::this_thread::sleep_for(10ms); |
| 7197 | |
| 7198 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7199 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7200 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7201 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 7202 | |
| 7203 | // We do not consume the motion right away, because that would require dispatcher to first |
| 7204 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 7205 | // Set the focused window first. |
| 7206 | mFocusedWindow->setFocusable(true); |
| 7207 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 7208 | setFocusedWindow(mFocusedWindow); |
| 7209 | mFocusedWindow->consumeFocusEvent(true); |
| 7210 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 7211 | // to another application. This could be a bug / behaviour in the policy. |
| 7212 | |
| 7213 | mUnfocusedWindow->consumeMotionDown(); |
| 7214 | |
| 7215 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7216 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 7217 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 7218 | } |
| 7219 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7220 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 7221 | // that has feature NO_INPUT_CHANNEL. |
| 7222 | // Layout: |
| 7223 | // Top (closest to user) |
| 7224 | // mNoInputWindow (above all windows) |
| 7225 | // mBottomWindow |
| 7226 | // Bottom (furthest from user) |
| 7227 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 7228 | virtual void SetUp() override { |
| 7229 | InputDispatcherTest::SetUp(); |
| 7230 | |
| 7231 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7232 | mNoInputWindow = |
| 7233 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7234 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7235 | /*token=*/std::make_optional<sp<IBinder>>(nullptr)); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7236 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7237 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7238 | // It's perfectly valid for this window to not have an associated input channel |
| 7239 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7240 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 7241 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7242 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7243 | |
| 7244 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7245 | } |
| 7246 | |
| 7247 | protected: |
| 7248 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 7249 | sp<FakeWindowHandle> mNoInputWindow; |
| 7250 | sp<FakeWindowHandle> mBottomWindow; |
| 7251 | }; |
| 7252 | |
| 7253 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 7254 | PointF touchedPoint = {10, 10}; |
| 7255 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7256 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7257 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7258 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7259 | |
| 7260 | mNoInputWindow->assertNoEvents(); |
| 7261 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 7262 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 7263 | // and therefore should prevent mBottomWindow from receiving touches |
| 7264 | mBottomWindow->assertNoEvents(); |
| 7265 | } |
| 7266 | |
| 7267 | /** |
| 7268 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 7269 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 7270 | */ |
| 7271 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 7272 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7273 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 7274 | "Window with input channel and NO_INPUT_CHANNEL", |
| 7275 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7276 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 7277 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7278 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7279 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 7280 | |
| 7281 | PointF touchedPoint = {10, 10}; |
| 7282 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7283 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7284 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7285 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 7286 | |
| 7287 | mNoInputWindow->assertNoEvents(); |
| 7288 | mBottomWindow->assertNoEvents(); |
| 7289 | } |
| 7290 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7291 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 7292 | protected: |
| 7293 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7294 | sp<FakeWindowHandle> mWindow; |
| 7295 | sp<FakeWindowHandle> mMirror; |
| 7296 | |
| 7297 | virtual void SetUp() override { |
| 7298 | InputDispatcherTest::SetUp(); |
| 7299 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7300 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 7301 | mMirror = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindowMirror", |
| 7302 | ADISPLAY_ID_DEFAULT, mWindow->getToken()); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7303 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7304 | mWindow->setFocusable(true); |
| 7305 | mMirror->setFocusable(true); |
| 7306 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7307 | } |
| 7308 | }; |
| 7309 | |
| 7310 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 7311 | // Request focus on a mirrored window |
| 7312 | setFocusedWindow(mMirror); |
| 7313 | |
| 7314 | // window gets focused |
| 7315 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7316 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7317 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7318 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 7319 | } |
| 7320 | |
| 7321 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 7322 | // focusable. |
| 7323 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 7324 | setFocusedWindow(mMirror); |
| 7325 | |
| 7326 | // window gets focused |
| 7327 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7329 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7330 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7332 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7333 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7334 | |
| 7335 | mMirror->setFocusable(false); |
| 7336 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7337 | |
| 7338 | // window loses focus since one of the windows associated with the token in not focusable |
| 7339 | mWindow->consumeFocusEvent(false); |
| 7340 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7341 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7342 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7343 | mWindow->assertNoEvents(); |
| 7344 | } |
| 7345 | |
| 7346 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 7347 | // invisible. |
| 7348 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 7349 | setFocusedWindow(mMirror); |
| 7350 | |
| 7351 | // window gets focused |
| 7352 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7354 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7355 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7356 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7357 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7358 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7359 | |
| 7360 | mMirror->setVisible(false); |
| 7361 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7362 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7363 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7364 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7365 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7367 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7368 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7369 | |
| 7370 | mWindow->setVisible(false); |
| 7371 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7372 | |
| 7373 | // window loses focus only after all windows associated with the token become invisible. |
| 7374 | mWindow->consumeFocusEvent(false); |
| 7375 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7376 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7377 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7378 | mWindow->assertNoEvents(); |
| 7379 | } |
| 7380 | |
| 7381 | // A focused & mirrored window remains focused until both windows are removed. |
| 7382 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 7383 | setFocusedWindow(mMirror); |
| 7384 | |
| 7385 | // window gets focused |
| 7386 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7388 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7389 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7390 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7391 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7392 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7393 | |
| 7394 | // single window is removed but the window token remains focused |
| 7395 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 7396 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7397 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 7398 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7399 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7400 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 7401 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7402 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 7403 | |
| 7404 | // Both windows are removed |
| 7405 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 7406 | mWindow->consumeFocusEvent(false); |
| 7407 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7408 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 7409 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7410 | mWindow->assertNoEvents(); |
| 7411 | } |
| 7412 | |
| 7413 | // Focus request can be pending until one window becomes visible. |
| 7414 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 7415 | // Request focus on an invisible mirror. |
| 7416 | mWindow->setVisible(false); |
| 7417 | mMirror->setVisible(false); |
| 7418 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7419 | setFocusedWindow(mMirror); |
| 7420 | |
| 7421 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7422 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7423 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 7424 | InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7425 | |
| 7426 | mMirror->setVisible(true); |
| 7427 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 7428 | |
| 7429 | // window gets focused |
| 7430 | mWindow->consumeFocusEvent(true); |
| 7431 | // window gets the pending key event |
| 7432 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7433 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7434 | |
| 7435 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 7436 | protected: |
| 7437 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7438 | sp<FakeWindowHandle> mWindow; |
| 7439 | sp<FakeWindowHandle> mSecondWindow; |
| 7440 | |
| 7441 | void SetUp() override { |
| 7442 | InputDispatcherTest::SetUp(); |
| 7443 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7444 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7445 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7446 | mSecondWindow = |
| 7447 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7448 | mSecondWindow->setFocusable(true); |
| 7449 | |
| 7450 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 7451 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}}); |
| 7452 | |
| 7453 | setFocusedWindow(mWindow); |
| 7454 | mWindow->consumeFocusEvent(true); |
| 7455 | } |
| 7456 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7457 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7458 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7459 | } |
| 7460 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7461 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 7462 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7463 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7464 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 7465 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7466 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7467 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7468 | } |
| 7469 | }; |
| 7470 | |
| 7471 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 7472 | // Ensure that capture cannot be obtained for unfocused windows. |
| 7473 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 7474 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7475 | mSecondWindow->assertNoEvents(); |
| 7476 | |
| 7477 | // Ensure that capture can be enabled from the focus window. |
| 7478 | requestAndVerifyPointerCapture(mWindow, true); |
| 7479 | |
| 7480 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 7481 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 7482 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7483 | |
| 7484 | // Ensure that capture can be disabled from the window with capture. |
| 7485 | requestAndVerifyPointerCapture(mWindow, false); |
| 7486 | } |
| 7487 | |
| 7488 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7489 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7490 | |
| 7491 | setFocusedWindow(mSecondWindow); |
| 7492 | |
| 7493 | // Ensure that the capture disabled event was sent first. |
| 7494 | mWindow->consumeCaptureEvent(false); |
| 7495 | mWindow->consumeFocusEvent(false); |
| 7496 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7497 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7498 | |
| 7499 | // 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] | 7500 | notifyPointerCaptureChanged({}); |
| 7501 | notifyPointerCaptureChanged(request); |
| 7502 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7503 | mWindow->assertNoEvents(); |
| 7504 | mSecondWindow->assertNoEvents(); |
| 7505 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 7506 | } |
| 7507 | |
| 7508 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7509 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7510 | |
| 7511 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7512 | notifyPointerCaptureChanged({}); |
| 7513 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7514 | |
| 7515 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7516 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 7517 | mWindow->consumeCaptureEvent(false); |
| 7518 | mWindow->assertNoEvents(); |
| 7519 | } |
| 7520 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7521 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 7522 | requestAndVerifyPointerCapture(mWindow, true); |
| 7523 | |
| 7524 | // The first window loses focus. |
| 7525 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7526 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7527 | mWindow->consumeCaptureEvent(false); |
| 7528 | |
| 7529 | // Request Pointer Capture from the second window before the notification from InputReader |
| 7530 | // arrives. |
| 7531 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7532 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7533 | |
| 7534 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7535 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7536 | |
| 7537 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7538 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 7539 | |
| 7540 | mSecondWindow->consumeFocusEvent(true); |
| 7541 | mSecondWindow->consumeCaptureEvent(true); |
| 7542 | } |
| 7543 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 7544 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 7545 | // App repeatedly enables and disables capture. |
| 7546 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7547 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7548 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7549 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7550 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7551 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7552 | |
| 7553 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 7554 | // first request is now stale, this should do nothing. |
| 7555 | notifyPointerCaptureChanged(firstRequest); |
| 7556 | mWindow->assertNoEvents(); |
| 7557 | |
| 7558 | // InputReader notifies that the second request was enabled. |
| 7559 | notifyPointerCaptureChanged(secondRequest); |
| 7560 | mWindow->consumeCaptureEvent(true); |
| 7561 | } |
| 7562 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 7563 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 7564 | requestAndVerifyPointerCapture(mWindow, true); |
| 7565 | |
| 7566 | // App toggles pointer capture off and on. |
| 7567 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 7568 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 7569 | |
| 7570 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 7571 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 7572 | |
| 7573 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 7574 | // preceding "disable" request. |
| 7575 | notifyPointerCaptureChanged(enableRequest); |
| 7576 | |
| 7577 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 7578 | // any notifications. |
| 7579 | mWindow->assertNoEvents(); |
| 7580 | } |
| 7581 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7582 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 7583 | protected: |
| 7584 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7585 | |
| 7586 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 7587 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 7588 | |
| 7589 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 7590 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7591 | |
| 7592 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 7593 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 7594 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 7595 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 7596 | MAXIMUM_OBSCURING_OPACITY); |
| 7597 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7598 | static const int32_t TOUCHED_APP_UID = 10001; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7599 | static const int32_t APP_B_UID = 10002; |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7600 | static const int32_t APP_C_UID = 10003; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7601 | |
| 7602 | sp<FakeWindowHandle> mTouchWindow; |
| 7603 | |
| 7604 | virtual void SetUp() override { |
| 7605 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7606 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7607 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 7608 | } |
| 7609 | |
| 7610 | virtual void TearDown() override { |
| 7611 | InputDispatcherTest::TearDown(); |
| 7612 | mTouchWindow.clear(); |
| 7613 | } |
| 7614 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 7615 | sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, |
| 7616 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7617 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7618 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7619 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7620 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7621 | return window; |
| 7622 | } |
| 7623 | |
| 7624 | sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { |
| 7625 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 7626 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7627 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7628 | // Generate an arbitrary PID based on the UID |
| 7629 | window->setOwnerInfo(1777 + (uid % 10000), uid); |
| 7630 | return window; |
| 7631 | } |
| 7632 | |
| 7633 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7634 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 7635 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7636 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7637 | } |
| 7638 | }; |
| 7639 | |
| 7640 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7641 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7642 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7643 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7644 | |
| 7645 | touch(); |
| 7646 | |
| 7647 | mTouchWindow->assertNoEvents(); |
| 7648 | } |
| 7649 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7650 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7651 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 7652 | const sp<FakeWindowHandle>& w = |
| 7653 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
| 7654 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7655 | |
| 7656 | touch(); |
| 7657 | |
| 7658 | mTouchWindow->assertNoEvents(); |
| 7659 | } |
| 7660 | |
| 7661 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7662 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 7663 | const sp<FakeWindowHandle>& w = |
| 7664 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7665 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7666 | |
| 7667 | touch(); |
| 7668 | |
| 7669 | w->assertNoEvents(); |
| 7670 | } |
| 7671 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7672 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7673 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
| 7674 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7675 | |
| 7676 | touch(); |
| 7677 | |
| 7678 | mTouchWindow->consumeAnyMotionDown(); |
| 7679 | } |
| 7680 | |
| 7681 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7682 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7683 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7684 | w->setFrame(Rect(0, 0, 50, 50)); |
| 7685 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7686 | |
| 7687 | touch({PointF{100, 100}}); |
| 7688 | |
| 7689 | mTouchWindow->consumeAnyMotionDown(); |
| 7690 | } |
| 7691 | |
| 7692 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7693 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7694 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7695 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7696 | |
| 7697 | touch(); |
| 7698 | |
| 7699 | mTouchWindow->consumeAnyMotionDown(); |
| 7700 | } |
| 7701 | |
| 7702 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 7703 | const sp<FakeWindowHandle>& w = |
| 7704 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7705 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7706 | |
| 7707 | touch(); |
| 7708 | |
| 7709 | mTouchWindow->consumeAnyMotionDown(); |
| 7710 | } |
| 7711 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7712 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 7713 | const sp<FakeWindowHandle>& w = |
| 7714 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
| 7715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7716 | |
| 7717 | touch(); |
| 7718 | |
| 7719 | w->assertNoEvents(); |
| 7720 | } |
| 7721 | |
| 7722 | /** |
| 7723 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 7724 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 7725 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 7726 | */ |
| 7727 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7728 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 7729 | const sp<FakeWindowHandle>& w = |
| 7730 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7731 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7732 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7733 | |
| 7734 | touch(); |
| 7735 | |
| 7736 | w->consumeMotionOutside(); |
| 7737 | } |
| 7738 | |
| 7739 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 7740 | const sp<FakeWindowHandle>& w = |
| 7741 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7742 | w->setWatchOutsideTouch(true); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7743 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7744 | |
| 7745 | touch(); |
| 7746 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 7747 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7750 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7751 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7752 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7753 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7754 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7755 | |
| 7756 | touch(); |
| 7757 | |
| 7758 | mTouchWindow->consumeAnyMotionDown(); |
| 7759 | } |
| 7760 | |
| 7761 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 7762 | const sp<FakeWindowHandle>& w = |
| 7763 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7764 | MAXIMUM_OBSCURING_OPACITY); |
| 7765 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7766 | |
| 7767 | touch(); |
| 7768 | |
| 7769 | mTouchWindow->consumeAnyMotionDown(); |
| 7770 | } |
| 7771 | |
| 7772 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7773 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7774 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7775 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7776 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7777 | |
| 7778 | touch(); |
| 7779 | |
| 7780 | mTouchWindow->assertNoEvents(); |
| 7781 | } |
| 7782 | |
| 7783 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 7784 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 7785 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7786 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7787 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7788 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7789 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7790 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7791 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7792 | |
| 7793 | touch(); |
| 7794 | |
| 7795 | mTouchWindow->assertNoEvents(); |
| 7796 | } |
| 7797 | |
| 7798 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 7799 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 7800 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7801 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 7802 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7803 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7804 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 7805 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7806 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7807 | |
| 7808 | touch(); |
| 7809 | |
| 7810 | mTouchWindow->consumeAnyMotionDown(); |
| 7811 | } |
| 7812 | |
| 7813 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7814 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 7815 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7816 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7817 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7818 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7819 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7820 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7821 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7822 | |
| 7823 | touch(); |
| 7824 | |
| 7825 | mTouchWindow->consumeAnyMotionDown(); |
| 7826 | } |
| 7827 | |
| 7828 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 7829 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7830 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7831 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7832 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7833 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7834 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 7835 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 7836 | |
| 7837 | touch(); |
| 7838 | |
| 7839 | mTouchWindow->assertNoEvents(); |
| 7840 | } |
| 7841 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7842 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7843 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 7844 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7845 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7846 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7847 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7848 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7849 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7850 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7851 | |
| 7852 | touch(); |
| 7853 | |
| 7854 | mTouchWindow->assertNoEvents(); |
| 7855 | } |
| 7856 | |
| 7857 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7858 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 7859 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7860 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 7861 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7862 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7863 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7864 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 7865 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wA, wB, mTouchWindow}}}); |
| 7866 | |
| 7867 | touch(); |
| 7868 | |
| 7869 | mTouchWindow->consumeAnyMotionDown(); |
| 7870 | } |
| 7871 | |
| 7872 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 7873 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7874 | getOccludingWindow(TOUCHED_APP_UID, "T", 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, {w, mTouchWindow}}}); |
| 7877 | |
| 7878 | touch(); |
| 7879 | |
| 7880 | mTouchWindow->consumeAnyMotionDown(); |
| 7881 | } |
| 7882 | |
| 7883 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 7884 | const sp<FakeWindowHandle>& w = |
| 7885 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7886 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7887 | |
| 7888 | touch(); |
| 7889 | |
| 7890 | mTouchWindow->consumeAnyMotionDown(); |
| 7891 | } |
| 7892 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7893 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7894 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 7895 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7896 | const sp<FakeWindowHandle>& w = |
| 7897 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
| 7898 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7899 | |
| 7900 | touch(); |
| 7901 | |
| 7902 | mTouchWindow->assertNoEvents(); |
| 7903 | } |
| 7904 | |
| 7905 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 7906 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 7907 | const sp<FakeWindowHandle>& w = |
| 7908 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
| 7909 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7910 | |
| 7911 | touch(); |
| 7912 | |
| 7913 | mTouchWindow->consumeAnyMotionDown(); |
| 7914 | } |
| 7915 | |
| 7916 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7917 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 7918 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 7919 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 7920 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7921 | OPACITY_ABOVE_THRESHOLD); |
| 7922 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7923 | |
| 7924 | touch(); |
| 7925 | |
| 7926 | mTouchWindow->consumeAnyMotionDown(); |
| 7927 | } |
| 7928 | |
| 7929 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7930 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 7931 | const sp<FakeWindowHandle>& w1 = |
| 7932 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7933 | OPACITY_BELOW_THRESHOLD); |
| 7934 | const sp<FakeWindowHandle>& w2 = |
| 7935 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 7936 | OPACITY_BELOW_THRESHOLD); |
| 7937 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w1, w2, mTouchWindow}}}); |
| 7938 | |
| 7939 | touch(); |
| 7940 | |
| 7941 | mTouchWindow->assertNoEvents(); |
| 7942 | } |
| 7943 | |
| 7944 | /** |
| 7945 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 7946 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 7947 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 7948 | */ |
| 7949 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7950 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 7951 | const sp<FakeWindowHandle>& wB = |
| 7952 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 7953 | OPACITY_BELOW_THRESHOLD); |
| 7954 | const sp<FakeWindowHandle>& wC = |
| 7955 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 7956 | OPACITY_BELOW_THRESHOLD); |
| 7957 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {wB, wC, mTouchWindow}}}); |
| 7958 | |
| 7959 | touch(); |
| 7960 | |
| 7961 | mTouchWindow->assertNoEvents(); |
| 7962 | } |
| 7963 | |
| 7964 | /** |
| 7965 | * This test is testing that a window from a different UID but with same application token doesn't |
| 7966 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 7967 | */ |
| 7968 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 7969 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 7970 | const sp<FakeWindowHandle>& w = |
| 7971 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 7972 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 7973 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {w, mTouchWindow}}}); |
| 7974 | |
| 7975 | touch(); |
| 7976 | |
| 7977 | mTouchWindow->consumeAnyMotionDown(); |
| 7978 | } |
| 7979 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7980 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 7981 | protected: |
| 7982 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7983 | sp<FakeWindowHandle> mWindow; |
| 7984 | sp<FakeWindowHandle> mSecondWindow; |
| 7985 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 7986 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 7987 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 7988 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7989 | |
| 7990 | void SetUp() override { |
| 7991 | InputDispatcherTest::SetUp(); |
| 7992 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7993 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7994 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7995 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7996 | mSecondWindow = |
| 7997 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7998 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 7999 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8000 | mSpyWindow = |
| 8001 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8002 | mSpyWindow->setSpy(true); |
| 8003 | mSpyWindow->setTrustedOverlay(true); |
| 8004 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 8005 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8006 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8007 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8008 | } |
| 8009 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8010 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 8011 | switch (fromSource) { |
| 8012 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 8013 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8014 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8015 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8016 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8017 | break; |
| 8018 | case AINPUT_SOURCE_STYLUS: |
| 8019 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8020 | injectMotionEvent( |
| 8021 | mDispatcher, |
| 8022 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8023 | AINPUT_SOURCE_STYLUS) |
| 8024 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8025 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8026 | .x(50) |
| 8027 | .y(50)) |
| 8028 | .build())); |
| 8029 | break; |
| 8030 | case AINPUT_SOURCE_MOUSE: |
| 8031 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8032 | injectMotionEvent( |
| 8033 | mDispatcher, |
| 8034 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8035 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8036 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8037 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8038 | .x(50) |
| 8039 | .y(50)) |
| 8040 | .build())); |
| 8041 | break; |
| 8042 | default: |
| 8043 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 8044 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8045 | |
| 8046 | // Window should receive motion event. |
| 8047 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8048 | // Spy window should also receive motion event |
| 8049 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8050 | } |
| 8051 | |
| 8052 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 8053 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 8054 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8055 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8056 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8057 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8058 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8059 | |
| 8060 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8061 | mDragWindow = |
| 8062 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8063 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8064 | mDispatcher->setInputWindows( |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8065 | {{ADISPLAY_ID_DEFAULT, {mDragWindow, mSpyWindow, mWindow, mSecondWindow}}}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8066 | |
| 8067 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8068 | bool transferred = |
| 8069 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8070 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8071 | if (transferred) { |
| 8072 | mWindow->consumeMotionCancel(); |
| 8073 | mDragWindow->consumeMotionDown(); |
| 8074 | } |
| 8075 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8076 | } |
| 8077 | }; |
| 8078 | |
| 8079 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8080 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 8081 | |
| 8082 | // Move on window. |
| 8083 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8084 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8085 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8086 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8087 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8088 | mWindow->consumeDragEvent(false, 50, 50); |
| 8089 | mSecondWindow->assertNoEvents(); |
| 8090 | |
| 8091 | // Move to another window. |
| 8092 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8093 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8094 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8095 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8096 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8097 | mWindow->consumeDragEvent(true, 150, 50); |
| 8098 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8099 | |
| 8100 | // Move back to original window. |
| 8101 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8102 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8103 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8104 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8105 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8106 | mWindow->consumeDragEvent(false, 50, 50); |
| 8107 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 8108 | |
| 8109 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8110 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8111 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8112 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8113 | mWindow->assertNoEvents(); |
| 8114 | mSecondWindow->assertNoEvents(); |
| 8115 | } |
| 8116 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8117 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8118 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8119 | |
| 8120 | // No cancel event after drag start |
| 8121 | mSpyWindow->assertNoEvents(); |
| 8122 | |
| 8123 | const MotionEvent secondFingerDownEvent = |
| 8124 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8125 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8126 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8127 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 8128 | .build(); |
| 8129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8130 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8131 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8132 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8133 | |
| 8134 | // Receives cancel for first pointer after next pointer down |
| 8135 | mSpyWindow->consumeMotionCancel(); |
| 8136 | mSpyWindow->consumeMotionDown(); |
| 8137 | |
| 8138 | mSpyWindow->assertNoEvents(); |
| 8139 | } |
| 8140 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8141 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8142 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 8143 | |
| 8144 | // Move on window. |
| 8145 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8146 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8147 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8148 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8149 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8150 | mWindow->consumeDragEvent(false, 50, 50); |
| 8151 | mSecondWindow->assertNoEvents(); |
| 8152 | |
| 8153 | // Move to another window. |
| 8154 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8155 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8156 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8157 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8158 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8159 | mWindow->consumeDragEvent(true, 150, 50); |
| 8160 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8161 | |
| 8162 | // drop to another window. |
| 8163 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8164 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8165 | {150, 50})) |
| 8166 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8167 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8168 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8169 | mWindow->assertNoEvents(); |
| 8170 | mSecondWindow->assertNoEvents(); |
| 8171 | } |
| 8172 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8173 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8174 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8175 | |
| 8176 | // Move on window and keep button pressed. |
| 8177 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8178 | injectMotionEvent(mDispatcher, |
| 8179 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8180 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8181 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8182 | .build())) |
| 8183 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8184 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8185 | mWindow->consumeDragEvent(false, 50, 50); |
| 8186 | mSecondWindow->assertNoEvents(); |
| 8187 | |
| 8188 | // Move to another window and release button, expect to drop item. |
| 8189 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8190 | injectMotionEvent(mDispatcher, |
| 8191 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 8192 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8193 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8194 | .build())) |
| 8195 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8196 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8197 | mWindow->assertNoEvents(); |
| 8198 | mSecondWindow->assertNoEvents(); |
| 8199 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8200 | |
| 8201 | // nothing to the window. |
| 8202 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8203 | injectMotionEvent(mDispatcher, |
| 8204 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 8205 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8206 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 8207 | .build())) |
| 8208 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8209 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8210 | mWindow->assertNoEvents(); |
| 8211 | mSecondWindow->assertNoEvents(); |
| 8212 | } |
| 8213 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8214 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8215 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 8216 | |
| 8217 | // Set second window invisible. |
| 8218 | mSecondWindow->setVisible(false); |
| 8219 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mDragWindow, mWindow, mSecondWindow}}}); |
| 8220 | |
| 8221 | // Move on window. |
| 8222 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8223 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8224 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8225 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8226 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8227 | mWindow->consumeDragEvent(false, 50, 50); |
| 8228 | mSecondWindow->assertNoEvents(); |
| 8229 | |
| 8230 | // Move to another window. |
| 8231 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8232 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8233 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8234 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8235 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8236 | mWindow->consumeDragEvent(true, 150, 50); |
| 8237 | mSecondWindow->assertNoEvents(); |
| 8238 | |
| 8239 | // drop to another window. |
| 8240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8241 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8242 | {150, 50})) |
| 8243 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8244 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8245 | mFakePolicy->assertDropTargetEquals(nullptr); |
| 8246 | mWindow->assertNoEvents(); |
| 8247 | mSecondWindow->assertNoEvents(); |
| 8248 | } |
| 8249 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8250 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8251 | // Ensure window could track pointerIds if it didn't support split touch. |
| 8252 | mWindow->setPreventSplitting(true); |
| 8253 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8254 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8255 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8256 | {50, 50})) |
| 8257 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8258 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8259 | |
| 8260 | const MotionEvent secondFingerDownEvent = |
| 8261 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8262 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8263 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8264 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8265 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8266 | .build(); |
| 8267 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8268 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8269 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8270 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8271 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8272 | |
| 8273 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8274 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8275 | } |
| 8276 | |
| 8277 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 8278 | // First down on second window. |
| 8279 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8280 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8281 | {150, 50})) |
| 8282 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8283 | |
| 8284 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8285 | |
| 8286 | // Second down on first window. |
| 8287 | const MotionEvent secondFingerDownEvent = |
| 8288 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8289 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8290 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8291 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8292 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8293 | .build(); |
| 8294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8295 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8296 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8297 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8298 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8299 | |
| 8300 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8301 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8302 | |
| 8303 | // Move on window. |
| 8304 | const MotionEvent secondFingerMoveEvent = |
| 8305 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8306 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8307 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8308 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8309 | .build(); |
| 8310 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8311 | injectMotionEvent(mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
| 8312 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8313 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8314 | mWindow->consumeDragEvent(false, 50, 50); |
| 8315 | mSecondWindow->consumeMotionMove(); |
| 8316 | |
| 8317 | // Release the drag pointer should perform drop. |
| 8318 | const MotionEvent secondFingerUpEvent = |
| 8319 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8320 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8321 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 8322 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 8323 | .build(); |
| 8324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8325 | injectMotionEvent(mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
| 8326 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 8327 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8328 | mFakePolicy->assertDropTargetEquals(mWindow->getToken()); |
| 8329 | mWindow->assertNoEvents(); |
| 8330 | mSecondWindow->consumeMotionMove(); |
| 8331 | } |
| 8332 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8333 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8334 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8335 | |
| 8336 | // Update window of second display. |
| 8337 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8338 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8339 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8340 | |
| 8341 | // Let second display has a touch state. |
| 8342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8343 | injectMotionEvent(mDispatcher, |
| 8344 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8345 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8346 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8347 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8348 | .build())); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 8349 | windowInSecondary->consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8350 | SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 8351 | // Update window again. |
| 8352 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
| 8353 | |
| 8354 | // Move on window. |
| 8355 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8356 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8357 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 8358 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8359 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8360 | mWindow->consumeDragEvent(false, 50, 50); |
| 8361 | mSecondWindow->assertNoEvents(); |
| 8362 | |
| 8363 | // Move to another window. |
| 8364 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8365 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8366 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 8367 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8368 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8369 | mWindow->consumeDragEvent(true, 150, 50); |
| 8370 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8371 | |
| 8372 | // drop to another window. |
| 8373 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8374 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8375 | {150, 50})) |
| 8376 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8377 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8378 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8379 | mWindow->assertNoEvents(); |
| 8380 | mSecondWindow->assertNoEvents(); |
| 8381 | } |
| 8382 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8383 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 8384 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 8385 | // Move on window. |
| 8386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8387 | injectMotionEvent(mDispatcher, |
| 8388 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8389 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8390 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8391 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8392 | .x(50) |
| 8393 | .y(50)) |
| 8394 | .build())) |
| 8395 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8396 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8397 | mWindow->consumeDragEvent(false, 50, 50); |
| 8398 | mSecondWindow->assertNoEvents(); |
| 8399 | |
| 8400 | // Move to another window. |
| 8401 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8402 | injectMotionEvent(mDispatcher, |
| 8403 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 8404 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 8405 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8406 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8407 | .x(150) |
| 8408 | .y(50)) |
| 8409 | .build())) |
| 8410 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8411 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 8412 | mWindow->consumeDragEvent(true, 150, 50); |
| 8413 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 8414 | |
| 8415 | // drop to another window. |
| 8416 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8417 | injectMotionEvent(mDispatcher, |
| 8418 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 8419 | .buttonState(0) |
| 8420 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8421 | ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 8422 | .x(150) |
| 8423 | .y(50)) |
| 8424 | .build())) |
| 8425 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8426 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 8427 | mFakePolicy->assertDropTargetEquals(mSecondWindow->getToken()); |
| 8428 | mWindow->assertNoEvents(); |
| 8429 | mSecondWindow->assertNoEvents(); |
| 8430 | } |
| 8431 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8432 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 8433 | |
| 8434 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 8435 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8436 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8437 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8438 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8439 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8440 | window->setFocusable(true); |
| 8441 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8442 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8443 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8444 | |
| 8445 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8446 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8447 | window->assertNoEvents(); |
| 8448 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8449 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8450 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8451 | window->assertNoEvents(); |
| 8452 | |
| 8453 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8454 | window->setDropInput(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8455 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 8456 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8457 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8458 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8459 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8460 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8461 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8462 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8463 | window->assertNoEvents(); |
| 8464 | } |
| 8465 | |
| 8466 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 8467 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8468 | std::make_shared<FakeApplicationHandle>(); |
| 8469 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8470 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8471 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8472 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8473 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8474 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8475 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8476 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8477 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8478 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8479 | window->setOwnerInfo(222, 222); |
| 8480 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8481 | window->setFocusable(true); |
| 8482 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8483 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8484 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8485 | |
| 8486 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8487 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8488 | window->assertNoEvents(); |
| 8489 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8490 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8491 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8492 | window->assertNoEvents(); |
| 8493 | |
| 8494 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8495 | window->setDropInputIfObscured(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8496 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8497 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8498 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8499 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8500 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8501 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8502 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8503 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8504 | window->assertNoEvents(); |
| 8505 | } |
| 8506 | |
| 8507 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 8508 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 8509 | std::make_shared<FakeApplicationHandle>(); |
| 8510 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8511 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 8512 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8513 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
| 8514 | obscuringWindow->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8515 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8516 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8517 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8518 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8519 | window->setDropInputIfObscured(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8520 | window->setOwnerInfo(222, 222); |
| 8521 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8522 | window->setFocusable(true); |
| 8523 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); |
| 8524 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8525 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8526 | |
| 8527 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8528 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8529 | window->assertNoEvents(); |
| 8530 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8531 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8532 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8533 | window->assertNoEvents(); |
| 8534 | |
| 8535 | // When the window is no longer obscured because it went on top, it should get input |
| 8536 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, obscuringWindow}}}); |
| 8537 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8538 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8539 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 8540 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8541 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8542 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 8543 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8544 | window->assertNoEvents(); |
| 8545 | } |
| 8546 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8547 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 8548 | protected: |
| 8549 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8550 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8551 | sp<FakeWindowHandle> mWindow; |
| 8552 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8553 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8554 | |
| 8555 | void SetUp() override { |
| 8556 | InputDispatcherTest::SetUp(); |
| 8557 | |
| 8558 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8559 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8560 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8561 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8562 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8563 | mSecondWindow = |
| 8564 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8565 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8566 | mThirdWindow = |
| 8567 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 8568 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 8569 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8570 | |
| 8571 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8572 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mSecondWindow}}, |
| 8573 | {SECOND_DISPLAY_ID, {mThirdWindow}}}); |
| 8574 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8575 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8576 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8577 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 8578 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8579 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8580 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 8581 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8582 | mThirdWindow->assertNoEvents(); |
| 8583 | } |
| 8584 | |
| 8585 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 8586 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8587 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8588 | SECOND_DISPLAY_ID)) { |
| 8589 | mWindow->assertNoEvents(); |
| 8590 | mSecondWindow->assertNoEvents(); |
| 8591 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8592 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8593 | } |
| 8594 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8595 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, |
| 8596 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8597 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 8598 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8599 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 8600 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8601 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8602 | } |
| 8603 | }; |
| 8604 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8605 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8606 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8607 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 8608 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8609 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8610 | } |
| 8611 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8612 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 8613 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8614 | int32_t ownerPid = windowInfo.ownerPid; |
| 8615 | int32_t ownerUid = windowInfo.ownerUid; |
| 8616 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
| 8617 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8618 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 8619 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8620 | mWindow->assertNoEvents(); |
| 8621 | mSecondWindow->assertNoEvents(); |
| 8622 | } |
| 8623 | |
| 8624 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 8625 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8626 | int32_t ownerPid = windowInfo.ownerPid; |
| 8627 | int32_t ownerUid = windowInfo.ownerUid; |
| 8628 | mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8629 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8630 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8631 | } |
| 8632 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8633 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8634 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 8635 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 8636 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8637 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8638 | mWindow->assertNoEvents(); |
| 8639 | mSecondWindow->assertNoEvents(); |
| 8640 | } |
| 8641 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8642 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 8643 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 8644 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8645 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8646 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 8647 | mWindow->assertNoEvents(); |
| 8648 | mSecondWindow->assertNoEvents(); |
| 8649 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 8650 | } |
| 8651 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8652 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 8653 | // Interact with the window first. |
| 8654 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 8655 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8656 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8657 | |
| 8658 | // Then remove focus. |
| 8659 | mWindow->setFocusable(false); |
| 8660 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 8661 | |
| 8662 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 8663 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 8664 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 8665 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8666 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 8667 | } |
| 8668 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8669 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 8670 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8671 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8672 | std::shared_ptr<FakeApplicationHandle> application = |
| 8673 | std::make_shared<FakeApplicationHandle>(); |
| 8674 | std::string name = "Fake Spy "; |
| 8675 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8676 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 8677 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8678 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8679 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8680 | return spy; |
| 8681 | } |
| 8682 | |
| 8683 | sp<FakeWindowHandle> createForeground() { |
| 8684 | std::shared_ptr<FakeApplicationHandle> application = |
| 8685 | std::make_shared<FakeApplicationHandle>(); |
| 8686 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8687 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 8688 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8689 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8690 | return window; |
| 8691 | } |
| 8692 | |
| 8693 | private: |
| 8694 | int mSpyCount{0}; |
| 8695 | }; |
| 8696 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8697 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8698 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8699 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 8700 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 8701 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
| 8702 | ScopedSilentDeath _silentDeath; |
| 8703 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8704 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 8705 | spy->setTrustedOverlay(false); |
| 8706 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}), |
| 8707 | ".* not a trusted overlay"); |
| 8708 | } |
| 8709 | |
| 8710 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8711 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 8712 | */ |
| 8713 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8714 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy}}}); |
| 8716 | |
| 8717 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8718 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8719 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8720 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8721 | } |
| 8722 | |
| 8723 | /** |
| 8724 | * Verify the order in which different input windows receive events. The touched foreground window |
| 8725 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 8726 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 8727 | * receive events before ones belows it. |
| 8728 | * |
| 8729 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 8730 | * spy1, spy2, window, spy3. |
| 8731 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 8732 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 8733 | * window. |
| 8734 | */ |
| 8735 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 8736 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8737 | auto spy1 = createSpy(); |
| 8738 | auto spy2 = createSpy(); |
| 8739 | auto spy3 = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8740 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window, spy3}}}); |
| 8741 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 8742 | const size_t numChannels = channels.size(); |
| 8743 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 8744 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8745 | if (!epollFd.ok()) { |
| 8746 | FAIL() << "Failed to create epoll fd"; |
| 8747 | } |
| 8748 | |
| 8749 | for (size_t i = 0; i < numChannels; i++) { |
| 8750 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 8751 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 8752 | FAIL() << "Failed to add fd to epoll"; |
| 8753 | } |
| 8754 | } |
| 8755 | |
| 8756 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8757 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8758 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8759 | |
| 8760 | std::vector<size_t> eventOrder; |
| 8761 | std::vector<struct epoll_event> events(numChannels); |
| 8762 | for (;;) { |
| 8763 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 8764 | (100ms).count()); |
| 8765 | if (nFds < 0) { |
| 8766 | FAIL() << "Failed to call epoll_wait"; |
| 8767 | } |
| 8768 | if (nFds == 0) { |
| 8769 | break; // epoll_wait timed out |
| 8770 | } |
| 8771 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 8772 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 8773 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8774 | channels[i]->consumeMotionDown(); |
| 8775 | } |
| 8776 | } |
| 8777 | |
| 8778 | // Verify the order in which the events were received. |
| 8779 | EXPECT_EQ(3u, eventOrder.size()); |
| 8780 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 8781 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 8782 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 8783 | } |
| 8784 | |
| 8785 | /** |
| 8786 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 8787 | */ |
| 8788 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 8789 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8790 | auto spy = createSpy(); |
| 8791 | spy->setTouchable(false); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8792 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8793 | |
| 8794 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8795 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8796 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8797 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8798 | spy->assertNoEvents(); |
| 8799 | } |
| 8800 | |
| 8801 | /** |
| 8802 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 8803 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 8804 | * to the window. |
| 8805 | */ |
| 8806 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 8807 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8808 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8809 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
| 8810 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8811 | |
| 8812 | // Inject an event outside the spy window's touchable region. |
| 8813 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8814 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8815 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8816 | window->consumeMotionDown(); |
| 8817 | spy->assertNoEvents(); |
| 8818 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8819 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8820 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8821 | window->consumeMotionUp(); |
| 8822 | spy->assertNoEvents(); |
| 8823 | |
| 8824 | // Inject an event inside the spy window's touchable region. |
| 8825 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8826 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8827 | {5, 10})) |
| 8828 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8829 | window->consumeMotionDown(); |
| 8830 | spy->consumeMotionDown(); |
| 8831 | } |
| 8832 | |
| 8833 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8834 | * 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] | 8835 | * 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] | 8836 | */ |
| 8837 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 8838 | auto window = createForeground(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8839 | window->setOwnerInfo(12, 34); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8840 | auto spy = createSpy(); |
| 8841 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8842 | spy->setOwnerInfo(56, 78); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8843 | spy->setFrame(Rect{0, 0, 20, 20}); |
| 8844 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8845 | |
| 8846 | // Inject an event outside the spy window's frame and touchable region. |
| 8847 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8848 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8849 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8850 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8851 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 8852 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8853 | } |
| 8854 | |
| 8855 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8856 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 8857 | * pointers that are down within its bounds. |
| 8858 | */ |
| 8859 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 8860 | auto windowLeft = createForeground(); |
| 8861 | windowLeft->setFrame({0, 0, 100, 200}); |
| 8862 | auto windowRight = createForeground(); |
| 8863 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8864 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8865 | spy->setFrame({0, 0, 200, 200}); |
| 8866 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, windowLeft, windowRight}}}); |
| 8867 | |
| 8868 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8869 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8870 | {50, 50})) |
| 8871 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8872 | windowLeft->consumeMotionDown(); |
| 8873 | spy->consumeMotionDown(); |
| 8874 | |
| 8875 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8876 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8877 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8878 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8879 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8880 | .build(); |
| 8881 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8882 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8883 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8884 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8885 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8886 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8887 | } |
| 8888 | |
| 8889 | /** |
| 8890 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 8891 | * the spy should receive the second pointer with ACTION_DOWN. |
| 8892 | */ |
| 8893 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 8894 | auto window = createForeground(); |
| 8895 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8896 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8897 | spyRight->setFrame({100, 0, 200, 200}); |
| 8898 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spyRight, window}}}); |
| 8899 | |
| 8900 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8901 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8902 | {50, 50})) |
| 8903 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8904 | window->consumeMotionDown(); |
| 8905 | spyRight->assertNoEvents(); |
| 8906 | |
| 8907 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8908 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8909 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8910 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 8911 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8912 | .build(); |
| 8913 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8914 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8915 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8916 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8917 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 8918 | spyRight->consumeMotionDown(); |
| 8919 | } |
| 8920 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8921 | /** |
| 8922 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 8923 | * windows should be allowed to control split touch. |
| 8924 | */ |
| 8925 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8926 | // 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] | 8927 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8928 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8929 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8930 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8931 | auto window = createForeground(); |
| 8932 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8933 | |
| 8934 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8935 | |
| 8936 | // First finger down, no window touched. |
| 8937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8938 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8939 | {100, 200})) |
| 8940 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8941 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8942 | window->assertNoEvents(); |
| 8943 | |
| 8944 | // Second finger down on window, the window should receive touch down. |
| 8945 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8946 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8947 | .displayId(ADISPLAY_ID_DEFAULT) |
| 8948 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8949 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8950 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8951 | .build(); |
| 8952 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8953 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 8954 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 8955 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8956 | |
| 8957 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8958 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8959 | } |
| 8960 | |
| 8961 | /** |
| 8962 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 8963 | * do not receive key events. |
| 8964 | */ |
| 8965 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8966 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8967 | spy->setFocusable(false); |
| 8968 | |
| 8969 | auto window = createForeground(); |
| 8970 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 8971 | setFocusedWindow(window); |
| 8972 | window->consumeFocusEvent(true); |
| 8973 | |
| 8974 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 8975 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8976 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8977 | |
| 8978 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 8979 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8980 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 8981 | |
| 8982 | spy->assertNoEvents(); |
| 8983 | } |
| 8984 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 8985 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 8986 | |
| 8987 | /** |
| 8988 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 8989 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 8990 | */ |
| 8991 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 8992 | auto window = createForeground(); |
| 8993 | auto spy1 = createSpy(); |
| 8994 | auto spy2 = createSpy(); |
| 8995 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy1, spy2, window}}}); |
| 8996 | |
| 8997 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8998 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 8999 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9000 | window->consumeMotionDown(); |
| 9001 | spy1->consumeMotionDown(); |
| 9002 | spy2->consumeMotionDown(); |
| 9003 | |
| 9004 | // Pilfer pointers from the second spy window. |
| 9005 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 9006 | spy2->assertNoEvents(); |
| 9007 | spy1->consumeMotionCancel(); |
| 9008 | window->consumeMotionCancel(); |
| 9009 | |
| 9010 | // The rest of the gesture should only be sent to the second spy window. |
| 9011 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9012 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 9013 | ADISPLAY_ID_DEFAULT)) |
| 9014 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9015 | spy2->consumeMotionMove(); |
| 9016 | spy1->assertNoEvents(); |
| 9017 | window->assertNoEvents(); |
| 9018 | } |
| 9019 | |
| 9020 | /** |
| 9021 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 9022 | * in the middle of the gesture. |
| 9023 | */ |
| 9024 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 9025 | auto window = createForeground(); |
| 9026 | auto spy = createSpy(); |
| 9027 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9028 | |
| 9029 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9030 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9031 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9032 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9033 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 9034 | |
| 9035 | window->releaseChannel(); |
| 9036 | |
| 9037 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9038 | |
| 9039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9040 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 9041 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9042 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 9043 | } |
| 9044 | |
| 9045 | /** |
| 9046 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 9047 | * the spy, but not to any other windows. |
| 9048 | */ |
| 9049 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 9050 | auto spy = createSpy(); |
| 9051 | auto window = createForeground(); |
| 9052 | |
| 9053 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9054 | |
| 9055 | // First finger down on the window and the spy. |
| 9056 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9057 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9058 | {100, 200})) |
| 9059 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9060 | spy->consumeMotionDown(); |
| 9061 | window->consumeMotionDown(); |
| 9062 | |
| 9063 | // Spy window pilfers the pointers. |
| 9064 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9065 | window->consumeMotionCancel(); |
| 9066 | |
| 9067 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 9068 | const MotionEvent secondFingerDownEvent = |
| 9069 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9070 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9071 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9072 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9073 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9074 | .build(); |
| 9075 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9076 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9077 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9078 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9079 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9080 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9081 | |
| 9082 | // Third finger goes down outside all windows, so injection should fail. |
| 9083 | const MotionEvent thirdFingerDownEvent = |
| 9084 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9085 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9086 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9087 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9088 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 9089 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9090 | .build(); |
| 9091 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 9092 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9093 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9094 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9095 | |
| 9096 | spy->assertNoEvents(); |
| 9097 | window->assertNoEvents(); |
| 9098 | } |
| 9099 | |
| 9100 | /** |
| 9101 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 9102 | */ |
| 9103 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 9104 | auto spy = createSpy(); |
| 9105 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9106 | auto window = createForeground(); |
| 9107 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9108 | |
| 9109 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9110 | |
| 9111 | // First finger down on the window only |
| 9112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9113 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9114 | {150, 150})) |
| 9115 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9116 | window->consumeMotionDown(); |
| 9117 | |
| 9118 | // Second finger down on the spy and window |
| 9119 | const MotionEvent secondFingerDownEvent = |
| 9120 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9121 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9122 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9123 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9124 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9125 | .build(); |
| 9126 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9127 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9128 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9129 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9130 | spy->consumeMotionDown(); |
| 9131 | window->consumeMotionPointerDown(1); |
| 9132 | |
| 9133 | // Third finger down on the spy and window |
| 9134 | const MotionEvent thirdFingerDownEvent = |
| 9135 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9136 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9137 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9138 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 9139 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 9140 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9141 | .build(); |
| 9142 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9143 | injectMotionEvent(mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9144 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9145 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9146 | spy->consumeMotionPointerDown(1); |
| 9147 | window->consumeMotionPointerDown(2); |
| 9148 | |
| 9149 | // Spy window pilfers the pointers. |
| 9150 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9151 | window->consumeMotionPointerUp(/* idx */ 2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9152 | window->consumeMotionPointerUp(/* idx */ 1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 9153 | |
| 9154 | spy->assertNoEvents(); |
| 9155 | window->assertNoEvents(); |
| 9156 | } |
| 9157 | |
| 9158 | /** |
| 9159 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 9160 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 9161 | * window, then that window should receive ACTION_CANCEL. |
| 9162 | */ |
| 9163 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 9164 | auto spy = createSpy(); |
| 9165 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9166 | auto window = createForeground(); |
| 9167 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9168 | |
| 9169 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9170 | |
| 9171 | // First finger down on both spy and window |
| 9172 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9173 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9174 | {10, 10})) |
| 9175 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9176 | window->consumeMotionDown(); |
| 9177 | spy->consumeMotionDown(); |
| 9178 | |
| 9179 | // Second finger down on the spy and window |
| 9180 | const MotionEvent secondFingerDownEvent = |
| 9181 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9182 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9183 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9184 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9185 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9186 | .build(); |
| 9187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9188 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9189 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9190 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9191 | spy->consumeMotionPointerDown(1); |
| 9192 | window->consumeMotionPointerDown(1); |
| 9193 | |
| 9194 | // Spy window pilfers the pointers. |
| 9195 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9196 | window->consumeMotionCancel(); |
| 9197 | |
| 9198 | spy->assertNoEvents(); |
| 9199 | window->assertNoEvents(); |
| 9200 | } |
| 9201 | |
| 9202 | /** |
| 9203 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 9204 | * be sent to other windows |
| 9205 | */ |
| 9206 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 9207 | auto spy = createSpy(); |
| 9208 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 9209 | auto window = createForeground(); |
| 9210 | window->setFrame(Rect(0, 0, 200, 200)); |
| 9211 | |
| 9212 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9213 | |
| 9214 | // First finger down on both window and spy |
| 9215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9216 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9217 | {10, 10})) |
| 9218 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9219 | window->consumeMotionDown(); |
| 9220 | spy->consumeMotionDown(); |
| 9221 | |
| 9222 | // Spy window pilfers the pointers. |
| 9223 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 9224 | window->consumeMotionCancel(); |
| 9225 | |
| 9226 | // Second finger down on the window only |
| 9227 | const MotionEvent secondFingerDownEvent = |
| 9228 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9229 | .displayId(ADISPLAY_ID_DEFAULT) |
| 9230 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9231 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 9232 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 9233 | .build(); |
| 9234 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9235 | injectMotionEvent(mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 9236 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 9237 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9238 | window->consumeMotionDown(); |
| 9239 | window->assertNoEvents(); |
| 9240 | |
| 9241 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 9242 | spy->consumeMotionMove(); |
| 9243 | spy->assertNoEvents(); |
| 9244 | } |
| 9245 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9246 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 9247 | public: |
| 9248 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 9249 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9250 | std::make_shared<FakeApplicationHandle>(); |
| 9251 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9252 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 9253 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9254 | overlay->setFocusable(false); |
| 9255 | overlay->setOwnerInfo(111, 111); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9256 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9257 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9258 | overlay->setTrustedOverlay(true); |
| 9259 | |
| 9260 | std::shared_ptr<FakeApplicationHandle> application = |
| 9261 | std::make_shared<FakeApplicationHandle>(); |
| 9262 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9263 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 9264 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9265 | window->setFocusable(true); |
| 9266 | window->setOwnerInfo(222, 222); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9267 | |
| 9268 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 9269 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9270 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9271 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9272 | return {std::move(overlay), std::move(window)}; |
| 9273 | } |
| 9274 | |
| 9275 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9276 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9277 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9278 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9279 | } |
| 9280 | |
| 9281 | void sendStylusEvent(int32_t action) { |
| 9282 | NotifyMotionArgs motionArgs = |
| 9283 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 9284 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9285 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9286 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9287 | } |
| 9288 | }; |
| 9289 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 9290 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 9291 | |
| 9292 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
| 9293 | ScopedSilentDeath _silentDeath; |
| 9294 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9295 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9296 | overlay->setTrustedOverlay(false); |
| 9297 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
| 9298 | ASSERT_DEATH(mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}), |
| 9299 | ".* not a trusted overlay"); |
| 9300 | } |
| 9301 | |
| 9302 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 9303 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9304 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9305 | |
| 9306 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9307 | overlay->consumeMotionDown(); |
| 9308 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9309 | overlay->consumeMotionUp(); |
| 9310 | |
| 9311 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9312 | window->consumeMotionDown(); |
| 9313 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9314 | window->consumeMotionUp(); |
| 9315 | |
| 9316 | overlay->assertNoEvents(); |
| 9317 | window->assertNoEvents(); |
| 9318 | } |
| 9319 | |
| 9320 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 9321 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9322 | overlay->setSpy(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 9323 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9324 | |
| 9325 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9326 | overlay->consumeMotionDown(); |
| 9327 | window->consumeMotionDown(); |
| 9328 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9329 | overlay->consumeMotionUp(); |
| 9330 | window->consumeMotionUp(); |
| 9331 | |
| 9332 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9333 | window->consumeMotionDown(); |
| 9334 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 9335 | window->consumeMotionUp(); |
| 9336 | |
| 9337 | overlay->assertNoEvents(); |
| 9338 | window->assertNoEvents(); |
| 9339 | } |
| 9340 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 9341 | /** |
| 9342 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 9343 | * The scenario is as follows: |
| 9344 | * - The stylus interceptor overlay is configured as a spy window. |
| 9345 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 9346 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 9347 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 9348 | */ |
| 9349 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 9350 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 9351 | overlay->setSpy(true); |
| 9352 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9353 | |
| 9354 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 9355 | overlay->consumeMotionDown(); |
| 9356 | window->consumeMotionDown(); |
| 9357 | |
| 9358 | // The interceptor pilfers the pointers. |
| 9359 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 9360 | window->consumeMotionCancel(); |
| 9361 | |
| 9362 | // The interceptor configures itself so that it is no longer a spy. |
| 9363 | overlay->setSpy(false); |
| 9364 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); |
| 9365 | |
| 9366 | // It continues to receive the rest of the stylus gesture. |
| 9367 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 9368 | overlay->consumeMotionMove(); |
| 9369 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 9370 | overlay->consumeMotionUp(); |
| 9371 | |
| 9372 | window->assertNoEvents(); |
| 9373 | } |
| 9374 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9375 | struct User { |
| 9376 | int32_t mPid; |
| 9377 | int32_t mUid; |
| 9378 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 9379 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 9380 | |
| 9381 | User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) |
| 9382 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 9383 | |
| 9384 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
| 9385 | return injectMotionEvent(mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
| 9386 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 9387 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9388 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 9389 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9390 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 9391 | } |
| 9392 | |
| 9393 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9394 | return inputdispatcher::injectKey(mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9395 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9396 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9397 | mPolicyFlags); |
| 9398 | } |
| 9399 | |
| 9400 | sp<FakeWindowHandle> createWindow() const { |
| 9401 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 9402 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9403 | sp<FakeWindowHandle> window = |
| 9404 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Owned Window", |
| 9405 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 9406 | window->setOwnerInfo(mPid, mUid); |
| 9407 | return window; |
| 9408 | } |
| 9409 | }; |
| 9410 | |
| 9411 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 9412 | |
| 9413 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
| 9414 | auto owner = User(mDispatcher, 10, 11); |
| 9415 | auto window = owner.createWindow(); |
| 9416 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9417 | |
| 9418 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9419 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9420 | window->consumeMotionDown(); |
| 9421 | |
| 9422 | setFocusedWindow(window); |
| 9423 | window->consumeFocusEvent(true); |
| 9424 | |
| 9425 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9426 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9427 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9428 | } |
| 9429 | |
| 9430 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
| 9431 | auto owner = User(mDispatcher, 10, 11); |
| 9432 | auto window = owner.createWindow(); |
| 9433 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 9434 | |
| 9435 | auto rando = User(mDispatcher, 20, 21); |
| 9436 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9437 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9438 | |
| 9439 | setFocusedWindow(window); |
| 9440 | window->consumeFocusEvent(true); |
| 9441 | |
| 9442 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 9443 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 9444 | window->assertNoEvents(); |
| 9445 | } |
| 9446 | |
| 9447 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
| 9448 | auto owner = User(mDispatcher, 10, 11); |
| 9449 | auto window = owner.createWindow(); |
| 9450 | auto spy = owner.createWindow(); |
| 9451 | spy->setSpy(true); |
| 9452 | spy->setTrustedOverlay(true); |
| 9453 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); |
| 9454 | |
| 9455 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9456 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9457 | spy->consumeMotionDown(); |
| 9458 | window->consumeMotionDown(); |
| 9459 | } |
| 9460 | |
| 9461 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
| 9462 | auto owner = User(mDispatcher, 10, 11); |
| 9463 | auto window = owner.createWindow(); |
| 9464 | |
| 9465 | auto rando = User(mDispatcher, 20, 21); |
| 9466 | auto randosSpy = rando.createWindow(); |
| 9467 | randosSpy->setSpy(true); |
| 9468 | randosSpy->setTrustedOverlay(true); |
| 9469 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9470 | |
| 9471 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 9472 | // not receive the event. |
| 9473 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9474 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9475 | randosSpy->assertNoEvents(); |
| 9476 | window->consumeMotionDown(); |
| 9477 | } |
| 9478 | |
| 9479 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
| 9480 | auto owner = User(mDispatcher, 10, 11); |
| 9481 | auto window = owner.createWindow(); |
| 9482 | |
| 9483 | auto rando = User(mDispatcher, 20, 21); |
| 9484 | auto randosSpy = rando.createWindow(); |
| 9485 | randosSpy->setSpy(true); |
| 9486 | randosSpy->setTrustedOverlay(true); |
| 9487 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosSpy, window}}}); |
| 9488 | |
| 9489 | // A user that has injection permission can inject into any window. |
| 9490 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9491 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 9492 | ADISPLAY_ID_DEFAULT)); |
| 9493 | randosSpy->consumeMotionDown(); |
| 9494 | window->consumeMotionDown(); |
| 9495 | |
| 9496 | setFocusedWindow(randosSpy); |
| 9497 | randosSpy->consumeFocusEvent(true); |
| 9498 | |
| 9499 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
| 9500 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9501 | window->assertNoEvents(); |
| 9502 | } |
| 9503 | |
| 9504 | TEST_F(InputDispatcherTargetedInjectionTest, CanGenerateActionOutsideToOtherUids) { |
| 9505 | auto owner = User(mDispatcher, 10, 11); |
| 9506 | auto window = owner.createWindow(); |
| 9507 | |
| 9508 | auto rando = User(mDispatcher, 20, 21); |
| 9509 | auto randosWindow = rando.createWindow(); |
| 9510 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 9511 | randosWindow->setWatchOutsideTouch(true); |
| 9512 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {randosWindow, window}}}); |
| 9513 | |
| 9514 | // We allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
| 9515 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9516 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 9517 | window->consumeMotionDown(); |
| 9518 | randosWindow->consumeMotionOutside(); |
| 9519 | } |
| 9520 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 9521 | } // namespace android::inputdispatcher |